namespace ConfigOperator;

public class ConfigFileLoader(ConfigFiles configFiles)
{
    public void Load()
    {
        if (!DierctoryHelper.GetAllFiles(Paths.ConfigFileFolder, out List<string>? configs) || configs is null)
            return;
        configFiles.Files.Clear();

        foreach (string filePath in configs)
        {
            if (!XmlHelper.DeserializeXml(filePath, out ConfigSaveXML? output) || output is null)
                continue;

            if (output.Mini8Configs is null)
                continue;

            FileInfo fileInfo = new(filePath);

            TemperatureConfig temperatureConfig = new();
            output.Adapt(temperatureConfig);
            temperatureConfig.ConfigName = fileInfo.Name.Replace(fileInfo.Extension, "");
            temperatureConfig.Mini8sConfig = [];
            foreach (Mini8ConfigXML mini8ConfigXML in output.Mini8Configs)
            {
                Mini8Config mini8Config = new()
                {
                    Index = mini8ConfigXML.Index,
                    ChannelConfig = []
                };
                temperatureConfig.Mini8sConfig[mini8ConfigXML.Index] = mini8Config;

                if (mini8ConfigXML.ChannelConfig is null)
                    continue;  

                //mini8ConfigXML.ChannelConfig.Foreach(t => mini8Config.ChannelConfig[t.Index] = t);

                foreach (var channelConfig in mini8ConfigXML.ChannelConfig)
                {
                    if (channelConfig.Running_P < 0)
                        channelConfig.Running_P = 100;

                    if (channelConfig.Running_I < 0)
                        channelConfig.Running_I = 100;

                    if (channelConfig.Running_D < 0)
                        channelConfig.Running_D = 100;

                    mini8Config.ChannelConfig[channelConfig.Index] = channelConfig;
                }
            }

            configFiles.Files[temperatureConfig.ConfigName] = temperatureConfig;
        }
    }

    public void Save()
    {
        if (configFiles.Files is null)
            return;

        foreach (TemperatureConfig configFile in configFiles.Files.Values)
        {
            if (configFile is null)
                continue;
            if (string.IsNullOrEmpty(configFile.ConfigName))
                continue;
            if (configFile.Mini8sConfig is null)
                continue;

            ConfigSaveXML configSaveXML = new()
            {
                EditTime = configFile.EditTime,
                Editor = configFile.Editor,
                Description = configFile.Description,
                Mini8Configs = []
            };

            foreach (Mini8Config mini8Config in configFile.Mini8sConfig.Values)
            {
                if (mini8Config.ChannelConfig is null)
                    continue;

                Mini8ConfigXML mini8ConfigXML = new()
                {
                    Index = mini8Config.Index,
                    ChannelConfig = [.. mini8Config.ChannelConfig.Values],
                };
                configSaveXML.Mini8Configs.Add(mini8ConfigXML);
            }

            string path = Path.Combine(Paths.ConfigFileFolder, $"{configFile.ConfigName}.mc");
            XmlFileHelper.WriteFile(path, configSaveXML);
        }
    }

    public void LoadCurrentSetting()
    {
        string filePath = Path.Combine(Paths.ConfigFileFolder, "CurrentConfig", $"LastSaved.mc");

        if (!XmlHelper.DeserializeXml(filePath, out ConfigSaveXML? output) || output is null)
            return;

        if (output.Mini8Configs is null)
            return;

        FileInfo fileInfo = new(filePath);

        TemperatureConfig temperatureConfig = new();
        output.Adapt(temperatureConfig);
        temperatureConfig.ConfigName = fileInfo.Name.Replace(fileInfo.Extension, "");
        temperatureConfig.Mini8sConfig = [];
        foreach (Mini8ConfigXML mini8ConfigXML in output.Mini8Configs)
        {
            Mini8Config mini8Config = new()
            {
                Index = mini8ConfigXML.Index,
                ChannelConfig = []
            };
            temperatureConfig.Mini8sConfig[mini8ConfigXML.Index] = mini8Config;

            if (mini8ConfigXML.ChannelConfig is null)
                continue;

            mini8ConfigXML.ChannelConfig.Foreach(t => mini8Config.ChannelConfig[t.Index] = t);
        }

        configFiles.CurrentConfigFile = temperatureConfig;
    }

    public void SaveCurrentSetting()
    {
        if (configFiles.CurrentConfigFile is null)
            return;
        TemperatureConfig configFile = configFiles.CurrentConfigFile;

        if (configFile is null)
            return;
        if (string.IsNullOrEmpty(configFile.ConfigName))
            return;
        if (configFile.Mini8sConfig is null)
            return;

        ConfigSaveXML configSaveXML = new()
        {
            EditTime = configFile.EditTime,
            Editor = configFile.Editor,
            Description = configFile.Description,
            Mini8Configs = []
        };

        foreach (Mini8Config mini8Config in configFile.Mini8sConfig.Values)
        {
            if (mini8Config.ChannelConfig is null)
                continue;

            Mini8ConfigXML mini8ConfigXML = new()
            {
                Index = mini8Config.Index,
                ChannelConfig = [.. mini8Config.ChannelConfig.Values],
            };
            configSaveXML.Mini8Configs.Add(mini8ConfigXML);
        }

        string path = Path.Combine(Paths.ConfigFileFolder, "CurrentConfig", $"LastSaved.mc");
        XmlFileHelper.WriteFile(path, configSaveXML);
    }
}

public class CurrentConfigXML
{
    public string? SelectedConfig { get; set; }
}

public class ConfigSaveXML
{
    public DateTime EditTime { get; set; }
    public string? Editor { get; set; }
    public string? Description { get; set; }

    public List<Mini8ConfigXML>? Mini8Configs { get; set; }
}

public class Mini8ConfigXML
{
    public byte Index { get; set; }
    public List<ChannelConfig>? ChannelConfig { get; set; }
}