package cz.kamma.llamarunner;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Application configuration management for storing window size and position.
 */
public class AppConfig {

    private static final int DEFAULT_WIDTH = 900;
    private static final int DEFAULT_HEIGHT = 750;
    private static final int DEFAULT_X = -1; // -1 means center on screen
    private static final int DEFAULT_Y = -1;

    private int windowWidth;
    private int windowHeight;
    private int windowX;
    private int windowY;
    private final ConfigLocation configLocation;

    public AppConfig() {
        this(new ConfigLocation());
    }

    public AppConfig(ConfigLocation configLocation) {
        this.configLocation = configLocation;
        windowWidth = DEFAULT_WIDTH;
        windowHeight = DEFAULT_HEIGHT;
        windowX = DEFAULT_X;
        windowY = DEFAULT_Y;
        load();
    }

    private void load() {
        if (!configLocation.getConfigPropertiesFile().exists()) {
            return;
        }

        Properties props = new Properties();
        try (FileInputStream fis = new FileInputStream(configLocation.getConfigPropertiesFile())) {
            props.load(fis);

            windowWidth = Integer.parseInt(props.getProperty("windowWidth", String.valueOf(DEFAULT_WIDTH)));
            windowHeight = Integer.parseInt(props.getProperty("windowHeight", String.valueOf(DEFAULT_HEIGHT)));
            windowX = Integer.parseInt(props.getProperty("windowX", String.valueOf(DEFAULT_X)));
            windowY = Integer.parseInt(props.getProperty("windowY", String.valueOf(DEFAULT_Y)));
        } catch (IOException | NumberFormatException e) {
            // Use default values on error
        }
    }

    public void save() {
        configLocation.ensureDirectoryExists();

        Properties props = new Properties();
        props.setProperty("windowWidth", String.valueOf(windowWidth));
        props.setProperty("windowHeight", String.valueOf(windowHeight));
        props.setProperty("windowX", String.valueOf(windowX));
        props.setProperty("windowY", String.valueOf(windowY));

        try (FileOutputStream fos = new FileOutputStream(configLocation.getConfigPropertiesFile())) {
            props.store(fos, "Llama Runner Configuration");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getWindowWidth() {
        return windowWidth;
    }

    public void setWindowWidth(int windowWidth) {
        this.windowWidth = windowWidth;
    }

    public int getWindowHeight() {
        return windowHeight;
    }

    public void setWindowHeight(int windowHeight) {
        this.windowHeight = windowHeight;
    }

    public int getWindowX() {
        return windowX;
    }

    public void setWindowX(int windowX) {
        this.windowX = windowX;
    }

    public int getWindowY() {
        return windowY;
    }

    public void setWindowY(int windowY) {
        this.windowY = windowY;
    }
}
