package com.poratu.idea.plugins.tomcat.runner;

import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.actions.LazyRunConfigurationProducer;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ConfigurationTypeUtil;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.poratu.idea.plugins.tomcat.conf.TomcatRunConfiguration;
import com.poratu.idea.plugins.tomcat.conf.TomcatRunConfigurationType;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Stream;

public class TomcatRunConfigurationProducer extends LazyRunConfigurationProducer<TomcatRunConfiguration> {


    @Override
    protected boolean setupConfigurationFromContext(@NotNull TomcatRunConfiguration configuration, @NotNull ConfigurationContext context, @NotNull Ref<PsiElement> sourceElement) {

        Module module = context.getModule();
        Optional<VirtualFile> webModule = getWebModule(module);

        boolean isWebModule = webModule.isPresent();
        if (isWebModule) {

            VirtualFile virtualFile = webModule.get();
            configuration.setName(module.getName());
            configuration.setDocBase(virtualFile.getCanonicalPath());
            configuration.setContextPath("/" + module.getName());
            configuration.setModuleName(module.getName());

            ConfigurationFactory configurationFactory = getConfigurationFactory();
            final RunnerAndConfigurationSettings settings =
                    RunManager.getInstance(context.getProject()).createConfiguration(configuration, configurationFactory);

            settings.setName(configuration.getName() + " in " + module.getName());
        }

        return isWebModule;
    }


    @Override
    public boolean isConfigurationFromContext(@NotNull TomcatRunConfiguration configuration, @NotNull ConfigurationContext context) {

        Module module = context.getModule();
        Optional<VirtualFile> webModule = getWebModule(module);


        boolean present = webModule.isPresent();
        return present;
    }

    @NotNull
    @Override
    public ConfigurationFactory getConfigurationFactory() {
        TomcatRunConfigurationType configurationType = ConfigurationTypeUtil.findConfigurationType(TomcatRunConfigurationType.class);
        return configurationType.getConfigurationFactories()[0];
    }

    private Optional<VirtualFile> getWebModule(@NotNull Module module) {
        VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(false);

        Optional<VirtualFile> webmodule = Stream.of(sourceRoots).map(VirtualFile::getParent).distinct().flatMap(f ->
                Stream.of(f.getChildren()).filter(c -> {
                    Path path = Paths.get(c.getCanonicalPath(), "WEB-INF");
                    return path.toFile().exists();
                })).distinct().findFirst();
        return webmodule;
    }


}

@@ -1235,163 +1235,78 @@
     
-Module module = context.getModule();%0A        Optional%3CVirtualFile%3E webModule = getWebModule(module);%0A%0A        boolean isWebModule = webModule.isPresent(
+boolean result = isConfigurationFromContext(configuration, context
 );%0A
+%0A
     
@@ -1317,24 +1317,18 @@
 if (
-isWebModule
+result
 ) %7B%0A
-%0A
     
@@ -1365,21 +1365,93 @@
 e = 
-webModule.get
+context.getLocation().getVirtualFile();%0A            Module module = context.getModule
 ();%0A
@@ -1703,91 +1703,8 @@
 );%0A%0A
-            ConfigurationFactory configurationFactory = getConfigurationFactory();%0A
     
@@ -1859,17 +1859,20 @@
 ration, 
-c
+getC
 onfigura
@@ -1882,20 +1882,21 @@
 nFactory
+()
 );%0A
-%0A
         
@@ -1916,29 +1916,22 @@
 setName(
-configuration
+module
 .getName
@@ -1940,36 +1940,28 @@
  + %22 in 
-%22 + module.getName()
+SmartTomcat%22
 );%0A     
@@ -1961,25 +1961,24 @@
 ;%0A        %7D%0A
-%0A
         retu
@@ -1984,18 +1984,12 @@
 urn 
-isWebModul
+fals
 e;%0A 
@@ -2139,25 +2139,172 @@
  context) %7B%0A
-%0A
+        boolean result = false;%0A%0A        VirtualFile vf = context.getLocation().getVirtualFile();%0A        if (vf != null && vf.isDirectory()) %7B%0A    
         Modu
@@ -2328,32 +2328,36 @@
 xt.getModule();%0A
+    
         Optional
@@ -2404,18 +2404,20 @@
 odule);%0A
-%0A%0A
+    
         
@@ -2424,23 +2424,27 @@
 boolean 
-present
+isWebModule
  = webMo
@@ -2473,21 +2473,271 @@
     
-return presen
+    if (isWebModule) %7B%0A                VirtualFile virtualFile = webModule.get();%0A                if (vf.getCanonicalPath().equals(virtualFile.getCanonicalPath())) %7B%0A                    result = true;%0A                %7D%0A            %7D%0A        %7D%0A        return resul
 t;%0A 

update exclude files and default tomcat docPath and contextPath.