package tigase.jaxmpp.core.client.xmpp.stanzas;

import tigase.jaxmpp.core.client.xml.DefaultElement;
import tigase.jaxmpp.core.client.xml.Element;
import tigase.jaxmpp.core.client.xml.XMLException;

public class Presence extends Stanza {

	public static enum Show {
		away(3),
		chat(5),
		dnd(1),
		online(4),
		xa(2);

		private final int weight;

		private Show(int weight) {
			this.weight = weight;
		}

		public int getWeight() {
			return weight;
		}

	}

	public static Presence create() throws XMLException {
		return new Presence(new DefaultElement("presence"));
	}

	public Presence(Element element) throws XMLException {
		super(element);
		if (!"presence".equals(element.getName()))
			throw new RuntimeException("Wrong element name: " + element.getName());
	}

	public String getNickname() throws XMLException {
		return getChildElementValue("nick", "http://jabber.org/protocol/nick");
	}

	public Integer getPriority() throws XMLException {
		String x = getChildElementValue("priority");
		if (x == null)
			return 0;
		return Integer.valueOf(x);
	}

	public Show getShow() throws XMLException {
		String x = getChildElementValue("show");
		if (x == null)
			return Show.online;
		return Show.valueOf(x);
	}

	public String getStatus() throws XMLException {
		return getChildElementValue("status");
	}

	public void setNickname(String value) throws XMLException {
		setChildElementValue("nick", "http://jabber.org/protocol/nick", value == null ? null : value.toString());
	}

	public void setPriority(Integer value) throws XMLException {
		setChildElementValue("priority", value == null ? null : value.toString());
	}

	public void setShow(Show value) throws XMLException {
		if (value == null || value == Show.online)
			setChildElementValue("show", null);
		else
			setChildElementValue("show", value.name());
	}

	public void setStatus(String value) throws XMLException {
		setChildElementValue("status", value);
	}

}

@@ -563,32 +563,239 @@
 resence%22));%0A%09%7D%0A%0A
+%09private String cacheNickname;%0A%0A%09private boolean cacheNicknameSet = false;%0A%0A%09private Integer cachePriority;%0A%0A%09private Show cacheShow;%0A%0A%09private String cacheStatus;%0A%0A%09private boolean cacheStatusSet = false;%0A%0A
 %09public Presence
@@ -1021,38 +1021,97 @@
 MLException %7B%0A%09%09
-return
+if (cacheNicknameSet)%0A%09%09%09return cacheNickname;%0A%0A%09%09cacheNickname =
  getChildElement
@@ -1156,24 +1156,76 @@
 col/nick%22);%0A
+%09%09cacheNicknameSet = true;%0A%0A%09%09return cacheNickname;%0A
 %09%7D%0A%0A%09public 
@@ -1274,110 +1274,221 @@
 %7B%0A%09%09
-String x = getChildElementValue(%22priority%22);%0A%09%09if (x == null)%0A%09%09%09return 0;%0A%09%09return Integer.valueOf(x)
+if (cachePriority != null)%0A%09%09%09return cachePriority;%0A%0A%09%09String x = getChildElementValue(%22priority%22);%0A%09%09final Integer p;%0A%09%09if (x == null)%0A%09%09%09p = 0;%0A%09%09else%0A%09%09%09p = Integer.valueOf(x);%0A%0A%09%09cachePriority = p;%0A%0A%09%09return p
 ;%0A%09%7D
@@ -1534,16 +1534,63 @@
 ption %7B%0A
+%09%09if (cacheShow != null)%0A%09%09%09return cacheShow;%0A%0A
 %09%09String
@@ -1624,16 +1624,35 @@
 show%22);%0A
+%09%09final Show show;%0A
 %09%09if (x 
@@ -1663,22 +1663,22 @@
 ull)%0A%09%09%09
-return
+show =
  Show.on
@@ -1685,22 +1685,30 @@
 line;%0A%09%09
-return
+else%0A%09%09%09show =
  Show.va
@@ -1717,16 +1717,51 @@
 eOf(x);%0A
+%09%09cacheShow = show;%0A%09%09return show;%0A
 %09%7D%0A%0A%09pub
@@ -1811,45 +1811,145 @@
 %7B%0A%09%09
-return getChildElementValue(%22status%22)
+if (cacheStatusSet)%0A%09%09%09return cacheStatus;%0A%09%09cacheStatus = getChildElementValue(%22status%22);%0A%09%09cacheStatusSet = true;%0A%0A%09%09return cacheStatus
 ;%0A%09%7D

add cache for Presence properties