package fr.tolc.jahia.intellij.plugin.cnd.quickfixes;

import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.ide.projectView.ProjectView;
import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesFileType;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.indexing.FileBasedIndex;
import fr.tolc.jahia.intellij.plugin.cnd.utils.CndTranslationUtil;
import fr.tolc.jahia.intellij.plugin.cnd.utils.CndUtil;
import fr.tolc.jahia.intellij.plugin.cnd.psi.CndNodeType;
import fr.tolc.jahia.intellij.plugin.cnd.psi.CndProperty;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;

public class CreateNodeTypeTranslationsQuickFix extends BaseIntentionAction {

    private String jahiaWorkFolderPath;
    private String namespace;
    private String nodeTypeName;

    public CreateNodeTypeTranslationsQuickFix(String jahiaWorkFolderPath, String namespace, String nodeTypeName) {
        this.jahiaWorkFolderPath = jahiaWorkFolderPath;
        this.namespace = namespace;
        this.nodeTypeName = nodeTypeName;
    }

    @NotNull
    @Override
    public String getText() {
        return "Add translations in resource bundle";
    }
    
    @Nls
    @NotNull
    @Override
    public String getFamilyName() {
        return "Cnd";
    }

    @Override
    public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
        return true;
    }

    @Override
    public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
        ApplicationManager.getApplication().invokeLater(new Runnable() {
            @Override
            public void run() {
                Collection<VirtualFile> virtualFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, PropertiesFileType.INSTANCE, GlobalSearchScope.allScope(project));
                if (virtualFiles.size() == 1) {
                    VirtualFile virtualFile = virtualFiles.iterator().next();
                    PropertiesFile propertiesFile = (PropertiesFile) PsiManager.getInstance(project).findFile(virtualFile);
                    createNodeTypeTranslations(project, propertiesFile);
                } else {
                    final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFileDescriptor(PropertiesFileType.INSTANCE);
                    descriptor.setRoots(project.getBaseDir());
                    final VirtualFile file = FileChooser.chooseFile(descriptor, project, null);
                    if (file != null) {
                        PropertiesFile propertiesFile = (PropertiesFile) PsiManager.getInstance(project).findFile(file);
                        createNodeTypeTranslations(project, propertiesFile);
                    }
                }
            }
        });
    }

    private void createNodeTypeTranslations(final Project project, final PropertiesFile file) {
        String key = CndTranslationUtil.convertNodeTypeIdentifierToPropertyName(namespace, nodeTypeName);

        ApplicationManager.getApplication().runWriteAction(new Runnable() {
            @Override
            public void run() {
                CommandProcessor.getInstance().executeCommand(project, new Runnable() {
                    @Override
                    public void run() {
                        IProperty nodeTypeProperty = file.addProperty(key, "");
//                        I18nUtil.createProperty(project, fileList, key, "");

                        CndNodeType nodeType = CndUtil.findNodeType(project, namespace, nodeTypeName);
                        if (nodeType != null) {
                            List<CndProperty> cndProperties = nodeType.getPropertyList();
                            for (CndProperty cndProperty : cndProperties) {
                                String propertyKey = CndTranslationUtil.convertNodeTypePropertyNameToPropertyName(namespace, nodeTypeName, cndProperty.getPropertyName());
                                file.addProperty(propertyKey, "");
                            }
                        }

                        VirtualFile propertiesFile = file.getContainingFile().getVirtualFile();

                        //Open file in editor
                        FileEditorManager.getInstance(project).openFile(propertiesFile, false);

                        //Expand folder in Project view
                        ProjectView.getInstance(project).select(null, propertiesFile, false);

                        //Caret at the end of node type property
                        ((Navigatable) nodeTypeProperty.getPsiElement().getLastChild().getNavigationElement()).navigate(true);
                        FileEditorManager.getInstance(project).getSelectedTextEditor().getCaretModel().moveCaretRelatively(1, 0, false, false, false);
                    }
                }, "add translations for node type " + namespace + ":" + nodeTypeName, project);
            }
        });
    }
}

@@ -2534,32 +2534,34 @@
 ic void run() %7B%0A
+//
                 
@@ -2730,24 +2730,26 @@
 (project));%0A
+//
             
@@ -2780,24 +2780,26 @@
 e() == 1) %7B%0A
+//
             
@@ -2860,24 +2860,26 @@
 r().next();%0A
+//
             
@@ -2986,24 +2986,26 @@
 rtualFile);%0A
+//
             
@@ -3057,32 +3057,34 @@
 ropertiesFile);%0A
+//
                 
@@ -3339,21 +3339,24 @@
 tualFile
+%5B%5D
  file
+s
  = FileC
@@ -3372,16 +3372,17 @@
 ooseFile
+s
 (descrip
@@ -3426,24 +3426,36 @@
     
-if (file != null
+for (VirtualFile file: files
 ) %7B%0A
@@ -3648,24 +3648,24 @@
 rtiesFile);%0A
+
             
@@ -3674,34 +3674,16 @@
       %7D%0A
-                %7D%0A
         

Create translations quickfix chooseFile to chooseFiles