package smserve_main;

import structures.Messagequeue;

public class Messagequeuehandler {
	
	public String getNewMessages(String pUser) throws Exception {
		String result = "";
		for (int i = 0; 
			i < smserve_main.SMserve.mails.get(this.getMessagequeueId(pUser)).messages.size(); 
			i++) {
			if (smserve_main.SMserve.mails.get(this.getMessagequeueId(pUser)).messages.get(i).reads == 0) {
				result +=
					smserve_main.SMserve.mails.get(this.getMessagequeueId(pUser)).messages.get(i).fromUser + "|" +
					smserve_main.SMserve.mails.get(this.getMessagequeueId(pUser)).messages.get(i).message + ";";
				smserve_main.SMserve.mails.get(this.getMessagequeueId(pUser)).messages.get(i).increment();
			}
		}
		return result;
	}
	
	public String getMessages() {
		String result = "";
		for (int i = 0; i < smserve_main.SMserve.mails.size(); i++) {
			for (int k = 0; k < smserve_main.SMserve.mails.get(i).messages.size(); k++) {
				result += smserve_main.SMserve.mails.get(i).messages.get(k).fromUser + "|" + 
					smserve_main.SMserve.mails.get(i).messages.get(k).message + ";";
					smserve_main.SMserve.mails.get(i).messages.get(k).increment();
			}
		}
		return result;
	}
	
	public int getMessagequeueId(String pUser) throws Exception {
		for (int i=0; i<smserve_main.SMserve.mails.size(); i++) {
			if (smserve_main.SMserve.mails.get(i).user.equals(pUser)) {
				return i;
			}
		}
		throw new Exception("No Messagequeue");
	}
	
	public Messagequeue getMessagequeue(String pUser) throws Exception {
		return smserve_main.SMserve.mails.get(this.getMessagequeueId(pUser));
	}
}
