package net.minecraft.launcher;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.mc.main.Util;
import org.yaml.snakeyaml.Yaml;

public final class StorageManager {
   private String confVersion = "2";
   private String language = "en";
   private String lastused = "";
   private String autoName = "";
   private Boolean autoLogin = false;
   private Boolean syncLanguage = false;
   private Boolean useDarkTheme = true;
   private File usernameFile = new File(Util.getWorkingDirectory(), "skaia.usernames");
   private File usernameFileOld = new File(Util.getWorkingDirectory(), "usernames.txt");
   private List<String> usernames;

   public StorageManager() {
      if (!this.usernameFile.exists()) {
         try {
            this.usernameFile.createNewFile();
         } catch (IOException var2) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, (String)null, var2);
         }
      }

      if (this.usernameFileOld.exists()) {
         this.convertFromOld();
         this.usernameFileOld.delete();
      }

      this.usernames = new ArrayList();
      this.load();
   }

   public void convertFromOld() {
      try {
         BufferedReader br = new BufferedReader(new FileReader(this.usernameFileOld));
         List<String> toput = new ArrayList();

         String line;
         for(int var3 = 0; (line = br.readLine()) != null; ++var3) {
            toput.add(line);
         }

         this.usernames = toput;
         br.close();
         this.save();
      } catch (IOException var5) {
      }

   }

   public List<String> getUsernames() {
      List<String> uclean = new ArrayList();
      Iterator var3 = this.usernames.iterator();

      while(var3.hasNext()) {
         String s = (String)var3.next();

         try {
            String uname = URLDecoder.decode(s, "UTF-8");
            uclean.add(uname);
         } catch (UnsupportedEncodingException var5) {
         }
      }

      return uclean;
   }

   public boolean removeUsername(String un) throws IOException {
      String username = URLEncoder.encode(un, "UTF-8");
      if (this.usernames.contains(username)) {
         this.usernames.remove(username);
         this.save();
         return true;
      } else {
         return false;
      }
   }

   public boolean addUsername(String username) throws IOException {
      String uname = URLEncoder.encode(username, "UTF-8");
      if (this.usernames.contains(uname)) {
         return false;
      } else {
         this.usernames.add(uname);
         this.save();
         return true;
      }
   }

   public String getLastUsed() {
      return this.lastused;
   }

   public void setLastUsed(String last) {
      this.lastused = last;
      this.save();
   }

   public String getLanguage() {
      return this.language;
   }

   public void setLanguage(String lang) {
      this.language = lang;
   }

   public void setAutoLoginName(String name) {
      this.autoName = name;
   }

   public String getAutoLoginName() {
      return this.autoName;
   }

   public Boolean isAutoLogin() {
      return this.autoLogin;
   }

   public void setAutoLogin(Boolean hmm) {
      this.autoLogin = hmm;
   }

   public void setSyncLanguage(Boolean bool) {
      this.syncLanguage = bool;
      this.save();
   }

   public Boolean willSyncLanguage() {
      return this.syncLanguage;
   }

   public void setDark(Boolean useDark) {
      this.useDarkTheme = useDark;
   }

   public Boolean getDark() {
      return this.useDarkTheme;
   }

   public void save() {
      Map<String, Object> data = new HashMap();
      data.put("username.lastused", this.lastused);
      data.put("username.names", this.usernames);
      data.put("auto.enabled", this.autoLogin);
      data.put("auto.name", this.autoName);
      data.put("language", this.language);
      data.put("confver", this.confVersion);
      data.put("auto.language", this.syncLanguage);
      data.put("theme.dark", this.useDarkTheme);
      System.out.println("SAVED: " + this.language + this.autoName + this.autoLogin);
      Yaml yaml = new Yaml();

      try {
         FileWriter writer = new FileWriter(this.usernameFile);
         yaml.dump(data, writer);
         writer.close();
      } catch (IOException var4) {
      }

   }

   public void load() {
      try {
         BufferedReader br = new BufferedReader(new FileReader(this.usernameFile));
         Yaml yaml = new Yaml();
         Map<String, Object> data = (Map)yaml.load((Reader)br);
         br.close();
         String cv = (String)data.get("confver");
         String lang = (String)data.get("language");
         if (cv == null) {
            this.language = "en";
         } else {
            this.language = lang;
         }

         this.usernames = (List)data.get("username.names");
         this.lastused = (String)data.get("username.lastused");
         this.autoLogin = (Boolean)data.get("auto.enabled");
         this.autoName = (String)data.get("auto.name");
         this.syncLanguage = (Boolean)data.get("auto.language");
         if (this.syncLanguage == null) {
            this.syncLanguage = false;
         }

         this.useDarkTheme = (Boolean)data.get("theme.dark");
         if (this.useDarkTheme == null) {
            this.useDarkTheme = true;
         }

         System.out.println("LOADED" + this.language + this.autoName + this.autoLogin);
      } catch (Exception var9) {
      } finally {
         this.save();
      }

   }
}
