using MainShell.AlgorithmCalib.Common;
using MainShell.Filewritable;
using MainShell.PageCalib.OriginCalib.Service;
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 static class RotateMatixFormCalibModuleNames
    {
        public const string RotateForm = "旋转表标定模块";
    }

    public class RotateMatixFormCalibModuleItem
    {
        // ===== 对标 OriginCalibModuleItem 通用属性 =====
        public string ModuleName { get; set; }
        public bool IsIndependent { get; set; }
        public string TemplatePath { get; set; }

        [JsonIgnore]
        public IAlgorithmCalibrationPostProcessor PostProcessor { get; set; }

        // ===== 旋转表特有参数（原 RotateCalibParItem） =====
        public double X1AvoidancePosition { get; set; }
        public double RotateStep { get; set; }
        public double WaferStep { get; set; }
        public int RowCol { get; set; }

        // ===== 验证参数 =====
        public int VerifyRowCol { get; set; }
        public double VerifyWaferStep { get; set; }
        public double VerifyRotateRange { get; set; }

        // ===== 运行时参数（不序列化） =====
        [JsonIgnore]
        public double StartX { get; set; }
        [JsonIgnore]
        public double StartY { get; set; }
        [JsonIgnore]
        public double VerifyStartX { get; set; }
        [JsonIgnore]
        public double VerifyStartY { get; set; }

        //逼近容差
        public double AlignTolerance { get; set; } = 0.005;
    }

    public class RotateMatixFormCalibSetting : JsonFileWritableBase
    {
        [JsonIgnore]
        public override string Dir => Paths.CalibSettingPath;

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

        public List<RotateMatixFormCalibModuleItem> Modules { get; set; } = new List<RotateMatixFormCalibModuleItem>();

        public static RotateMatixFormCalibSetting LoadOrCreate()
        {
            // TODO: 对标 OriginCalibSetting.LoadOrCreate()
            var setting = new RotateMatixFormCalibSetting();
            string path = Path.Combine(setting.Dir, setting.FileName);
            if (File.Exists(path))
            {
                setting.Read(path);
                setting.EnsureCollections();
                return setting;
            }

            setting.Modules = CreateDefaultModules();
            setting.Write(path);
            return setting;
        }

        public void EnsureCollections()
        {
            // TODO: 对标 OriginCalibSetting.EnsureCollections()
            if (Modules == null)
                Modules = new List<RotateMatixFormCalibModuleItem>();

        }

        private static List<RotateMatixFormCalibModuleItem> CreateDefaultModules()
        {
            // TODO: 原 RotateCalibParItem 的默认值搬到这里
            return new List<RotateMatixFormCalibModuleItem>
            {
                new RotateMatixFormCalibModuleItem
                {
                    ModuleName = RotateMatixFormCalibModuleNames.RotateForm,
                    IsIndependent = true,
                    TemplatePath = "",
                    RotateStep = 0.05,
                    WaferStep = 0.4,
                    RowCol = 10,
                }
            };
        }
    }
}
