package org.buffagon.intellij.catberry.actions;

import com.intellij.ide.IdeView;
import com.intellij.ide.actions.CreateDirectoryOrPackageHandler;
import com.intellij.ide.util.DirectoryChooserUtil;
import com.intellij.json.JsonFileType;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.PsiFileSystemItem;
import icons.CatberryIcons;
import org.buffagon.intellij.catberry.CatberryBundle;
import org.buffagon.intellij.catberry.CatberryConstants;
import org.buffagon.intellij.catberry.CatberryProjectSettingsProvider;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * Action for create new Catberry component.
 *
 * @author Prokofiev Alex
 */
public class CreateCatberryComponentAction extends DumbAwareAction {
  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
    if (view == null)
      return;

    final Project project = e.getData(CommonDataKeys.PROJECT);
    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
    if (directory == null)
      return;

    String name = Messages.showInputDialog(project, CatberryBundle.message("new.cat.component.prompt"),
        CatberryBundle.message("new.cat.component"), CatberryIcons.LOGO_16, "component", null);
    if (name == null)
      return;

    final String path = directory.getVirtualFile().getPath();

    String preset = CatberryProjectSettingsProvider.getInstance(project).getTemplateEngineName();
    if (!createCatberryModuleStructure(path, name, preset))
      return;

    LocalFileSystem.getInstance().refreshWithoutFileWatcher(false);
    PsiDirectory componentDir = directory.findSubdirectory(name);
    if (componentDir == null)
      return;

    PsiFile componentFile = componentDir.findFile(CatberryConstants.CAT_COMPONENT_JSON);
    if (componentFile == null)
      return;

    view.selectElement(componentFile);
  }

  private boolean createCatberryModuleStructure(String path, String name, String template) {
    try {
      Process process = new ProcessBuilder("catberry", "addcomp", "--dest=" + path, "--preset=" + template, name)
          .start();
      return process.waitFor() == 0;
    } catch (IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return false;
  }

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

  private static boolean isEnabled(AnActionEvent e) {
    Project project = e.getData(CommonDataKeys.PROJECT);

    final IdeView ideView = e.getData(LangDataKeys.IDE_VIEW);
    if (project == null || ideView == null)
      return false;

    CatberryProjectSettingsProvider settingsProvider = CatberryProjectSettingsProvider.getInstance(project);
    if (!settingsProvider.isCatberryEnabled())
      return false;

    final PsiDirectory[] directories = ideView.getDirectories();
    return (directories.length == 1 &&
        directories[0].getVirtualFile().getPath().contains(settingsProvider.getComponentsRoot()));
  }
}

@@ -103,137 +103,33 @@
 ide.
-actions.CreateDirectoryOrPackageHandler;%0Aimport com.intellij.ide.util.DirectoryChooserUtil;%0Aimport com.intellij.json.JsonFileType
+util.DirectoryChooserUtil
 ;%0Aim
@@ -298,55 +298,8 @@
 ys;%0A
-import com.intellij.openapi.diagnostic.Logger;%0A
 impo
@@ -396,62 +396,8 @@
 ct;%0A
-import com.intellij.openapi.roots.ProjectRootManager;%0A
 impo
@@ -486,60 +486,8 @@
 em;%0A
-import com.intellij.openapi.vfs.VirtualFileManager;%0A
 impo
@@ -557,91 +557,8 @@
 le;%0A
-import com.intellij.psi.PsiFileFactory;%0Aimport com.intellij.psi.PsiFileSystemItem;%0A
 impo
@@ -825,137 +825,19 @@
 .io.
-BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0Aimport java.util.Arrays
+IOException
 ;%0A%0A/

optimize imports