package de.peeeq.wurstio.languageserver.requests;

import com.google.common.collect.ImmutableMap;
import de.peeeq.wurstio.languageserver.ModelManager;
import de.peeeq.wurstio.languageserver.WFile;
import de.peeeq.wurstio.languageserver.WurstLanguageServer;
import de.peeeq.wurstscript.ast.CompilationUnit;
import de.peeeq.wurstscript.ast.WImport;
import de.peeeq.wurstscript.ast.WPackage;
import de.peeeq.wurstscript.parser.WPos;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.services.LanguageClient;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class PerformCodeActionRequest extends UserRequest<Object> {
    public static final String IMPORT_PACKAGE = "IMPORT_PACKAGE";
    private final WurstLanguageServer server;
    private final ExecuteCommandParams params;
    private final List<Object> args;

    public PerformCodeActionRequest(WurstLanguageServer server, ExecuteCommandParams params) {
        this.server = server;
        this.params = params;
        this.args = params.getArguments();
    }

    @Override
    public Object execute(ModelManager modelManager) {
        System.err.println("code action " + args);
        if (args.isEmpty()) {
            throw new RuntimeException("No arguments given.");
        }
        Map<?, ?> action = (Map<?,?>) args.get(0);
        switch ((String) action.get("type")) {
            case IMPORT_PACKAGE:
                return addImport(modelManager, (String) action.get("uriString"), (String) action.get("import"));

        }
        throw new RuntimeException("Unhandled action: " + action);
    }

    private Object addImport(ModelManager modelManager, String fileUri, String importName) {
        WFile file = WFile.create(fileUri);
        CompilationUnit cu = modelManager.getCompilationUnit(file);

        Position pos = new Position(0,0);

        if (!cu.getPackages().isEmpty()) {
            WPackage p = cu.getPackages().get(0);
            int line = p.getNameId().getSource().getLine() + 1;
            for (WImport imp : p.getImports()) {
                line = Math.max(line, imp.getPackagenameId().getSource().getLine());
            }
            pos.setLine(line);
        }

        Range range = new Range(pos, pos);
        TextEdit textEdit = new TextEdit(range, "import " + importName + "\n");
        List<TextEdit> textEdits = Collections.singletonList(textEdit);
        return applyTextEdits(file, textEdits);
    }

    private Object applyTextEdits(WFile file, List<TextEdit> textEdits) {
        LanguageClient languageClient = server.getLanguageClient();
        int version = server.worker().getBufferManager().getTextDocumentVersion(file);
        VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(version);
        textDocument.setUri(file.getUriString());

        TextDocumentEdit change = new TextDocumentEdit(textDocument, textEdits);
        List<TextDocumentEdit> documentChanges = Collections.singletonList(change);
        languageClient.applyEdit(new ApplyWorkspaceEditParams(new WorkspaceEdit(documentChanges)));
        return "ok";
    }

    public static Map<String, String> importPackageAction(String uriString, String imp) {
        return ImmutableMap.of(
                "type", IMPORT_PACKAGE,
                "uriString", uriString,
                "import", imp
        );
    }
}

@@ -66,35 +66,23 @@
 gle.
-common.collect.ImmutableMap
+gson.JsonObject
 ;%0Aim
@@ -234,24 +234,61 @@
 uageServer;%0A
+import de.peeeq.wurstscript.WLogger;%0A
 import de.pe
@@ -411,49 +411,8 @@
 ge;%0A
-import de.peeeq.wurstscript.parser.WPos;%0A
 impo
@@ -542,30 +542,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0A/*
@@ -1102,26 +1102,20 @@
     
-System.err.println
+WLogger.info
 (%22co
@@ -1250,17 +1250,18 @@
     
-Map%3C?, ?%3E
+JsonObject
  act
@@ -1271,16 +1271,18 @@
  = (
-Map%3C?,?%3E
+JsonObject
 ) ar
@@ -1308,25 +1308,16 @@
 switch (
-(String) 
 action.g
@@ -1326,16 +1326,30 @@
 (%22type%22)
+.getAsString()
 ) %7B%0A    
@@ -1423,25 +1423,16 @@
 Manager,
- (String)
  action.
@@ -1451,18 +1451,23 @@
 ng%22)
-, (
+.getAs
 String
-)
+(),
  act
@@ -1483,16 +1483,30 @@
 import%22)
+.getAsString()
 );%0A%0A    
@@ -1829,16 +1829,17 @@
 ition(0,
+ 
 0);%0A%0A   
@@ -3128,27 +3128,18 @@
 tic 
-Map%3CString, String%3E
+JsonObject
  imp
@@ -3199,48 +3199,81 @@
     
-return ImmutableMap.of(%0A                
+JsonObject jsonObject = new JsonObject();%0A        jsonObject.addProperty(
 %22typ
@@ -3290,17 +3290,18 @@
 _PACKAGE
-,
+);
 %0A       
@@ -3301,24 +3301,39 @@
         
-        
+jsonObject.addProperty(
 %22uriStri
@@ -3346,17 +3346,18 @@
 riString
-,
+);
 %0A       
@@ -3357,24 +3357,39 @@
         
-        
+jsonObject.addProperty(
 %22import%22
@@ -3393,16 +3393,18 @@
 rt%22, imp
+);
 %0A       
@@ -3404,17 +3404,33 @@
         
-)
+return jsonObject
 ;%0A    %7D%0A

fix import quickfix