using System.Text.Json;
using System.Text.Json.Serialization;
using LindtLeerformPlugin.Models;

namespace LindtLeerformPlugin.Services;

public static class CalibrationDataStore
{
    private static readonly string FilePath = Path.Combine("..", "Data", "Config", "calibration.json");

    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        PropertyNameCaseInsensitive = true,
        WriteIndented = true,
        Converters = { new JsonStringEnumConverter() }
    };

    public static CalibrationData? Load()
    {
        if (!File.Exists(FilePath))
            return null;

        var json = File.ReadAllText(FilePath);
        return JsonSerializer.Deserialize<CalibrationData>(json, JsonOptions);
    }

    public static void Save(CalibrationData data)
    {
        var directory = Path.GetDirectoryName(FilePath);
        if (directory != null)
            Directory.CreateDirectory(directory);

        var json = JsonSerializer.Serialize(data, JsonOptions);
        File.WriteAllText(FilePath, json);
    }
}
