package com.axis.innovators.box.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 对状态进行管理
 * @author tzdwindows 7
 */
public class StateManager {
    private final String statusFile;
    private final Map<String, String> configMap;
    private final Properties properties;

    /**
     * 构造函数
     * @param statusName 状态文件名
     */
    public StateManager(String statusName) {
        statusFile = FolderCreator.getConfigurationFolder() + "\\" + statusName + ".properties";
        configMap = new HashMap<>();
        properties = new Properties();
        loadConfiguration();
    }

    /**
     * 默认构造函数
     */
    public StateManager() {
        statusFile = FolderCreator.getConfigurationFolder() + "\\toolbox.properties";
        configMap = new HashMap<>();
        properties = new Properties();
        loadConfiguration();
    }

    /**
     * 加载配置文件内容到Map中
     * 如果文件不存在，则创建该文件
     */
    private void loadConfiguration() {
        File configFile = new File(statusFile);

        if (!configFile.exists()) {
            try {
                configFile.getParentFile().mkdirs();
                configFile.createNewFile();
                System.out.println("The configuration file does not exist, a new one has been created: " + statusFile);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }

        try (FileInputStream fis = new FileInputStream(configFile)) {
            properties.load(fis);
            for (String key : properties.stringPropertyNames()) {
                configMap.put(key, properties.getProperty(key));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存状态到Map和配置文件中
     * @param key 键
     * @param value 值
     */
    public void saveState(String key, String value) {
        configMap.put(key, value);
        properties.setProperty(key, value);
        saveToFile();
    }

    public void saveState(String key, int value) {
        saveState(key, String.valueOf(value));
    }

    public void saveState(String key, long value) {
        saveState(key, String.valueOf(value));
    }

    public void saveState(String key, float value) {
        saveState(key, String.valueOf(value));
    }

    public void saveState(String key, boolean value) {
        saveState(key, String.valueOf(value));
    }

    public void saveState(String key, double value) {
        saveState(key, String.valueOf(value));
    }

    public void saveState(String key, char value) {
        saveState(key, String.valueOf(value));
    }

    public void saveState(String key, byte value) {
        saveState(key, String.valueOf(value));
    }

    public void saveState(String key, short value) {
        saveState(key, String.valueOf(value));
    }

    /**
     * 将当前状态保存到配置文件中
     */
    private void saveToFile() {
        try (FileOutputStream fos = new FileOutputStream(statusFile)) {
            properties.store(fos, "Updated configuration");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从Map中获取状态值
     * @param key 键
     * @return 值
     */
    public String getState(String key) {
        return configMap.get(key);
    }

    public int getStateAsInt(String key) {
        return Integer.parseInt(configMap.get(key));
    }

    public long getStateAsLong(String key) {
        return Long.parseLong(configMap.get(key));
    }

    public float getStateAsFloat(String key) {
        return Float.parseFloat(configMap.get(key));
    }

    public boolean getStateAsBoolean(String key) {
        return Boolean.parseBoolean(configMap.get(key));
    }

    public double getStateAsDouble(String key) {
        return Double.parseDouble(configMap.get(key));
    }

    public char getStateAsChar(String key) {
        return configMap.get(key).charAt(0);
    }

    public byte getStateAsByte(String key) {
        return Byte.parseByte(configMap.get(key));
    }

    public short getStateAsShort(String key) {
        return Short.parseShort(configMap.get(key));
    }
}