package com.acme.util.ui;


import com.acme.structure.exception.base.ApplicationException;
import com.acme.structure.util.DateUtility;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class StatusUtil
{
    static Map<Integer, String> styleClass = new HashMap<Integer, String>();
    static
    {
        String informationStyle = "";
        String warningStyle = "";
        String errorStyle = "";

        styleClass.put(UserInformationMessage.INFO_MESSAGE_TYPE_INFO, informationStyle);
        styleClass.put(UserInformationMessage.INFO_MESSAGE_TYPE_WARNING, warningStyle);
        styleClass.put(UserInformationMessage.INFO_MESSAGE_TYPE_ERROR, errorStyle);
    }

    public StatusUtil() throws Exception
    {
        super();
    }

    private String appendTime()
    {
        return " (" + DateUtility.convertDateToString(DateUtility.getToday(), "HH:mm:ss") + ") ";
    }

    private UserInformationMessage appendMessage(String statusMsg, int messageType)
    {
        UserInformationMessage userInformationMessage = null;
        try
        {
            userInformationMessage = new UserInformationMessage();
            userInformationMessage.setMessageTime(appendTime());
            userInformationMessage.setIconPath("");
            userInformationMessage.setStyleClass(styleClass.get(messageType));
            userInformationMessage.setMessage(statusMsg);
            userInformationMessage.setMessageType(messageType);
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }

        return userInformationMessage;
    }

    public UserInformationMessage incomingWaitMsg()
    {
        return appendMessage(UIHelper.getStringResourceItem("Exception", "wait_msg"), UserInformationMessage.INFO_MESSAGE_TYPE_INFO);
    }

    public UserInformationMessage incomingSuccessMsg()
    {
        return appendMessage(UIHelper.getStringResourceItem("Exception", "success_msg"), UserInformationMessage.INFO_MESSAGE_TYPE_INFO);
    }

    public UserInformationMessage incomingWarningMsg(String warningMessage)
    {
        return appendMessage(UIHelper.getStringResourceItem("Exception", "warning") + warningMessage, UserInformationMessage.INFO_MESSAGE_TYPE_WARNING);
    }

    public UserInformationMessage incomingWarningMsg(ApplicationException warning)
    {
        return createUserInformationMessage(warning);
    }

    public UserInformationMessage incomingStatusMsg(String statusMsg)
    {
        return appendMessage(" " + statusMsg, UserInformationMessage.INFO_MESSAGE_TYPE_INFO);
    }

    public UserInformationMessage criticalErrorOccured(String errorMessage)
    {
        return appendMessage(" " + UIHelper.getStringResourceItem("Exception", "critical_error") + errorMessage, UserInformationMessage.INFO_MESSAGE_TYPE_ERROR);
    }

    public UserInformationMessage criticalErrorOccured(Exception error)
    {
        return appendMessage(" " + UIHelper.getStringResourceItem("Exception", "critical_error") + error.toString(), UserInformationMessage.INFO_MESSAGE_TYPE_ERROR);
    }

    public UserInformationMessage checkedExceptionOccured(ApplicationException exception)
    {
        return createUserInformationMessage(exception);
    }

    public List checkedExceptionsOccured(Collection exceptionList)
    {
        Iterator itr = exceptionList.iterator();
        List<UserInformationMessage> userMessageList = new ArrayList<UserInformationMessage>();
        while (itr.hasNext())
            userMessageList.add(createUserInformationMessage((ApplicationException)itr.next()));

        return userMessageList;
    }

    private UserInformationMessage createUserInformationMessage(ApplicationException exc)
    {
        switch (exc.getExceptionType())
        {
        case ApplicationException.EXCEPTION_TYPE_WARNING:
            return appendMessage(" " + UIHelper.getStringResourceItem("Exception", "warning") + exc.getCompleteCode() + UIHelper.getStringResourceItem("Exception", exc.getCompleteCode()) + exc.getReason() + "\n", UserInformationMessage.INFO_MESSAGE_TYPE_WARNING);
        case ApplicationException.EXCEPTION_TYPE_ERROR:
            return appendMessage(" " + UIHelper.getStringResourceItem("Exception", "error") + exc.getCompleteCode() + UIHelper.getStringResourceItem("Exception", exc.getCompleteCode()) + exc.getReason() + "\n", UserInformationMessage.INFO_MESSAGE_TYPE_ERROR);
        case ApplicationException.EXCEPTION_TYPE_INFO:
            return appendMessage(" " + UIHelper.getStringResourceItem("Exception", "info") + exc.getCompleteCode() + UIHelper.getStringResourceItem("Exception", exc.getCompleteCode()) + exc.getReason() + "\n", UserInformationMessage.INFO_MESSAGE_TYPE_INFO);
        }

        return new UserInformationMessage();
    }
}
