package geisplugin.popup.actions;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

import geisplugin.Tools;

/**
 * DE: 
 *
 * EN: 
 * 
 * 
 * Program changes
 * **************************************************************************************
 * Date			* Ticket	* KonzFirm	* Responsible	* programmer    *
 * Change										*
 * **************************************************************************************
 *
 * @author huellmante
 * @version 1.0
 *
 */

public class CCJspGenerator extends AbstractHandler {

	@Override
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		try {
			if (Tools.isNotUpToDate()) {
				return null;
			} 
		} finally {
			try {
				if (Tools.con != null && !Tools.con.isClosed()) {
					try {
						Tools.con.rollback();
						Tools.con.close();
					} catch (final SQLException e) {
					}
					Tools.con = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	    Shell shell = HandlerUtil.getActiveShell(event);
	    
	    String path = "";
	    try {
	    	path = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getCanonicalPath()+Tools.getCurrentSelectedProject().getFullPath().toString().replace('/', '\\')+"\\";
		} catch (Exception e) {}
	    
	    //MessageDialog.openInformation(shell, "path", path);
	    
	    File webcontent = new File(path+"webcontent/");
	    
	    if (path == null || path.isEmpty() || !webcontent.exists() || !webcontent.isDirectory()) {
	    	MessageDialog.openError(shell, "Info", "Error, can't find a valid webcontent path.");
	    	return null;
	    }
	    
	    path += "webcontent\\";
	    
	    InputDialog dia = new InputDialog(shell, "Set your .jsp path", "Set your .jsp path after "+path, "", null);
	    dia.open();
	    
	    String jsp = dia.getValue();
	    
	    if (jsp.isEmpty()) {
	    	MessageDialog.openError(shell, "Error", "Empty path.");
	    	return null;
	    }
	    
	    if (!jsp.toLowerCase().endsWith(".jsp")) {
	    	jsp += ".jsp";
	    }
	    
	    File newJspFile = new File(path+jsp);
	    newJspFile.getParentFile().mkdirs();
	    
	    try {
			Tools.writeNoPathCreation(newJspFile.getAbsolutePath(), "<%@page contentType=\"text/html\"%>\r\n<%@page pageEncoding=\"UTF-8\"%>\r\n\r\n<%@taglib prefix=\"f\" uri=\"http://java.sun.com/jsf/core\"%>\r\n<%@taglib prefix=\"h\" uri=\"http://java.sun.com/jsf/html\"%>\r\n\r\n@@taglibraries@@\r\n\r\n<!-- ========== CONTENT BEGIN ========== -->\r\n<f:view>\r\n<h:form>\r\n<f:subview id=\"template\">\r\n\r\n    <t:rowtitlebar id=\"g_2\" />\r\n    <t:rowheader id=\"g_3\" />\r\n    <t:rowbodypane id=\"g_4\" />\r\n    <t:rowstatusbar id=\"g_5\" />\r\n    \r\n<t:pageaddons/>\r\n</f:subview>\r\n</h:form>\r\n</f:view>\r\n<!-- ========== CONTENT END ========== -->\r\n");
		} catch (final IOException e) {
			MessageDialog.openError(shell, "Error", e.getMessage());
			e.printStackTrace();
			return null;
		}
	    
	    try {
			Tools.getCurrentSelectedProject().refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (final Exception e) {
			MessageDialog.openError(shell, "Info", "Error, can't refresh your workplace automatically");
		}
	    
	    return null;
	}

}
