package com.jetbrains.python.sdk;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.SystemProperties;

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

/**
 * User : catherine
 */
public class VirtualEnvSdkFlavor extends CPythonSdkFlavor {
  private VirtualEnvSdkFlavor() {
  }

  private final static String[] NAMES = new String[]{"python", "jython", "pypy", "python.exe", "jython.bat", "pypy.exe"};

  public static VirtualEnvSdkFlavor INSTANCE = new VirtualEnvSdkFlavor();

  @Override
  public Collection<String> suggestHomePaths() {
    final Project project = PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext());
    List<String> candidates = new ArrayList<String>();
    if (project != null) {
      VirtualFile rootDir = project.getBaseDir();
      if (rootDir != null)
        candidates.addAll(findInDirectory(rootDir));
    }
    VirtualFile userHome = LocalFileSystem.getInstance().findFileByPath(SystemProperties.getUserHome().replace('\\','/'));
    if (userHome != null) {
      VirtualFile child = userHome.findChild(".virtualenvs");
      if (child != null) {
        candidates.addAll(findInDirectory(child));
      }
    }
    return candidates;
  }

  private Collection<String> findInDirectory(VirtualFile rootDir) {
    List<String> candidates = new ArrayList<String>();
    if (rootDir != null) {
      VirtualFile[] suspects = rootDir.getChildren();
      for (VirtualFile child : suspects) {
        if (child.isDirectory()) {
          final String childName = child.getName();
          if (childName.equals("bin") || childName.equals("Scripts"))
            candidates.addAll(findInterpreter(child));
          else
            candidates.addAll(findInDirectory(child));
        }
      }
    }
    return candidates;
  }

  private Collection<String> findInterpreter(VirtualFile dir) {
    List<String> candidates = new ArrayList<String>();
    for (VirtualFile child : dir.getChildren()) {
      if (!child.isDirectory()) {
        final String childName = child.getName();
        for (String name : NAMES) {
          if (childName.startsWith(name)) {
            if (!childName.endsWith("-config")) candidates.add(child.getPath());
            break;
          }
        }
      }
    }
    return candidates;
  }
}

@@ -266,51 +266,8 @@
 ile;
-%0Aimport com.intellij.util.SystemProperties;
 %0A%0Aim
@@ -1051,24 +1051,104 @@
 ir));%0A    %7D%0A
+    String path = System.getenv().get(%22WORKON_HOME%22);%0A    if (path != null) %7B%0A  
     VirtualF
@@ -1151,20 +1151,22 @@
 ualFile 
-user
+workon
 Home = L
@@ -1213,38 +1213,12 @@
 ath(
-SystemProperties.getUserHome()
+path
 .rep
@@ -1242,109 +1242,24 @@
     
+  
 if (
-userHome != null) %7B%0A      VirtualFile child = userHome.findChild(%22.virtualenvs%22);%0A      if (child
+workonHome
  != 
@@ -1300,37 +1300,42 @@
 findInDirectory(
-child
+workonHome
 ));%0A      %7D%0A    

use of WORKON_HOME