/**
 * 
 */
package com.n2bb.sysmonui.alerts;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import com.tandbergtv.workflow.core.health.ComponentHealthStatus;
import com.tandbergtv.workflow.monitor.AlertSystemHealthManager;

/**
 * @author venkataprasath
 * This class will check the status of the Alert Server Components and redirect to corresponding action classes if the alert server is running or redirect to the error 
 */
public class AlertServerStatusAction extends DispatchAction {

	ActionMessages errors = new ActionMessages();

	private static final Logger logger = Logger
			.getLogger(AlertServerStatusAction.class);

	private static final String ALERT_SYSTEM_RUNNING = "running";

	private static final String ALERT_SYSTEM_DOWN = "systemDown";
	
	private static final String ALERT_SERVER_STATUS = "alertServerStatus";

	private static final String REDIRECT_TO_LIST_ALERT_PAGE = "listAlerts";

	private static final String REDIRECT_TO_CREATE_ALERT_PAGE = "createAlert";

	private static final String REDIRECT_TO_EDIT_ALERT_PAGE = "editAlert";

	private static final String REDIRECT_TO_MODIFY_ALERTS_PAGE = "modifyAlerts";

	private static final String REDIRECT_TO_REPORT_ALERT_PAGE = "reportAlerts";

	private static final String REDIRECT_TO_SAVE_CREATED_ALERT_PAGE = "saveCreatedAlert";

	private static final String REDIRECT_TO_SAVE_MODIFIED_ALERT_PAGE = "saveModifiedAlert";

	private static final String REDIRECT_TO_LIST_ALERTPATTERNS_PAGE = "listAlertPatterns";

	private static final String REDIRECT_TO_CREATE_ALERTPATTERNS_PAGE = "createAlertPattern";

	private static final String REDIRECT_TO_EDIT_ALERTPATTERNS_PAGE = "editAlertPattern";

	private static final String REDIRECT_TO_MODIFY_ALERTPATTERNS_PAGE = "modifyAlertPatterns";

	private static final String REDIRECT_TO_REPORT_ALERTPATTERNS_PAGE = "reportAlertPatterns";

	private static final String REDIRECT_TO_SAVE_CREATED_ALERTPATTERNS_PAGE = "saveCreatedAlertPattern";

	private static final String REDIRECT_TO_SAVE_MODIFIED_ALERTPATTERNS_PAGE = "saveModifiedAlertPattern";

	private static final String REDIRECT_TO_LIST_ALERT_HISTORY_PAGE = "listAlertHistory";

	private static final String REDIRECT_TO_FILTER_ALERT_HISTORY_PAGE = "filterAlertHistory";

	private static final String REDIRECT_TO_REPORT_ALERT_HISTORY_PAGE = "reportAlertHistory";
	
	private static final String REDIRECT_TO_ALERT_HISTORY_DETAIL_PAGE = "alertHistoryDetail";

	private static final String ERROR_MESSAGE = "Alert system is down";

	public AlertServerStatusAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getSystemStatus(HttpServletRequest request) {
		String status = ALERT_SYSTEM_RUNNING;
		AlertSystemHealthManager alertSystemHealthManager = AlertSystemHealthManager
				.getInstance();

		alertSystemHealthManager.pollHealthStatus();

		if (alertSystemHealthManager.getHealthStatus() == ComponentHealthStatus.ERROR) {
			if (errors.isEmpty()) {
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
						ERROR_MESSAGE, false));
			}
			saveErrors(request, errors);
			status = ALERT_SYSTEM_DOWN;
		}
		request.setAttribute(ALERT_SERVER_STATUS, status);
		return status;
	}

	public ActionForward toListAlerts(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_LIST_ALERT_PAGE);
	}

	public ActionForward toCreateAlert(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_CREATE_ALERT_PAGE);
	}

	public ActionForward toEditAlert(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_EDIT_ALERT_PAGE);
	}

	public ActionForward toModifyAlerts(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_MODIFY_ALERTS_PAGE);
	}

	public ActionForward toReportAlerts(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_REPORT_ALERT_PAGE);
	}

	public ActionForward toSaveCreatedAlert(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_SAVE_CREATED_ALERT_PAGE);
	}

	public ActionForward toSaveModifiedAlert(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_SAVE_MODIFIED_ALERT_PAGE);
	}

	public ActionForward toListAlertPatterns(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_LIST_ALERTPATTERNS_PAGE);
	}

	public ActionForward toCreateAlertPattern(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_CREATE_ALERTPATTERNS_PAGE);
	}

	public ActionForward toEditAlertPattern(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_EDIT_ALERTPATTERNS_PAGE);
	}

	public ActionForward toModifyAlertPatterns(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_MODIFY_ALERTPATTERNS_PAGE);
	}

	public ActionForward toReportAlertPatterns(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_REPORT_ALERTPATTERNS_PAGE);
	}

	public ActionForward toSaveCreatedAlertPattern(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping
				.findForward(REDIRECT_TO_SAVE_CREATED_ALERTPATTERNS_PAGE);
	}

	public ActionForward toSaveModifiedAlertPattern(
			ActionMapping actionMapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping
				.findForward(REDIRECT_TO_SAVE_MODIFIED_ALERTPATTERNS_PAGE);
	}

	public ActionForward toListAlertHistory(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_LIST_ALERT_HISTORY_PAGE);
	}

	public ActionForward toFilterAlertHistory(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_FILTER_ALERT_HISTORY_PAGE);
	}

	public ActionForward toReportAlertsHistory(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_REPORT_ALERT_HISTORY_PAGE);
	}
	public ActionForward toAlertHistoryDetail(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return actionMapping.findForward(REDIRECT_TO_ALERT_HISTORY_DETAIL_PAGE);
	}
	public ActionForward toSystemDown(ActionMapping actionMapping,
			HttpServletRequest request) {
		if (getSystemStatus(request).equals(ALERT_SYSTEM_DOWN)) {
			return actionMapping.findForward(ALERT_SYSTEM_DOWN);
		}
		return null;
	}

}
