package it.mice.voila.runtime.infobar;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Default implementation of the InfoBarManager interface.
 * By default session timeout and user info should be exposed to the info bar.
 * By default delegate message list to a MessageBroker implementation array.
 */
public class InfoBarManagerImpl implements InfoBarManager {
    /**
     * Array of message broker implementation to delegate message list extraction to.
     */
	private MessageBroker[] messageBrokers;
    /**
     * Include session timeout info
     */
    boolean includeSessionTimeoutMessage = true;
    /**
     * Include user info
     */
    boolean includeUserInfoMessage = true;

    /**
     * Getter for message broker implementation list.
     * @return message broker implementation list
     */
    public MessageBroker[] getMessageBrokers() {
        return messageBrokers;
    }

    /**
     * Setter for message broker implementation list.
     * @param messageBrokers message broker implementation list to set
     */
    public void setMessageBrokers(MessageBroker[] messageBrokers) {
        this.messageBrokers = messageBrokers;
    }

    /**
     * Getter for includeSessionTimeoutMessage.
     * @return includeSessionTimeoutMessage
     */
	public boolean isIncludeSessionTimeoutMessage() {
		return includeSessionTimeoutMessage;
	}

    /**
     * Getter for includeUserInfoMessage.
     * @return includeUserInfoMessage
     */
	public boolean isIncludeUserInfoMessage() {
		return includeUserInfoMessage;
	}

    /**
     * Setter for includeSessionTimeoutMessage.
     * @param includeSessionTimeoutMessage to set
     */
	public void setIncludeSessionTimeoutMessage(boolean includeSessionTimeoutMessage) {
		this.includeSessionTimeoutMessage = includeSessionTimeoutMessage;
	}

    /**
     * Setter for includeUserInfoMessage.
     * @param includeUserInfoMessage to set
     */
	public void setIncludeUserInfoMessage(boolean includeUserInfoMessage) {
		this.includeUserInfoMessage = includeUserInfoMessage;
	}

	/**
	 * Default implementation that return all messages returned by each Message Broker.
	 * @return all messages returned by each Message Broker invoked.
	 */
	public Collection<String> getMessageList() {
        ArrayList<String> messages = new ArrayList<String>();
        if (messageBrokers != null) {
            for (int i = 0; i < messageBrokers.length; i++) {
            	Collection<String> msgs = messageBrokers[i].getMessages();
            	if (msgs != null) {
                    messages.addAll(msgs);
            	}
            }
        }
        return messages;
    }
}
