package de.blight.game.config;

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

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

public class KeyBindingStore {

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

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

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