package com.jetbrains.ther.run.configuration;

import com.intellij.execution.Location;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.actions.RunConfigurationProducer;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.LightVirtualFile;
import com.jetbrains.ther.TheRFileType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class TheRRunConfigurationProducer extends RunConfigurationProducer<TheRRunConfiguration> {

  public TheRRunConfigurationProducer() {
    super(TheRRunConfigurationType.getInstance().getMainFactory());
  }

  @Override
  protected boolean setupConfigurationFromContext(@NotNull final TheRRunConfiguration configuration,
                                                  @NotNull final ConfigurationContext context,
                                                  @Nullable final Ref<PsiElement> sourceElement) {
    final VirtualFile scriptVirtualFile = getScriptVirtualFile(context);

    if (scriptVirtualFile == null) {
      return false;
    }

    configuration.setName(TheRRunConfigurationSuggests.suggestedName(configuration));
    configuration.setScriptPath(scriptVirtualFile.getPath());
    TheRRunConfigurationSuggests.setSuggestedWorkingDirectoryPathIfNotSpecified(configuration);

    return true;
  }

  @Override
  public boolean isConfigurationFromContext(@NotNull final TheRRunConfiguration configuration,
                                            @NotNull final ConfigurationContext context) {
    final VirtualFile scriptVirtualFile = getScriptVirtualFile(context);

    if (scriptVirtualFile == null) {
      return false;
    }

    final String configurationScriptPath = configuration.getScriptPath();
    final String configurationWorkingDirectoryPath = configuration.getWorkingDirectoryPath();

    final String contextScriptPath = scriptVirtualFile.getPath();
    final String contextWorkingDirectoryPath = TheRRunConfigurationSuggests.suggestedWorkingDirectoryPath(configuration);

    return configurationScriptPath.equals(contextScriptPath) && configurationWorkingDirectoryPath.equals(contextWorkingDirectoryPath);
  }

  @Nullable
  private static VirtualFile getScriptVirtualFile(@NotNull final ConfigurationContext context) {
    final Location location = context.getLocation();
    if (location == null) return null;

    final PsiFile psiFile = getRunnablePsiFile(location);
    if (psiFile == null) return null;

    return getPhysicalVirtualFile(psiFile);
  }

  @Nullable
  private static PsiFile getRunnablePsiFile(@NotNull final Location location) {
    final PsiFile result = location.getPsiElement().getContainingFile();

    if (result == null || result.getFileType() != TheRFileType.INSTANCE) return null;

    return result;
  }

  @Nullable
  private static VirtualFile getPhysicalVirtualFile(@NotNull final PsiFile psiFile) {
    final VirtualFile result = psiFile.getVirtualFile();

    if (result == null || result instanceof LightVirtualFile) return null;

    return result;
  }
}

@@ -1215,21 +1215,62 @@
 tion.set
-Name(
+ScriptPath(scriptVirtualFile.getPath());%0A%0A    
 TheRRunC
@@ -1291,24 +1291,27 @@
 ggests.s
+etS
 uggested
 Name(con
@@ -1306,12 +1306,42 @@
 sted
-Name
+WorkingDirectoryPathIfNotSpecified
 (con
@@ -1351,17 +1351,16 @@
 uration)
-)
 ;%0A    co
@@ -1378,53 +1378,13 @@
 .set
-ScriptPath(scriptVirtualFile.getPath());%0A    
+Name(
 TheR
@@ -1405,35 +1405,32 @@
 onSuggests.s
-etS
 uggested
 WorkingDirec
@@ -1421,42 +1421,12 @@
 sted
-WorkingDirectoryPathIfNotSpecified
+Name
 (con
@@ -1428,32 +1428,33 @@
 e(configuration)
+)
 ;%0A%0A    return tr

fix run config producer: all suggests should be applied in the end