package ch.deletescape.jterm;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import ch.deletescape.jterm.config.Resources;
import ch.deletescape.jterm.config.UserProperties;
import ch.deletescape.jterm.io.Printer;
import ch.deletescape.jterm.updater.UpdateChecker;

public class JTerm {
  private static final String USER = System.getProperty("user.name");
  private static String home = System.getProperty("user.home");
  private static Path currPath;
  private static boolean isRunning = true;
  private static final Scanner SCANNER = new Scanner(System.in);
  private static final ScriptEngineManager SCRIPT_MANAGER = new ScriptEngineManager();
  private static final ScriptEngine JSENGINE = SCRIPT_MANAGER.getEngineByName("js");
  private static final Logger LOGGER = LogManager.getLogger();

  public static void main(String[] args) throws IOException {
    if (home == null) {
      home = System.getProperty("user.dir");
    }
    CommandUtils.initializeEnv();
    Printer.out.println(Resources.getString("JTerm.BannerLine"));
    if (UserProperties.isFirstStart()) {
      Printer.out.println(Resources.getString("JTerm.FirstTimeUser"), USER);
      LOGGER.info("Firsttime info message shown");
    }
    try {
      if (!UserProperties.getBoolean("jterm.updatecheck.disable")) {
        UpdateChecker.Update update = new UpdateChecker().getLatest();
        if (update.isNewer()) {
          Printer.out.println(Resources.getString("JTerm.UpdateFound"), update.getName(), update.getVersion());
        }
      }
    } catch (IOException e) {
      LOGGER.error(e.toString(), e);
    }
    if (args.length > 0) {
      CommandUtils.evaluateCommand(String.join(" ", args));
    }
    LOGGER.info("Session started");
    while (isRunning) {
      CommandUtils.printInputPromt();
      CommandUtils.evaluateCommand(SCANNER.nextLine());
    }
    SCANNER.close();
  }

  public static Scanner getScanner() {
    return SCANNER;
  }

  public static String getUser() {
    return USER;
  }

  public static String getHome() {
    return home;
  }

  public static Path getCurrPath() {
    if (currPath == null) {
      currPath = Paths.get(home);
    }
    return currPath;
  }

  public static void setCurrPath(Path currPath) {
    JTerm.currPath = currPath;
  }

  public static ScriptEngine getJsEngine() {
    return JSENGINE;
  }

  public static void exit() {
    isRunning = false;
    LOGGER.info("Exiting session");
  }
}

@@ -579,147 +579,8 @@
 %22);%0A
-  private static String home = System.getProperty(%22user.home%22);%0A  private static Path currPath;%0A  private static boolean isRunning = true;%0A
   pr
@@ -640,16 +640,16 @@
 em.in);%0A
+
   privat
@@ -874,16 +874,155 @@
 ogger();
+%0A  private static String home = System.getProperty(%22user.home%22);%0A  private static Path currPath;%0A  private static boolean isRunning = true;
 %0A%0A  publ
@@ -1194,629 +1194,23 @@
     
-Printer.out.println(Resources.getString(%22JTerm.BannerLine%22));%0A    if (UserProperties.isFirstStart()) %7B%0A      Printer.out.println(Resources.getString(%22JTerm.FirstTimeUser%22), USER);%0A      LOGGER.info(%22Firsttime info message shown%22);%0A    %7D%0A    try %7B%0A      if (!UserProperties.getBoolean(%22jterm.updatecheck.disable%22)) %7B%0A        UpdateChecker.Update update = new UpdateChecker().getLatest();%0A        if (update.isNewer()) %7B%0A          Printer.out.println(Resources.getString(%22JTerm.UpdateFound%22), update.getName(), update.getVersion());%0A        %7D%0A      %7D%0A    %7D catch (IOException e) %7B%0A      LOGGER.error(e.toString(), e);%0A    %7D
+printBanners();
 %0A   
@@ -1343,127 +1343,23 @@
     
-while (isRunning) %7B%0A      CommandUtils.printInputPromt();%0A      CommandUtils.evaluateCommand(SCANNER.nextLine());%0A    %7D
+terminalLoop();
 %0A   
@@ -1900,16 +1900,16 @@
  false;%0A
-
     LOGG
@@ -1939,11 +1939,849 @@
 n%22);%0A  %7D
+%0A%0A  private static void printBanners() %7B%0A    Printer.out.println(Resources.getString(%22JTerm.BannerLine%22));%0A    if (UserProperties.isFirstStart()) %7B%0A      Printer.out.println(Resources.getString(%22JTerm.FirstTimeUser%22), USER);%0A      LOGGER.info(%22Firsttime info message shown%22);%0A    %7D%0A    try %7B%0A      if (!UserProperties.getBoolean(%22jterm.updatecheck.disable%22)) %7B%0A        UpdateChecker.Update update = new UpdateChecker().getLatest();%0A        if (update.isNewer()) %7B%0A          Printer.out.println(Resources.getString(%22JTerm.UpdateFound%22), update.getName(), update.getVersion());%0A        %7D%0A      %7D%0A    %7D catch (IOException e) %7B%0A      LOGGER.error(e.toString(), e);%0A    %7D%0A  %7D%0A%0A  private static void terminalLoop() %7B%0A    while (isRunning) %7B%0A      CommandUtils.printInputPromt();%0A      CommandUtils.evaluateCommand(SCANNER.nextLine());%0A    %7D%0A  %7D
 %0A%7D%0A

refactor JTerm