package com.chrisfolger.needsmoredojo.intellij.actions;

import com.chrisfolger.needsmoredojo.core.amd.ImportCreator;
import com.chrisfolger.needsmoredojo.core.settings.DojoSettings;
import com.chrisfolger.needsmoredojo.core.util.PsiFileUtil;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;

public class AddNewImportAction extends JavaScriptAction
{
    @Override
    public void actionPerformed(AnActionEvent e)
    {
        final PsiFile psiFile = PsiFileUtil.getPsiFileInCurrentEditor(e.getProject());

        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        String initialChoice = "";

        if(editor != null)
        {
            PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
            initialChoice = new ImportCreator().getSuggestedImport(element);
        }

        String importModule = Messages.showInputDialog("Enter the module name", "Add new AMD import", null, initialChoice, null);

        if(importModule == null)
        {
            return;
        }

        DojoSettings settingsService = ServiceManager.getService(psiFile.getProject(), DojoSettings.class);
        String[] choices = new ImportCreator().getPossibleDojoImports(psiFile, importModule, settingsService.isPreferRelativeImports());
        // there will be always one choice (the original module)
        if(choices.length > 0)
        {
            importModule = Messages.showEditableChooseDialog("", "Add new AMD Import", null, choices, choices[0], null);
        }

        if(importModule == null)
        {
            return;
        }

        final String importedModule = importModule;

        if(importedModule.equals(""))
        {
            Notifications.Bus.notify(new Notification("needsmoredojo", "Add new Import", "Invalid module", NotificationType.INFORMATION));
            return;
        }

        CommandProcessor.getInstance().executeCommand(psiFile.getProject(), new Runnable() {
            @Override
            public void run() {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {
                    @Override
                    public void run() {
                        new ImportCreator().addImport(psiFile, importedModule);
                    }
                });
            }
        },
        "Add new AMD import",
        "Add new AMD import");
    }
}
@@ -1341,32 +1341,373 @@
 nt);%0A        %7D%0A%0A
+        String warning = %22%22;%0A%0A        String projectSources = ServiceManager.getService(e.getProject(), DojoSettings.class).getProjectSourcesDirectory();%0A        if(projectSources == null %7C%7C projectSources.equals(%22%22))%0A        %7B%0A            warning += %22%5Cn*For best results, configure your project sources in the settings dialog*%22;%0A        %7D%0A%0A
         String i
@@ -1768,16 +1768,26 @@
 le name%22
+ + warning
 , %22Add n

add warning message to add new import popup if project sources aren't defined. closes #80