package ch.deletescape.jterm.config;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;
import java.util.Properties;

import ch.deletescape.jterm.JTerm;
import ch.deletescape.jterm.io.Printer;

public class UserProperties {
  private static final Properties USER_PROPS = new Properties();
  private static final Path PROPERTIES_PATH = Paths.get(JTerm.getHome(), ".jterm/user.properties");

  private UserProperties() {
  }

  public static void init() {
    loadProps();
    setDefaultLocale();
  }

  public static void setProperty(String key, String value) {
    USER_PROPS.setProperty(key, value);
    saveProps();
  }

  public static String getProperty(String key, String defaultValue) {
    return USER_PROPS.getProperty(key, defaultValue);
  }

  public static String getProperty(String key) {
    return USER_PROPS.getProperty(key);
  }

  private static void loadProps() {
    if (Files.exists(PROPERTIES_PATH)) {
      try {
        USER_PROPS.load(Files.newInputStream(PROPERTIES_PATH));
      } catch (IOException e) {
        Printer.err.println(e.toString());
      }
    }
  }

  private static void saveProps() {
    try {
      if (!Files.exists(PROPERTIES_PATH)) {
        Files.createFile(PROPERTIES_PATH);
      }
      USER_PROPS.store(Files.newOutputStream(PROPERTIES_PATH), "JTerm user properties");
    } catch (IOException e) {
      Printer.err.println(e.toString());
    }

  }

  private static void setDefaultLocale() {
    String userLocale = USER_PROPS.getProperty("user.locale");
    if (userLocale != null) {
      Locale.setDefault(Locale.forLanguageTag(userLocale));
    }
    Resources.localeChanged();
  }
}

@@ -396,31 +396,25 @@
 al Path 
-PROPERTIES_PATH
+JTERM_DIR
  = Paths
@@ -446,26 +446,131 @@
 term
-/user.properties%22)
+%22);%0A  private static final Path PROPERTIES_PATH = JTERM_DIR.resolve(%22user.properties%22);%0A  private static boolean firstStart
 ;%0A%0A 
@@ -628,24 +628,42 @@
 id init() %7B%0A
+    firstStart();%0A
     loadProp
@@ -993,24 +993,24 @@
 ring key) %7B%0A
-
     return U
@@ -1038,24 +1038,339 @@
 (key);%0A  %7D%0A%0A
+  public static boolean isFirstStart() %7B%0A    return firstStart;%0A  %7D%0A%0A  private static void firstStart() %7B%0A    if (!Files.exists(JTERM_DIR)) %7B%0A      firstStart = true;%0A      try %7B%0A        Files.createDirectory(JTERM_DIR);%0A      %7D catch (IOException e) %7B%0A        Printer.err.println(e.toString());%0A      %7D%0A    %7D%0A  %7D%0A%0A
   private st

create .jterm directory if it doesn't yet exist and determine if it is first start