package geisplugin.popup.actions;


import geisplugin.Tools;

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.jdt.core.ICompilationUnit;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

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


public class AddHeaderAction 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);
		for (final Object firstElement : ((IStructuredSelection) HandlerUtil.getActiveMenuSelection(event)).toList()) {
			try {
				if (firstElement instanceof ICompilationUnit) {
					createOutput(shell, firstElement);
				} else {
					MessageDialog.openInformation(shell, "Info", "Please select Java source file");
				}
			} catch (Exception e) {
				MessageDialog.openError(shell, "Fehler", "Fehler " + e.getMessage());
			}
		}
		return null;
	}

	private void createOutput(final Shell shell, final Object firstElement) {
	    ICompilationUnit cu = (ICompilationUnit) firstElement;
		String file = cu.getResource().getRawLocation().toString();

	    try {

	    	String fileAsString = Tools.read(file);

			if (fileAsString.contains("* Program changes\n")) {
				MessageDialog.openError(shell, "Error", "Already existing!");
				return;
			}
			
			int index = 0;

			int b = fileAsString.indexOf("public class");
			if (b != -1) {
				index = b;
			}
			
			b = fileAsString.indexOf("public final class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("public final static class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("public static final class");
			if (b != -1 && b < index) {
				index = b;
			}

			b = fileAsString.indexOf("private class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private final class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private final static class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private static final class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			
			b = fileAsString.indexOf("public abstract class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private abstract class");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("public interface");
			if (b != -1) {
				index = b;
			}
			
			b = fileAsString.indexOf("public final interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("public final static interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("public static final interface");
			if (b != -1 && b < index) {
				index = b;
			}

			b = fileAsString.indexOf("private interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private final interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private final static interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private static final interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			
			b = fileAsString.indexOf("public abstract interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private abstract interface");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("public enum");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private enum");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("public final enum");
			if (b != -1 && b < index) {
				index = b;
			}
			
			b = fileAsString.indexOf("private final enum");
			if (b != -1 && b < index) {
				index = b;
			}
			
	    	String a = fileAsString.substring(0, index).trim();
	    	String header = "\n\n/**\n * DE: \n *\n * EN: \n * \n * \n * Program changes\n * **************************************************************************************\n * Date\t\t\t* Ticket\t* KonzFirm\t* Responsible\t* programmer    *\n * Change\t\t\t\t\t\t\t\t\t\t*\n * **************************************************************************************\n *\n * @author "+System.getProperty("user.name")+"\n * @version 1.0\n *\n */";

	    	if (!a.isEmpty()) {
		    	int y = a.lastIndexOf("import");
		    	if(y > -1) {
		    		a = a.substring(y).trim();
		    	}
		    	y = a.indexOf('\n');
		    	if(y > -1) {
		    		a = a.substring(0, a.indexOf('\n')).trim();
		    	}
		    	fileAsString = fileAsString.replace(a,a + header);
	    	} else {
	    		//MessageDialog.openInformation(shell, "Info","Imports/Package are missing.");
	    		fileAsString = header.trim() + "\n\n" + fileAsString;
	    	}
	    	Tools.writeNoPathCreation(file, fileAsString);
	    	Tools.getCurrentSelectedProject().refreshLocal(IResource.DEPTH_INFINITE, null);
	    } catch (final Exception e) {
	    	MessageDialog.openError(shell, "Fehler", "Fehler!\n"+e.getMessage());
	    }
	}
}