package it.csi.demo.democomp.presentation.democomp.action;

import java.util.*;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.validator.annotations.*;

import it.csi.demo.democomp.dto.*;
import it.csi.demo.democomp.business.*;
import it.csi.demo.democomp.presentation.democomp.interceptor.FatClientOnly;

/**
 * MenuAction Action Class.
 *
 * @author GuiGen
 */
@Validation()
public class MenuAction extends BaseAction {

	/**
	 * classe model associata
	 */
	public Class modelClass() {
		return it.csi.demo.democomp.dto.GlobalMenuModel.class;
	}

	/**
	 *
	 * @return Il risultato delle azioni, SUCCESS altrimenti.
	 */
	@SkipValidation
	public String execute() throws CommandExecutionException {
		return SUCCESS;
	}

	/**
	 * 
	 * @return SUCCESS result.
	 */
	public String goToMnuHome() throws CommandExecutionException {

		// gestione degli eventi di tipo CLICKED
		ICommand action = initCommand("mnuHome", "CLICKED");
		String result = action.doCommand(this);
		if (result != null) {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToMnuHome] returning result ["
						+ result + "]");
			}
			setActiveMenu("mnuHome");
			return result;
		} else {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToMnuHome] returning default result [SUCCESS]");
			}
			setActiveMenu("mnuHome");
			return SUCCESS;
		}
	}

	/**
	 * 
	 * @return SUCCESS result.
	 */
	public String goToNumHomeDsp() throws CommandExecutionException {

		// gestione degli eventi di tipo CLICKED
		ICommand action = initCommand("numHomeDsp", "CLICKED");
		String result = action.doCommand(this);
		if (result != null) {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToNumHomeDsp] returning result ["
						+ result + "]");
			}
			setActiveMenu("numHomeDsp");
			return result;
		} else {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToNumHomeDsp] returning default result [SUCCESS]");
			}
			setActiveMenu("numHomeDsp");
			return SUCCESS;
		}
	}

	/**
	 * 
	 * @return SUCCESS result.
	 */
	public String goToNumHomeMdd() throws CommandExecutionException {

		// gestione degli eventi di tipo CLICKED
		ICommand action = initCommand("numHomeMdd", "CLICKED");
		String result = action.doCommand(this);
		if (result != null) {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToNumHomeMdd] returning result ["
						+ result + "]");
			}
			setActiveMenu("numHomeMdd");
			return result;
		} else {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToNumHomeMdd] returning default result [SUCCESS]");
			}
			setActiveMenu("numHomeMdd");
			return SUCCESS;
		}
	}

	/**
	 * 
	 * @return SUCCESS result.
	 */
	public String goToMnuNewsMDD() throws CommandExecutionException {

		// gestione degli eventi di tipo CLICKED
		ICommand action = initCommand("mnuNewsMDD", "CLICKED");
		String result = action.doCommand(this);
		if (result != null) {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToMnuNewsMDD] returning result ["
						+ result + "]");
			}
			setActiveMenu("mnuNewsMDD");
			return result;
		} else {
			if (log.isDebugEnabled()) {
				log.debug("[MenuAction::goToMnuNewsMDD] returning default result [SUCCESS]");
			}
			setActiveMenu("mnuNewsMDD");
			return SUCCESS;
		}
	}

	public static final String ACTIVE_MENU_ATTRNAME = "active_menu";

	public void setActiveMenu(String menuName) {
		session.put(ACTIVE_MENU_ATTRNAME, "menu_items_" + menuName);
	}

	/**
	 * inizializza la struttura dei command da eseguire per ciascun event handler 
	 * di ciascun widget
	 */
	private ICommand initCommand(String sourceWidget, String eventType) {
		HashMap<String, HashMap<String, ICommand>> cmdsByWidget = new HashMap<String, HashMap<String, ICommand>>();

		// contiene i comandi del menu mnuHome per ogni Ev.H.
		HashMap<String, ICommand> cmds4mnuHomeByEvh = new HashMap<String, ICommand>();

		cmds4mnuHomeByEvh.put("CLICKED", initCommandMnuHome_CLICKED());
		cmdsByWidget.put("mnuHome", cmds4mnuHomeByEvh);
		// contiene i comandi del menu numHomeDsp per ogni Ev.H.
		HashMap<String, ICommand> cmds4numHomeDspByEvh = new HashMap<String, ICommand>();

		cmds4numHomeDspByEvh.put("CLICKED", initCommandNumHomeDsp_CLICKED());
		cmdsByWidget.put("numHomeDsp", cmds4numHomeDspByEvh);
		// contiene i comandi del menu numHomeMdd per ogni Ev.H.
		HashMap<String, ICommand> cmds4numHomeMddByEvh = new HashMap<String, ICommand>();

		cmds4numHomeMddByEvh.put("CLICKED", initCommandNumHomeMdd_CLICKED());
		cmdsByWidget.put("numHomeMdd", cmds4numHomeMddByEvh);
		// contiene i comandi del menu mnuNewsMDD per ogni Ev.H.
		HashMap<String, ICommand> cmds4mnuNewsMDDByEvh = new HashMap<String, ICommand>();

		cmds4mnuNewsMDDByEvh.put("CLICKED", initCommandMnuNewsMDD_CLICKED());
		cmdsByWidget.put("mnuNewsMDD", cmds4mnuNewsMDDByEvh);

		return cmdsByWidget.get(sourceWidget).get(eventType);
	}

	private ICommand initCommandMnuHome_CLICKED() {
		// ExecCommand begin
		String[] resultNames4goToHome = new String[]{"ok"};

		ICommand[] actionsForResults4goToHome = new ICommand[1];
		/// Jump Command begin
		JumpCommand act_actions_clicked_mnuHome_resokAction_0 = new JumpCommand(
				"cpHome", null, false);
		/// Jump Command end
		actionsForResults4goToHome[0] = act_actions_clicked_mnuHome_resokAction_0;

		ExecCommand act_actions_clicked_mnuHome_1 = new ExecCommand(
				resultNames4goToHome, actionsForResults4goToHome) {
			public ExecResults doLogic(Object theModel)
					throws CommandExecutionException {
				it.csi.util.performance.StopWatch watcher = new it.csi.util.performance.StopWatch(
						it.csi.demo.democomp.util.Constants.APPLICATION_CODE);
				ExecResults result = null;
				try {
					watcher.start();
					result = getSpringBackEnd()
							.goToHome(
									(it.csi.demo.democomp.dto.GlobalMenuModel) theModel);
					watcher.stop();
					watcher.dumpElapsed("", "readOne()",
							"chiamata verso BackEnd", "goToHome");
					return result;
				} catch (BEException e) {
					throw new CommandExecutionException(
							"errore non gestito nell'esecuzione del metodo [goToHome]:"
									+ e.getMessage(), e);
				}

			}

			public void storeAppData(ExecResults res, BaseAction strutsAction) {
				// nothing to store...
			}
		};
		// Exec Action end
		return act_actions_clicked_mnuHome_1;
	}
	private ICommand initCommandNumHomeDsp_CLICKED() {
		/// Jump Ext Command begin
		JumpExtCommand act_actions_clicked_numHomeDsp_1 = new JumpExtCommand(
				"homepagedsp");
		/// Jump Ext Command end
		return act_actions_clicked_numHomeDsp_1;
	}
	private ICommand initCommandNumHomeMdd_CLICKED() {
		/// Jump Ext Command begin
		JumpExtCommand act_actions_clicked_numHomeMdd_1 = new JumpExtCommand(
				"homemdd");
		/// Jump Ext Command end
		return act_actions_clicked_numHomeMdd_1;
	}
	private ICommand initCommandMnuNewsMDD_CLICKED() {
		/// Jump Ext Command begin
		JumpExtCommand act_actions_clicked_mnuNewsMDD_1 = new JumpExtCommand(
				"extdoc");
		/// Jump Ext Command end
		return act_actions_clicked_mnuNewsMDD_1;
	}

	/**
	 * Gestione della validazione
	 */
	public void validate() {
		/*PROTECTED REGION ID(R-963475072) ENABLED START*/
		/* Inserire la validazione */
		/*PROTECTED REGION END*/
	}

	/**
	 *	Metodo per la rimozione dalla sessione degli application data a scope
	 *  SAME_PAGE. 
	 */
	public void clearPageScopedAppData() {
		// non sapendo quale content panel si sta abbandonando vengono rimossi tutti
		// gli appdata a scope PAGE

	}
}
