package org.perfcake.pc4idea.configuration;

import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.actions.RunConfigurationProducer;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiElement;
import org.perfcake.pc4idea.editor.PerfCakeEditorProvider;

/**
 * Created with IntelliJ IDEA.
 * User: Stanislav Kaleta
 * Date: 5.12.2014
 */
public class PCRunConfigurationProducer extends RunConfigurationProducer<PCRunConfiguration> {

    public PCRunConfigurationProducer(){
        super(PCConfigurationType.getInstance());
    }

    @Override
    protected boolean setupConfigurationFromContext(PCRunConfiguration pcRunConfig, ConfigurationContext configContext, Ref<PsiElement> psiElementRef) {
        if ((configContext.getLocation() != null) && (configContext.getLocation().getVirtualFile() != null) &&
                new PerfCakeEditorProvider().accept(configContext.getProject(), configContext.getLocation().getVirtualFile())){
            if (!pcRunConfig.isInitialized()) {
                String fileName = configContext.getLocation().getVirtualFile().getName();
                pcRunConfig.setScenarioName(fileName);
                pcRunConfig.setName(fileName);
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isConfigurationFromContext(PCRunConfiguration pcRunConfig, ConfigurationContext configContext) {
        if ((configContext.getLocation() != null) && (configContext.getLocation().getVirtualFile() != null)){
            String fileName = configContext.getLocation().getVirtualFile().getName();
            return fileName.equals(pcRunConfig.getScenarioName()) && pcRunConfig.isInitialized();
        } else {
            return false;
        }
    }
}

@@ -475,17 +475,16 @@
 tion%3E %7B%0A
-%0A
     publ
@@ -740,36 +740,39 @@
 tRef) %7B%0A        
-if (
+return 
 (configContext.g
@@ -983,342 +983,9 @@
 e())
-)%7B%0A            if (!pcRunConfig.isInitialized()) %7B%0A                String fileName = configContext.getLocation().getVirtualFile().getName();%0A                pcRunConfig.setScenarioName(fileName);%0A                pcRunConfig.setName(fileName);%0A            %7D%0A            return true;%0A        %7D else %7B%0A            return false;%0A        %7D
+;
 %0A   
@@ -1326,150 +1326,98 @@
     
-    return fileName.equals(pcRunConfig.getScenarioName()) && pcRunConfig.isInitialized();%0A        %7D else %7B%0A            return false;%0A        %7D
+%7D%0A        /*TODO if exists such a config - true = hide config setup*/%0A        return true;
 %0A   

Revert "added ability to create default config on scenario click"