package com.chrisfolger.needsmoredojo.actions;

import com.chrisfolger.needsmoredojo.base.DefineResolver;
import com.chrisfolger.needsmoredojo.conventions.UnusedImportsDetector;
import com.intellij.codeInsight.highlighting.HighlightManager;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class UnusedImportsAction extends AnAction {
    protected boolean deleteMode = false;

    public void actionPerformed(@NotNull final AnActionEvent e)
    {
        final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);

        DefineResolver resolver = new DefineResolver();
        final List<PsiElement> parameters = new ArrayList<PsiElement>();
        final List<PsiElement> defines = new ArrayList<PsiElement>();
        resolver.gatherDefineAndParameters(psiFile, defines, parameters);

        UnusedImportsDetector detector = new UnusedImportsDetector();
        psiFile.accept(detector.getVisitorToRemoveUsedParameters(parameters, defines));

        highlightElement(e.getProject(), parameters.toArray(new PsiElement[0]));
        highlightElement(e.getProject(), defines.toArray(new PsiElement[0]));

        if(this.deleteMode)
        {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {
                @Override
                public void run() {
                    final StringBuilder results = new StringBuilder();
                    List<PsiElement> elementsToDelete = new ArrayList<PsiElement>();

                    for(PsiElement element : parameters)
                    {
                        elementsToDelete.add(element);
                        elementsToDelete.add(element.getNextSibling());
                    }
                    for(PsiElement element : defines)
                    {
                        elementsToDelete.add(element);
                        results.append(element.getText() + ", ");
                        elementsToDelete.add(element.getNextSibling());
                    }

                    for(PsiElement element : elementsToDelete)
                    {
                        try
                        {
                            element.delete();
                        }
                        catch(Exception e)
                        {

                        }
                    }

                    if(elementsToDelete.size() > 0)
                    {
                        Notifications.Bus.notify(new Notification("needsmoredojo", "Deleted Imports", "Deleted imports: " + results.toString(), NotificationType.INFORMATION));
                    }
                }
            });
        }
    }

    private void highlightElement(@NotNull Project project, @NotNull PsiElement[] elements)
    {
        final FileEditorManager editorManager =
                FileEditorManager.getInstance(project);
        final HighlightManager highlightManager =
                HighlightManager.getInstance(project);
        final EditorColorsManager editorColorsManager =
                EditorColorsManager.getInstance();
        final Editor editor = editorManager.getSelectedTextEditor();
        final EditorColorsScheme globalScheme =
                editorColorsManager.getGlobalScheme();
        final TextAttributes textattributes =
                globalScheme.getAttributes(
                        EditorColors.SEARCH_RESULT_ATTRIBUTES);

        highlightManager.addOccurrenceHighlights(
                editor, elements, textattributes, true, null);
        final WindowManager windowManager = WindowManager.getInstance();
        final StatusBar statusBar = windowManager.getStatusBar(project);
        statusBar.setInfo("Press Esc to remove highlighting");
    }
}

@@ -594,32 +594,86 @@
 icationManager;%0A
+import com.intellij.openapi.command.CommandProcessor;%0A
 import com.intel
@@ -2148,32 +2148,199 @@
 Mode)%0A        %7B%0A
+            CommandProcessor.getInstance().executeCommand(psiFile.getProject(), new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A        
             Appl
@@ -2411,32 +2411,40 @@
                 
+        
 @Override%0A      
@@ -2429,32 +2429,40 @@
       @Override%0A
+        
                 
@@ -2493,32 +2493,40 @@
                 
+ 
+       
 final StringBuil
@@ -2556,24 +2556,32 @@
 gBuilder();%0A
+        
             
@@ -2666,32 +2666,40 @@
                 
+ 
+       
 for(PsiElement e
@@ -2731,34 +2731,44 @@
                 
-%7B%0A
+        %7B%0A  
                 
@@ -2767,32 +2767,38 @@
                 
+      
 elementsToDelete
@@ -2804,32 +2804,40 @@
 e.add(element);%0A
+        
                 
@@ -2904,34 +2904,50 @@
                 
+  
-%7D%0A
+      %7D%0A        
                 
@@ -2996,34 +2996,50 @@
                 
+   
-%7B%0A
+     %7B%0A        
                 
@@ -3101,16 +3101,24 @@
         
+   
+     
 results.
@@ -3147,24 +3147,32 @@
 () + %22, %22);%0A
+        
             
@@ -3243,35 +3243,51 @@
                 
+    
-%7D%0A%0A
+    %7D%0A%0A        
                 
@@ -3345,34 +3345,50 @@
                 
+     
-%7B%0A
+   %7B%0A        
                 
@@ -3415,34 +3415,50 @@
                 
+      
-%7B%0A
+  %7B%0A        
                 
@@ -3503,34 +3503,50 @@
                 
+      
-%7D%0A
+  %7D%0A        
                 
@@ -3596,16 +3596,24 @@
         
+        
 %7B%0A%0A     
@@ -3623,34 +3623,49 @@
                 
-%7D%0A
+        %7D%0A       
                 
@@ -3660,35 +3660,44 @@
                 
+ 
 %7D%0A%0A
+        
                 
@@ -3744,34 +3744,50 @@
                 
-%7B%0A
+        %7B%0A        
                 
@@ -3942,24 +3942,32 @@
 ORMATION));%0A
+        
             
@@ -3984,32 +3984,40 @@
                 
+        
 %7D%0A            %7D)
@@ -4018,11 +4018,136 @@
     
+ 
-%7D);
+       %7D);%0A                %7D%0A            %7D,%0A            %22Delete unused AMD imports%22,%0A            %22Delete unused AMD imports%22);%0A
 %0A   

undo works with removing unused imports