package com.cronos.admin.panel;

import java.io.File;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.cronos.admin.services.ConfigurationBean;
import com.cronos.admin.services.ConfigurationCCBean;
import com.cronos.admin.services.ConfigurationServerBean;
import com.cronos.admin.services.ConfigurationService;
import com.cronos.admin.services.HttpsConfigurator;
import com.cronos.admin.tomcat.RestartTomcatThread;

public class PanelAction extends DispatchAction {

	public static final int tiempoEspera = 20000;

	public ActionForward show(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		Boolean validSession = (Boolean) request.getSession().getAttribute("validsession");
		if (validSession == null || !validSession.booleanValue())
			return (mapping.findForward("login"));

		PanelForm panelForm = (PanelForm) form;

		String relativeWebPath = "/WEB-INF";
		String absoluteDiskPath = getServlet().getServletContext().getRealPath(relativeWebPath);

		ConfigurationBean conf = ConfigurationService.readXMLConfiguration(absoluteDiskPath);

		ConfigurationCCBean confCC = ConfigurationService.readXMLCCConfiguration(conf.getPath(), absoluteDiskPath);
		ConfigurationServerBean confCS = ConfigurationService.readXMLServerConfiguration(conf.getPathServerXML());

		ConfigurationService.ConfigurationCCBeanToPanelForm(confCC, panelForm);
		ConfigurationService.ConfigurationServerBeanToPanelForm(confCS, panelForm);

		request.setAttribute("message", "");
		return (mapping.findForward("show"));
	}

	@SuppressWarnings("finally")
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		Boolean validSession = (Boolean) request.getSession().getAttribute("validsession");
		if (validSession == null || !validSession.booleanValue())
			return (mapping.findForward("login"));

		ResourceBundle rb = ResourceBundle.getBundle("com.cronos.admin.resources.MessageResources",
				this.getLocale(request));
		PanelForm panelForm = (PanelForm) form;

		String relativeWebPath = "/WEB-INF";
		String absoluteDiskPath = getServlet().getServletContext().getRealPath(relativeWebPath);
		ConfigurationBean conf = ConfigurationService.readXMLConfiguration(absoluteDiskPath);

		ConfigurationCCBean confCC = new ConfigurationCCBean();
		ConfigurationServerBean confCS = new ConfigurationServerBean();

		ConfigurationService.PanelFormToConfigurationCCBean(panelForm, confCC);
		ConfigurationService.PanelFormToConfigurationServerBean(panelForm, confCS);
		// Guardar

		try {

			if (panelForm.getSslEnabled()) {
				if (panelForm.getKeystoreFile() == null || panelForm.getKeystoreFile().isEmpty()
						|| panelForm.getKeystoreFile().equals("autom")) {
					String nameKeystore = HttpsConfigurator.K_nameKeystore;
					String keyPass = HttpsConfigurator.K_keyPass;

					String java_home = System.getProperty("java.home");

					String path_keystore = java_home + File.separator + "lib" + File.separator + "security";
					String fullpathKeystore = path_keystore + File.separator + nameKeystore;

					confCS.setKeyStoreFile(fullpathKeystore);
					confCS.setKeyStorePass(keyPass);

					panelForm.setKeystoreFile(fullpathKeystore);
					panelForm.setKeystorePass(keyPass);

					HttpsConfigurator.CreateKeystore(fullpathKeystore, keyPass, confCS.getLinuxRootPassword());
				}

			}

			ConfigurationService.SaveCCConfiguration(confCC, conf.getPath());
			ConfigurationService.SaveServerConfiguration(confCS, conf.getPathServerXML());

			request.setAttribute("message", rb.getString("panel.mensaje.grabarok"));

		} catch (Exception ex) {
			request.setAttribute("message", rb.getString("panel.mensaje.grabarerror"));
		} finally {
			return (mapping.findForward("show"));
		}
	}

	public ActionForward cerrarsession(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.getSession().removeAttribute("validsession");
		return (mapping.findForward("closesession"));
	}

	public ActionForward restart(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		Boolean validSession = (Boolean) request.getSession().getAttribute("validsession");
		if (validSession == null || !validSession.booleanValue())
			return (mapping.findForward("login"));

		// TomcatService.Restart();
		RestartTomcatThread rt = new RestartTomcatThread();
		rt.run();
		try {
			Thread.sleep(tiempoEspera);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return (mapping.findForward("restart"));
	}
}
