package me.koba1.led.managers;

import lombok.Getter;
import me.koba1.led.Main;
import me.koba1.led.model.Settings;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;

@Getter
public class SettingsManager {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    private Settings settings;

    public SettingsManager() {
        this.settings = new Settings();
    }

    public void load() {
        if (Main.getSavedDataFile().exists() && Main.getSavedDataFile().length() == 0) {
            return;
        }
        // Settings settings = MAPPER.readValue(Main.getSavedDataFile(), new TypeReference<Settings>() {});
        this.settings = MAPPER.readValue(Main.getSavedDataFile(), new TypeReference<Settings>() {});
        System.out.println("Loaded settings: " + this.settings);
    }

    public Settings update(Settings settings) {
        System.out.println("Update settings: " + settings);
        this.settings = settings;
        // save();
        //todo: Wifi Setting update
        return this.settings;
    }

    public void save() {
        System.out.println("Saving " + this.settings);
        MAPPER.writeValue(Main.getSavedDataFile(), this.settings);
    }
}
