package de.ellpeck.prettypipes.misc;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.ellpeck.prettypipes.PrettyPipes;
import net.minecraft.client.Minecraft;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class PlayerPrefs {

    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private static PlayerPrefs instance;

    public ItemOrder terminalItemOrder = ItemOrder.AMOUNT;
    public boolean terminalAscending;
    public boolean syncJei = true;

    public void save() {
        var file = PlayerPrefs.getFile();
        if (file.exists())
            file.delete();
        try (var writer = new FileWriter(file)) {
            PlayerPrefs.GSON.toJson(this, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static PlayerPrefs get() {
        if (PlayerPrefs.instance == null) {
            var file = PlayerPrefs.getFile();
            if (file.exists()) {
                try (var reader = new FileReader(file)) {
                    PlayerPrefs.instance = PlayerPrefs.GSON.fromJson(reader, PlayerPrefs.class);
                    return PlayerPrefs.instance;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            PlayerPrefs.instance = new PlayerPrefs();
        }
        return PlayerPrefs.instance;
    }

    private static File getFile() {
        var location = Minecraft.getInstance().gameDirectory;
        return new File(location, PrettyPipes.ID + "prefs");
    }
}
