package eg.projects;

import java.io.File;
import java.io.IOException;

import java.awt.EventQueue;

//--Eadgyth--//
import eg.DisplaySetter;
import eg.console.*;
import eg.javatools.*;
import eg.utils.JOptions;
import eg.utils.FileUtils;
import eg.ui.filetree.FileTree;

/**
 * Represents a programming project in Java.
 */
public final class JavaActions extends ProjectConfig
      implements ProjectActions {

   private final static String F_SEP = File.separator;

   private final DisplaySetter displSet;
   private final Compile comp;
   private final CreateJar jar;
   private final ProcessStarter proc;
   private final ConsolePanel consPnl;
   private final FileTree fileTree;

   private String startCommand = "";

   JavaActions(DisplaySetter displSet, ProcessStarter proc,
         ConsolePanel consPnl, FileTree fileTree) {

      super(".java");
      this.displSet = displSet;
      this.proc = proc;
      this.consPnl = consPnl;
      this.fileTree = fileTree;
      comp = new Compile(consPnl);
      jar = new CreateJar(consPnl);
   }
   
   /**
    * Creates an adapted {@link SettingsWin}.
    */
   @Override
   public void createSettingsWin() {
      SettingsWin setWin = SettingsWin.adaptableWindow("Name of main class");
      setWin.addModuleOption("Package containing the main class")
            .addSourceDirOption()
            .addExecDirOption()
            .addArgsOption()
            .addBuildOption("jar file")
            .setupWindow();
       setSettingsWin(setWin);
   }
   
   @Override
   public boolean configureProject(String dir) {
      boolean success = super.configureProject(dir);
      if (success) {
         setStartCommand();
      }
      return success;
   }

   @Override
   public boolean retrieveProject(String dir) {
      boolean success = super.retrieveProject(dir);
      if (success) {
         setStartCommand();
      }
      return success;
   }
   
   @Override                                                                        
   public void compile() {      
      consPnl.setText("<<Compile " + getProjectName() + ">>\n");
      EventQueue.invokeLater(() -> {
         if (proc.isProcessEnded()) {    
            comp.compile(getProjectPath(), getExecutableDirName(),
                  getSourceDirName());            
            consPnl.setCaret(0);
            if (!displSet.isConsoleSelected()) {
               if (!comp.success()) {
                  int result = JOptions.confirmYesNo(
                        "Compilation of '"
                        + getProjectName() + "' failed.\n"
                        + comp.getMessage() + "."
                        + "\nOpen console window to view messages?");
                  if (result == 0) {
                     displSet.setShowConsoleState(true);
                  }
               }
               else {
                  JOptions.infoMessage(
                        "Successfully compiled '"
                        + getProjectName() + "'.");
               }
            }
         }
      });
   }

   /**
    * Runs the Java program in the console panel
    */
   @Override
   public void runProject() {
      if (!mainClassFileExists()) {
         return;
      }
      if (!displSet.isConsoleSelected()) {
         displSet.setShowConsoleState(true);
      }
      proc.startProcess(startCommand);
   }

   /**
    * Creates a jar file
    */
   @Override
   public void build() {
      if (!mainClassFileExists()) {
         return;
      }
      String jarName = getBuildName();
      if (jarName.length() == 0) {
         jarName = getMainFile();
      }
      boolean existed = jarFileExists(jarName);
      try {
         consPnl.setText("");
         jar.createJar(getProjectPath(), getMainFile(),
               getModuleName(), getExecutableDirName(), jarName);
         if (!existed) {
            boolean exists = false;
            while (!exists) {
               try {
                  Thread.sleep(200);
               }
               catch (InterruptedException e) {
               }
               exists = jarFileExists(jarName);
            }
            fileTree.updateTree();
         }
         consPnl.appendText("<<Saved jar file named " + jarName + ">>\n");
         JOptions.infoMessage("Saved jar file named " + jarName);
      }
      catch (IOException e) {
         FileUtils.logStack(e);
      }        
   }

   private boolean mainClassFileExists() {
      boolean exists = mainExecFileExists(".class");
      if (!exists) {
         JOptions.warnMessage("A compiled main class file could not be found");
      }
      return exists;
   }

   private boolean jarFileExists(String jarName) {
      String execDir = getProjectPath() + F_SEP + getExecutableDirName();
      return new File(execDir + F_SEP + jarName + ".jar").exists();
   }
   
   private void setStartCommand() {
      String main = getMainFile();
      if (getArgs().length() > 0) {
         main += " " + getArgs();
      }
      if (getExecutableDirName().length() == 0 && getModuleName().length() == 0 ) {
         startCommand = "java " + main;
      }
      else if (getExecutableDirName().length() == 0 && getModuleName().length() > 0 ) {
         startCommand = "java " + getModuleName() + "." + main;
      }
      else if (getExecutableDirName().length() > 0 && getModuleName().length() == 0 ) {
         startCommand = "java -cp " + getExecutableDirName() + " " + main;
      }
      else if (getExecutableDirName().length() > 0 && getModuleName().length() > 0 ) {
         startCommand = "java -cp " + getExecutableDirName() + " " + getModuleName()
               + "." + main;
      }
   }
}

@@ -4870,296 +4870,175 @@
 ring
- main = getMainFile();%0A      if (getArgs().length() %3E 0) %7B%0A         main += %22 %22 + getArgs();%0A      %7D%0A      if (getExecutableDirName().length() == 0 && getModuleName().length() == 0 ) %7B%0A         startCommand = %22java %22 + main;%0A      %7D%0A      else if (getExecutableDirName().length() == 0 && 
+Builder sb = new StringBuilder(%22java %22);%0A      if (getExecutableDirName().length() %3E 0) %7B%0A         sb.append(%22-cp %22 + getExecutableDirName() + %22 %22);%0A      %7D%0A      if (
 getM
@@ -5053,33 +5053,32 @@
 e().length() %3E 0
- 
 ) %7B%0A         sta
@@ -5079,32 +5079,17 @@
    s
-tartCommand = %22java %22 + 
+b.append(
 getM
@@ -5101,31 +5101,25 @@
 Name() + %22.%22
- + main
+)
 ;%0A      %7D%0A  
@@ -5126,69 +5126,54 @@
     
-else if (getExecutableDirName().length() %3E 0 && getModuleName
+String main = getMainFile();%0A      if (getArgs
 ().l
@@ -5184,13 +5184,11 @@
 h() 
-==
+%3E
  0
- 
 ) %7B%0A
@@ -5200,290 +5200,99 @@
     
-startCommand = %22java -cp %22 + getExecutableDirName() + %22 %22 + main;%0A      %7D%0A      else if (getExecutableDirName().length() %3E 0 && getModuleName().length() %3E 0 ) %7B%0A         startCommand = %22java -cp %22 + getExecutableDirName() + %22 %22 + getModuleName()%0A               + %22.%22 + main;%0A      %7D
+main += %22 %22 + getArgs();%0A      %7D%0A      sb.append(main);%0A      startCommand = sb.toString();
 %0A   

clear up setStartCommand method