package org.buffagon.intellij.catberry;

import com.intellij.ide.util.projectWizard.SettingsStep;
import com.intellij.ide.util.projectWizard.WebProjectTemplate;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModifiableModelsProvider;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.vfs.VirtualFile;
import icons.CatberryIcons;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 @author Prokofiev Alex
 */
public class CatberryProjectGenerator extends WebProjectTemplate<Object> {
  @Nls
  @NotNull
  @Override
  public String getName() {
    return CatberryBundle.message("catberry.project.title");
  }

  @Override
  public Icon getIcon() {
    return CatberryIcons.LOGO_16;
  }

  @Override
  public String getDescription() {
    return CatberryBundle.message("catberry.project.description");
  }

  @Override
  public void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir, @NotNull Object data, @NotNull final Module module) {
    ApplicationManager.getApplication().runWriteAction(
        new Runnable() {
          public void run() {
            final ModifiableRootModel modifiableModel = ModifiableModelsProvider.SERVICE.getInstance().getModuleModifiableModel(module);
            String template = CatberryProjectSettingsProvider.getInstance(project).getTemplateEngineName();
            try {
              Process process = new ProcessBuilder().directory(new File(baseDir.getPath())).command(
                  "catberry", "init", "empty-" + template).start();
              process.waitFor();
            } catch (IOException e) {
              e.printStackTrace();
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            ModifiableModelsProvider.SERVICE.getInstance().commitModuleModifiableModel(modifiableModel);
          }
        }
    );
  }

  @NotNull
  @Override
  public GeneratorPeer<Object> createPeer() {
    return new GeneratorPeer<Object>() {
      @NotNull
      @Override
      public JComponent getComponent() {
        return new JPanel();
      }

      @Override
      public void buildUI(@NotNull SettingsStep settingsStep) {

      }

      @NotNull
      @Override
      public Object getSettings() {
        return new Object();
      }

      @Nullable
      @Override
      public ValidationInfo validate() {
        return null;
      }

      @Override
      public boolean isBackgroundJobRunning() {
        return false;
      }

      @Override
      public void addSettingsStateListener(@NotNull SettingsStateListener settingsStateListener) {

      }
    };
  }
}

@@ -210,24 +210,71 @@
 ionManager;%0A
+import com.intellij.openapi.diagnostic.Logger;%0A
 import com.i
@@ -734,29 +734,8 @@
 .*;%0A
-import java.io.File;%0A
 impo
@@ -763,16 +763,18 @@
 n;%0A%0A/**%0A
+ *
  @author
@@ -868,16 +868,114 @@
 ject%3E %7B%0A
+  public static final Logger LOG = Logger.getInstance(CatberryProjectGenerator.class.getName());%0A%0A
   @Nls%0A 
@@ -1512,18 +1512,16 @@
 (%0A      
-  
 new Runn
@@ -1525,26 +1525,24 @@
 unnable() %7B%0A
-  
         publ
@@ -1553,26 +1553,24 @@
 oid run() %7B%0A
-  
           fi
@@ -1683,16 +1683,29 @@
 leModel(
+%0A            
 module);
@@ -1705,18 +1705,16 @@
 odule);%0A
-  
         
@@ -1825,18 +1825,14 @@
     
-  
 try %7B%0A
-  
     
@@ -1846,24 +1846,31 @@
  Process
+Builder
  process
  = new P
@@ -1861,16 +1861,23 @@
  process
+Builder
  = new P
@@ -1895,96 +1895,701 @@
 er()
-.directory(new File(baseDir.getPath())).command(%0A                  %22catberry%22, %22init%22,
+;%0A%0A            final String os = System.getProperty(%22os.name%22).toLowerCase();%0A            if(os.contains(%22win%22)) %7B%0A              processBuilder.command(%22catberry%22, %22init%22, %22--dest=%22 + baseDir.getPath(), %22empty-%22 + template);%0A            %7D else %7B%0A              String env_path = System.getenv(%22PATH%22);%0A              if(!env_path.contains(%22/bin%22))%0A                env_path = %22/bin:%22 + env_path;%0A%0A              if(!env_path.contains(%22/usr/bin%22))%0A                env_path = %22/usr/bin:%22 + env_path;%0A%0A              if(!env_path.contains(%22/usr/local/bin%22))%0A                env_path = %22/usr/local/bin:%22 + env_path;%0A%0A              final String command = %22catberry init --dest=%22 + baseDir.getPath() +
  %22
+ 
 empt
@@ -2602,17 +2602,172 @@
 template
-)
+;%0A              processBuilder.command(%22sh%22, %22-c%22, %22export PATH=%22 + env_path + %22&& %22 + command);%0A            %7D%0A%0A            Process process = processBuilder
 .start()
@@ -2776,26 +2776,24 @@
             
- 
- 
 process.wait
@@ -2795,26 +2795,24 @@
 .waitFor();%0A
-  
           %7D 
@@ -2851,33 +2851,33 @@
     
-  e.printStackTrace(
+LOG.error(e.toString()
 );%0A
-  
     
@@ -2933,28 +2933,30 @@
     
-  e.printStackTrace(
+LOG.error(e.toString()
 );%0A 
@@ -2960,30 +2960,26 @@
 ;%0A          
-  %7D%0A  
+%7D%0A
           Mo
@@ -3073,30 +3073,26 @@
 l);%0A        
-  %7D%0A  
+%7D%0A
       %7D%0A    

fix mac os x call catberry cli