using MainShell.Filewritable;
using MXJM.FileWritable;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainShell.AlgorithmCalib.Model
{
    public class FusionCalibSetting : JsonFileWritableBase
    {
        [JsonIgnore]
        public override string Dir => Paths.CalibSettingPath;

        [JsonIgnore]
        public override string FileName => "FusionCalibSetting.json";

        // Pad融合的4组模块
        public List<FusionCalibModuleItem> PadModules { get; set; } = new List<FusionCalibModuleItem>();

        // Die融合的参数（单组）
        public FusionCalibModuleItem DieModule { get; set; } = new FusionCalibModuleItem();

        public static FusionCalibSetting LoadOrCreate()
        {
            var setting = new FusionCalibSetting();
            string path = Path.Combine(setting.Dir, setting.FileName);
            if (File.Exists(path))
            {
                setting.Read(path);
                setting.EnsureCollections();
                return setting;
            }

            setting.PadModules = CreateDefaultPadModules();
            setting.DieModule = CreateDefaultDieModule();
            setting.Write(path);
            return setting;
        }

        public void EnsureCollections()
        {
            if (PadModules == null)
                PadModules = new List<FusionCalibModuleItem>();
            if (DieModule == null)
                DieModule = CreateDefaultDieModule();
        }

        private static List<FusionCalibModuleItem> CreateDefaultPadModules()
        {
            var modules = new List<FusionCalibModuleItem>();
            for (int i = 1; i <= 4; i++)
            {
                modules.Add(new FusionCalibModuleItem
                {
                    ModuleName = FusionCalibModuleNames.PadFusion,
                    IsIndependent = true,
                    Index = i,
                    Camera_ID = $"Fusion_Time{i}",
                    IsWaferCalib = false,
                    CalibStep = 0.4,
                    CalibCount = 10,
                    Count = 4,
                    Step = 5.0,
                });
            }
            return modules;
        }

        private static FusionCalibModuleItem CreateDefaultDieModule()
        {
            return new FusionCalibModuleItem
            {
                ModuleName = FusionCalibModuleNames.DieFusion,
                IsIndependent = true,
                Index = 1,
                Camera_ID = "DieFusion",
                IsWaferCalib = true,
                CalibStep = 0.4,
                CalibCount = 10,
                Count = 4,
                Step = 5.0,
            };
        }
    }
}