package de.blight.game.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.*;
import java.nio.file.*;

public class GraphicsStore {

    private static final Path FILE = Paths.get("config", "graphics.json");
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    public static GraphicsSettings load() {
        if (Files.exists(FILE)) {
            try (Reader r = Files.newBufferedReader(FILE)) {
                return GSON.fromJson(r, GraphicsSettings.class);
            } catch (IOException e) {
                System.err.println("graphics.json konnte nicht geladen werden: " + e.getMessage());
            }
        }
        return new GraphicsSettings();
    }

    public static void save(GraphicsSettings gs) {
        try {
            Files.createDirectories(FILE.getParent());
            try (Writer w = Files.newBufferedWriter(FILE)) {
                GSON.toJson(gs, w);
            }
        } catch (IOException e) {
            System.err.println("graphics.json konnte nicht gespeichert werden: " + e.getMessage());
        }
    }
}
