package zall.util.msg;

import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Logger;

import javax.servlet.http.HttpSession;

import zutil.log.LogUtil;

/**
 * This class represents all the messages to a single user.
 * The class is implemented as an Iterator.
 * 
 * <XMP>
 * Example HTML:
 * 
<SCRIPT type="text/javascript">
    jQuery(document).ready(function(){
        jQuery(".message").click(function(){
            jQuery(this).animate({
                opacity: 0
            }, 300).animate({
                height: 0,
                border: 0
            }, 300, function(){
                jQuery(this).css("display","none");
            });
        });
    });
</SCRIPT>
<div class="menu">
    <div class="message" style="border: 2px solid #E6E600; padding: 0px; margin: 5px; background: #FFFF99 none repeat scroll 0%; font-size: 11px; color: black; border-radius: 7px;">
        <center><b>Warning:</b></center>
    </div>
    <div class="message" style="border: 2px solid #FF0000; padding: 0px; margin: 5px; background: #FFDDCC none repeat scroll 0%; font-size: 11px; color: black; border-radius: 7px;">
        <center><b>Error:</b></center>
    </div>
    <div class="message" style="border: 2px solid #039C00; padding: 0px; margin: 5px; background: #9BFB66 none repeat scroll 0%; font-size: 11px; color: black; border-radius: 7px;">
        <center><b>Info:</b></center>
    </div>
</div>
 * 
 * </XMP>
 * 
 * @author Ziver
 */
public class UserMessage{
	public static final Logger logger = LogUtil.getLogger();
	public static final String SESSION_USERMESSAGE_KEY = "AJAX_USER_MESSAGES";
	
	/**
	 * Is the different types of messages
	 */
	public enum MessageType{
		ERROR, WARNING, INFO
	}
	/**
	 * This class represents a single message to a user
	 */
	protected class Message{
		MessageType type;
		String msg;
	}
	/** is the queue that contains the messages **/
	private Queue<Message> msg_queue;
	/** is the current message **/
	private Message current;
	
	
	public UserMessage(){
		msg_queue = new LinkedList<Message>();
	}
	
	//******** Queue methods
	
	/**
	 * Adds a new message to the queue
	 * 
	 * @param		type		is the type of the message
	 * @param		msg			is the message itself
	 */
	public void add(MessageType type, String msg){
		Message m = new Message();
		m.type = type;
		m.msg = msg;
		msg_queue.add( m );
		logger.finer("Queueing(Size: "+msg_queue.size()+") user message: "+type+"= \""+msg+"\"");
	}
	
	/**
	 * @return					if there is more messages in the queue
	 */
	public boolean hasNext(){
		return msg_queue.size() != 0;
	}
	
	/**
	 * Polls a new message from the Queue
	 * 
	 * @return					if the poll was successful
	 */
	public boolean next(){
		boolean tmp = hasNext();
		current = msg_queue.poll();
		return tmp;
	}
	
	public int size(){
		return msg_queue.size();
	}
	
	public void clear(){
		msg_queue.clear();
	}
	
	//******** Message Methods
	
	/**
	 * @return					the type of the current message
	 */
	public MessageType getType(){
		return current.type;
	}
	
	/**
	 * @return					the message of the current one
	 */
	public String getMessage(){
		return current.msg;
	}
	
	//******** Static methods
	
	public static UserMessage getUserMessage(HttpSession session){
		if( session.getAttribute(SESSION_USERMESSAGE_KEY) == null ){
			UserMessage msg = new UserMessage();
			msg.setSession( session );
			return msg;
		}
		return (UserMessage) session.getAttribute(SESSION_USERMESSAGE_KEY);
	}

	public void setSession(HttpSession session) {
		session.setAttribute(SESSION_USERMESSAGE_KEY, this);
	}
}
