using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;

public class ControlConfigManager
{
    private const string ConfigFolder = "ControlConfig";
    private Dictionary<string, Dictionary<string, MinMaxValue>> _config;

    private string _configFile;
    //string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
    public ControlConfigManager()
    {
        _configFile=Path.Combine(ConfigFolder, "global.json");
        LoadConfig();
    }
    public ControlConfigManager(string configFileName)
    {
        _configFile = Path.Combine(ConfigFolder, $"{configFileName}.json");
        LoadConfig();
    }
    private void LoadConfig()
    {
        if (!Directory.Exists(ConfigFolder))
        {
            Directory.CreateDirectory(ConfigFolder);
        }
        if (File.Exists(_configFile))
        {
            string json = File.ReadAllText(_configFile);
            _config = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, MinMaxValue>>>(json);
        }
        else
        {
            _config = new Dictionary<string, Dictionary<string, MinMaxValue>>();
        }
    }

    public void SaveConfig()
    {
        string json = JsonConvert.SerializeObject(_config, Newtonsoft.Json.Formatting.Indented);
        File.WriteAllText(_configFile, json);
    }

    public MinMaxValue GetMinMax(string formName, string controlName)
    {
        if (_config.ContainsKey(formName) && _config[formName].ContainsKey(controlName))
        {
            return _config[formName][controlName];
        }
        return new MinMaxValue { MinValue = int.MinValue, MaxValue = int.MaxValue }; // 默认值
    }

    public void SetMinMax(string formName, string controlName, MinMaxValue value)
    {
        if (!_config.ContainsKey(formName))
        {
            _config[formName] = new Dictionary<string, MinMaxValue>();
        }
        _config[formName][controlName] = value;
    }
}

public class MinMaxValue
{
    public MinMaxValue() { }
    public MinMaxValue(double minValue, double maxValue)
    {
        MinValue = minValue;
        MaxValue = maxValue;
    }
   
    public double MinValue { get; set; }
    public double MaxValue { get; set; }
}