package com.jetbrains.python.actions;

import com.intellij.ide.IdeBundle;
import com.intellij.ide.IdeView;
import com.intellij.ide.actions.CreateDirectoryOrPackageHandler;
import com.intellij.ide.util.DirectoryChooserUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.jetbrains.python.PyNames;

/**
 * @author yole
 */
public class CreatePackageAction extends DumbAwareAction {
  @Override
  public void actionPerformed(AnActionEvent e) {
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
    final Project project = e.getData(PlatformDataKeys.PROJECT);
    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);

    if (directory == null) return;
    new WriteCommandAction.Simple(project, "Create Package") {
      @Override
      protected void run() throws Throwable {
        CreateDirectoryOrPackageHandler validator = new CreateDirectoryOrPackageHandler(project, directory, false, ".");
        Messages.showInputDialog(project, IdeBundle.message("prompt.enter.new.package.name"),
                                          IdeBundle.message("title.new.package"),
                                          Messages.getQuestionIcon(), "", validator);
        final PsiDirectory result = validator.getCreatedElement();
        if (result != null && view != null) {
          createInitPyInHierarchy(result, directory);
          view.selectElement(result);
        }
      }
    }.execute();
  }

  private static void createInitPyInHierarchy(PsiDirectory created, PsiDirectory ancestor) {
    do {
      createInitPy(created);
      created = created.getParent();
    } while(created != null && created != ancestor);
  }

  private static void createInitPy(PsiDirectory directory) {
    final PsiFile file = PsiFileFactory.getInstance(directory.getProject()).createFileFromText(PyNames.INIT_DOT_PY, "");
    directory.add(file);
  }

  @Override
  public void update(AnActionEvent e) {
    boolean enabled = isEnabled(e);
    e.getPresentation().setVisible(enabled);
    e.getPresentation().setEnabled(enabled);
  }

  private static boolean isEnabled(AnActionEvent e) {
    Project project = e.getData(PlatformDataKeys.PROJECT);
    final IdeView ideView = e.getData(LangDataKeys.IDE_VIEW);
    if (project == null || ideView == null) {
      return false;
    }
    final PsiDirectory[] directories = ideView.getDirectories();
    if (directories.length == 0) {
      return false;
    }
    for (PsiDirectory directory : directories) {
      if (directory.findFile(PyNames.INIT_DOT_PY) != null) {
        return true;
      }
    }
    return false;
  }
}

@@ -390,64 +390,8 @@
 ys;%0A
-import com.intellij.openapi.command.WriteCommandAction;%0A
 impo
@@ -1070,37 +1070,87 @@
     
-new WriteCommandAction.Simp
+CreateDirectoryOrPackageHandler validator = new CreateDirectoryOrPackageHand
 le
+r
 (pro
@@ -1159,23 +1159,28 @@
 ct, 
-%22Create Package
+directory, false, %22.
 %22) %7B
@@ -1221,30 +1221,44 @@
 oid 
-run() throws Throwable
+createDirectories(String subDirName)
  %7B%0A 
@@ -1260,25 +1260,31 @@
 ) %7B%0A        
-C
+super.c
 reateDirecto
@@ -1288,111 +1288,105 @@
 ctor
-yOrPackageHandler validator = new CreateDirectoryOrPackageHandler(project, directory, false, %22.%22);%0A    
+ies(subDirName);%0A        createInitPyInHierarchy(getCreatedElement(), directory);%0A      %7D%0A    %7D;%0A
     
@@ -1509,20 +1509,16 @@
         
- 
-   
 IdeBundl
@@ -1545,28 +1545,24 @@
 .package%22),%0A
-    
             
@@ -1631,20 +1631,16 @@
 dator);%0A
-    
     fina
@@ -1694,20 +1694,16 @@
 ment();%0A
-    
     if (
@@ -1740,66 +1740,8 @@
 ) %7B%0A
-          createInitPyInHierarchy(result, directory);%0A    
     
@@ -1778,38 +1778,9 @@
     
-    %7D%0A      %7D%0A    %7D.execute();
+%7D
 %0A  %7D

refactor Create Package to make sure undo works correctly (PY-1829)