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 RemoveGenerateReflectionGetSet 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 (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, "Error", "Error " + e.getMessage());
			}
		}
		return null;
	}

	private void createOutput(final Shell shell, final Object firstElement) {
		try {

			String file = ((ICompilationUnit) firstElement).getResource().getRawLocation().toString();

			String fileAsString = Tools.read(file);

			fileAsString = fileAsString.replace("\n\tpublic void set(String fieldname, Object value) {\t\t\n\t\t\ttry {\n\t\t\t\tif (value != null) {\n\t\t\t\t\t\n\t\t\t\t\tif (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.String\")  ) {\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, value);\t\n\t\t\t\t\t\t\n\t\t\t\t\t} else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.Long\")  ) {\n\t\t\t\t\t\t\n\t\t\t\t\t\tif (value == null)\n\t\t\t\t\t\t\tvalue = 0;\n\t\t\t\t\t\t\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, Long.parseLong(value.toString()));\t\n\t\t\t\t\t} else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.Integer\")  ) {\n\t\t\t\t\t\tif (value.toString().trim().length() == 0)\n\t\t\t\t\t\t\tvalue = 0;\n\t\t\t\t\t\t\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, Integer.parseInt(value.toString()));\t\n\t\t\t\t\t} else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.lang.Double\")  ) {\n\t\t\t\t\t\tif (value.toString().trim().length() == 0)\n\t\t\t\t\t\t\tvalue = 0.0;\n\t\t\t\t\t\t\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, Double.parseDouble(value.toString()));\t\n\t\t\t\t\t}  else if (this.getClass().getField(fieldname.toLowerCase()).getType().getName().equals(\"java.util.Date\")  ) {\n\t\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthis.getClass().getField(fieldname.toLowerCase()).set(this, value);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t} catch (Exception e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t}\n\t\n\tpublic Object get(String fieldname) {\n\t\tObject result = null;\n\t\t\n\t\ttry {\n\t\t\t\n\t\t\tif (fieldname == null || fieldname.trim().length() == 0)\n\t\t\t\treturn \"\";\n\t\t\t\n\t\t\tresult = CCTools.getValueOf(this, fieldname.toLowerCase());\t\n\t\t\t\n\t\t\tif (result == null)\n\t\t\t\tresult = \"\";\n\t\t\t\n\t\t\t\n\t\t} catch (Exception e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn result;\n\t}\n\t\n\tpublic String getType(String fieldname) {\n\t\tString result = null;\n\t\ttry {\n\t\t\t\n\t\t\tif (fieldname == null || fieldname.trim().length() == 0)\n\t\t\t\treturn \"\";\n\t\t\t\n\t\t\tresult = this.getClass().getField(fieldname.trim().toLowerCase()).getType().getName();\t\n\t\t} catch (Exception e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn result;\n\t}", "");

			//TODO
			//RegEx welches auch gemoddete Versionenen erkennt, z.B.;
			//von
			//public void set(String fieldname, Object value)[...]
			//bis
			//return result;[...]}

			Tools.writeNoPathCreation(file, fileAsString);
			Tools.getCurrentSelectedProject().refreshLocal(IResource.DEPTH_INFINITE, null);
		} catch (Exception e) {
			MessageDialog.openError(shell, "Error", "Error!\n " + e.getMessage());
		}
	}
}
