/*
 * Decompiled with CFR 0.152.
 */
package net.minecraft.launcher;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import net.mc.main.Util;
import net.minecraft.launcher.Language;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class OptionsManager {
    private static final Logger LOGGER = LogManager.getLogger();
    LinkedHashMap<String, String> options = new LinkedHashMap();
    File serverData = new File(Util.getWorkingDirectory(), "options.txt");
    Boolean fileExists = false;

    public OptionsManager() {
        this.loadOptions();
        this.serverData = new File(Util.getWorkingDirectory(), "options.txt");
    }

    public Boolean exists() {
        return this.fileExists;
    }

    public void setOption(String key, String value) {
        if (this.fileExists.booleanValue()) {
            this.options.put(key, value);
            this.saveOptions();
        }
    }

    public void setLanguage() {
        if (this.fileExists.booleanValue() && Language.getBoolean("minecraft.language.exists").equals(true)) {
            this.setOption("lang", Language.get("minecraft.language.code"));
            LOGGER.info("[Skaia Launcher] Successfully set the minecraft language to " + Language.get("minecraft.language.code"));
        }
    }

    public String getOption(String key) {
        return this.options.get(key);
    }

    public void saveOptions() {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(this.serverData));
            for (String p : this.options.keySet()) {
                bw.write(String.valueOf(p) + ":" + this.options.get(p));
                bw.newLine();
            }
            bw.flush();
            bw.close();
            LOGGER.info("[Skaia Launcher] Saved options.txt");
        }
        catch (IOException e) {
            LOGGER.info("[Skaia Launcher] options.txt not found.");
        }
    }

    public void loadOptions() {
        FileInputStream fstream = null;
        try {
            try {
                String strLine;
                fstream = new FileInputStream(this.serverData);
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                while ((strLine = br.readLine()) != null) {
                    String[] breakParts = strLine.split(":", 2);
                    String key = breakParts[0];
                    String value = breakParts[1];
                    this.options.put(key, value);
                }
                br.close();
            }
            catch (FileNotFoundException ex) {
                LOGGER.info("[Skaia Launcher] options.txt not found. Run minecraft at least once to generate the file!.");
                try {
                    if (fstream != null) {
                        fstream.close();
                        this.fileExists = true;
                    }
                }
                catch (IOException ex3) {
                    LOGGER.info("[Skaia Launcher] options.txt not found. Run minecraft at least once to generate the file!  .");
                }
            }
            catch (IOException ex2) {
                LOGGER.info("[Skaia Launcher] options.txt not found. Run minecraft at least once to generate the file! .");
                try {
                    if (fstream != null) {
                        fstream.close();
                        this.fileExists = true;
                    }
                }
                catch (IOException ex3) {
                    LOGGER.info("[Skaia Launcher] options.txt not found. Run minecraft at least once to generate the file!  .");
                }
            }
        }
        finally {
            try {
                if (fstream != null) {
                    fstream.close();
                    this.fileExists = true;
                }
            }
            catch (IOException ex3) {
                LOGGER.info("[Skaia Launcher] options.txt not found. Run minecraft at least once to generate the file!  .");
            }
        }
    }
}
