package com.redhat.ceylon.eclipse.code.editor;

import static com.redhat.ceylon.eclipse.util.EditorUtil.getCurrentEditor;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ltk.core.refactoring.DocumentChange;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.text.edits.ReplaceEdit;

import ceylon.tool.converter.java2ceylon.Java8Lexer;
import ceylon.tool.converter.java2ceylon.Java8Parser;
import ceylon.tool.converter.java2ceylon.JavaToCeylonConverter;
import com.redhat.ceylon.eclipse.code.refactor.AbstractHandler;
import com.redhat.ceylon.eclipse.ui.CeylonPlugin;
import com.redhat.ceylon.eclipse.util.EditorUtil;

public class PasteAsCeylonHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		String ceylonCode = "<error>";
		Clipboard cb = new Clipboard(null);
		String javaCode = (String) cb.getContents(TextTransfer.getInstance());

		try {
			ceylonCode = transformJavaToCeylon(javaCode);
		} catch (IOException e) {
			CeylonPlugin.log(IStatus.ERROR, "Could not transform Java code", e);
		}

		insertTextInEditor(ceylonCode);

		return null;
	}

	private String transformJavaToCeylon(String javaCode) throws IOException {
		ANTLRInputStream input = new ANTLRInputStream(javaCode);
		Java8Lexer lexer = new Java8Lexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		Java8Parser parser = new Java8Parser(tokens);
		ParserRuleContext tree = parser.compilationUnit();

		StringWriter out = new StringWriter();
		JavaToCeylonConverter converter = new JavaToCeylonConverter(out, true, true);
		tree.accept(converter);

		String ceylonCode = out.toString();

		if (ceylonCode.equals("")) {
			return javaCode;
		}

		return ceylonCode;
	}

	private void insertTextInEditor(String ceylonCode) {
		CeylonEditor editor = (CeylonEditor) getCurrentEditor();
		IDocument doc = editor.getCeylonSourceViewer().getDocument();

		int offset = 0;

		offset = EditorUtil.getSelection(editor).getOffset();

		DocumentChange change = new DocumentChange("Paste Java as Ceylon", doc);
		change.setEdit(new ReplaceEdit(offset, 0, ceylonCode));

		try {
			change.perform(new NullProgressMonitor());
		} catch (CoreException e) {
			CeylonPlugin.log(IStatus.ERROR, "Could not paste transformed code", e);
		}

		FormatAction.format(editor.getParseController(), doc, EditorUtil.getSelection(editor), false,
				editor.getSelectionProvider());
	}

	@Override
	protected boolean isEnabled(CeylonEditor editor) {
		return true;
	}

}

@@ -120,39 +120,8 @@
 r;%0A%0A
-import java.io.BufferedWriter;%0A
 impo
@@ -140,24 +140,24 @@
 OException;%0A
+
 import java.
@@ -318,58 +318,8 @@
 xt;%0A
-import org.antlr.v4.runtime.tree.ParseTreeWalker;%0A
 impo
@@ -408,32 +408,76 @@
 utionException;%0A
+import org.eclipse.core.resources.IProject;%0A
 import org.eclip
@@ -825,179 +825,8 @@
 t;%0A%0A
-import ceylon.tool.converter.java2ceylon.Java8Lexer;%0Aimport ceylon.tool.converter.java2ceylon.Java8Parser;%0Aimport ceylon.tool.converter.java2ceylon.JavaToCeylonConverter;%0A
 impo
@@ -881,24 +881,78 @@
 actHandler;%0A
+import com.redhat.ceylon.eclipse.core.model.modelJ2C;%0A
 import com.r
@@ -1038,16 +1038,247 @@
 torUtil;
+%0Aimport com.redhat.ceylon.ide.common.model.CeylonIdeConfig;%0A%0Aimport ceylon.tool.converter.java2ceylon.Java8Lexer;%0Aimport ceylon.tool.converter.java2ceylon.Java8Parser;%0Aimport ceylon.tool.converter.java2ceylon.JavaToCeylonConverter;
 %0A%0Apublic
@@ -1564,24 +1564,230 @@
 );%0A%0A%09%09try %7B%0A
+%09%09    IProject project = ((CeylonEditor) getCurrentEditor()).getParseController().getProject();%0A%09%09    CeylonIdeConfig%3CIProject%3E ideConfig = modelJ2C.ceylonModel().getProject(project).getIdeConfiguration();%0A
 %09%09%09ceylonCod
@@ -1816,24 +1816,35 @@
 lon(javaCode
+, ideConfig
 );%0A%09%09%7D catch
@@ -2046,16 +2046,53 @@
 javaCode
+, CeylonIdeConfig%3CIProject%3E ideConfig
 ) throws
@@ -2411,16 +2411,19 @@
 iter();%0A
+%09%09%0A
 %09%09JavaTo
@@ -2485,18 +2485,165 @@
 ut, 
-true, true
+%0A%09%09        ideConfig.getConverterConfig().getTransformGetters(),%0A%09%09        ideConfig.getConverterConfig().getUseVariableInParameters());%0A%09%09%0A%09%09ideConfig.save(
 );%0A%09

Use settings for the code converter.