package com.cedricziel.idea.typo3.action;

import com.cedricziel.idea.typo3.TYPO3CMSIcons;
import com.cedricziel.idea.typo3.util.ExtensionFileGenerationUtil;
import com.cedricziel.idea.typo3.util.ExtensionUtility;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.RunResult;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class GenerateViewHelperAction extends NewExtensionFileAction {

    public GenerateViewHelperAction() {

        super("ViewHelper", "Generate a Fluid ViewHelper", TYPO3CMSIcons.TYPO3_ICON);
    }

    @Override
    protected void write(@NotNull Project project, @NotNull PsiDirectory extensionRootDirectory, @NotNull String className) {
        if (!className.endsWith("ViewHelper")) {
            className += "ViewHelper";
        }

        final String finalClassName = className;
        RunResult<PsiElement> elementRunResult = new WriteCommandAction<PsiElement>(project) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                PsiElement extensionFile;
                Map<String, String> context = new HashMap<>();

                String calculatedNamespace = ExtensionUtility.findDefaultNamespace(extensionRootDirectory);
                if (calculatedNamespace == null) {
                    result.setResult(null);
                    return;
                }

                calculatedNamespace += "ViewHelpers";

                context.put("namespace", calculatedNamespace);
                context.put("className", finalClassName);

                extensionFile = ExtensionFileGenerationUtil.fromTemplate(
                        "extension_file/ViewHelper.php",
                        "Classes/ViewHelpers",
                        finalClassName + ".php",
                        extensionRootDirectory,
                        context,
                        project
                );

                if (extensionFile != null) {
                    result.setResult(extensionFile);
                }
            }

        }.execute();

        if (elementRunResult.getResultObject() != null) {
            new OpenFileDescriptor(project, elementRunResult.getResultObject().getContainingFile().getVirtualFile(), 0).navigate(true);
        } else {
            Messages.showErrorDialog("Cannot create extension file", "Error");
        }
    }
}

@@ -207,16 +207,68 @@
 tility;%0A
+import com.cedricziel.idea.typo3.util.TYPO3Utility;%0A
 import c
@@ -633,16 +633,70 @@
 lement;%0A
+import com.intellij.util.IncorrectOperationException;%0A
 import o
@@ -1994,24 +1994,757 @@
 lassName);%0A%0A
+                String majorVersion = null;%0A                if (TYPO3Utility.getTYPO3Version(project) != null && TYPO3Utility.isMajorMinorCmsVersion(project, %227.6%22)) %7B%0A                    majorVersion = %227%22;%0A                %7D else if (TYPO3Utility.getTYPO3Version(project) != null && TYPO3Utility.isMajorMinorCmsVersion(project, %228.7%22)) %7B%0A                    majorVersion = %228%22;%0A                %7D else if (TYPO3Utility.getTYPO3Version(project) != null && TYPO3Utility.getTYPO3Version(project).startsWith(%229.%22)) %7B%0A                    majorVersion = %229%22;%0A                %7D%0A%0A                if (majorVersion == null) %7B%0A                    result.setResult(null);%0A                    return;%0A                %7D%0A%0A                try %7B%0A    
             
@@ -2829,16 +2829,20 @@
         
+    
 %22extensi
@@ -2849,16 +2849,37 @@
 on_file/
+%22 + majorVersion + %22/
 ViewHelp
@@ -2883,24 +2883,28 @@
 elper.php%22,%0A
+    
             
@@ -2958,24 +2958,28 @@
             
+    
 finalClassNa
@@ -2987,24 +2987,28 @@
 e + %22.php%22,%0A
+    
             
@@ -3063,24 +3063,28 @@
             
+    
 context,%0A   
@@ -3080,16 +3080,20 @@
 ontext,%0A
+    
         
@@ -3132,16 +3132,20 @@
         
+    
 );%0A%0A    
@@ -3152,24 +3152,28 @@
             
+  
+  
 if (extensio
@@ -3181,32 +3181,36 @@
 File != null) %7B%0A
+    
                 
@@ -3258,26 +3258,34 @@
             
-%7D%0A
+    %7D%0A    
             
@@ -3277,33 +3277,147 @@
                %7D
-%0A
+ catch (IncorrectOperationException e) %7B%0A                    // file already exists%0A                %7D%0A            %7D
 %0A        %7D.execu

Create TYPO3 Version dependant ViewHelper