using Common.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace Common.Models
{
    /// <summary>
    /// 样品瓶状态
    /// </summary>
    public enum SampleBottleStateEnum
    {
        /// <summary>
        /// 0:还未投料
        /// </summary>
        Empty = 0,
        /// <summary>
        /// 正在投料中
        /// </summary>
        IsDosing = 1,

        DoseFinish = 2,

        /// <summary>
        ///  1：高温反应中 
        /// </summary>
        HighTemperaturReact = 3,
        /// <summary>
        /// 2：低温反应中
        /// </summary>
        LowTemperaturReact = 4,
        /// <summary>
        ///穿刺取样中
        /// </summary>
        PunctureSampling = 5,
        /// <summary>
        /// 开盖取样中
        /// </summary>
        OpenLidSampling = 6,
        /// <summary>
        /// 穿刺补液
        /// </summary>
        PunctureAddLiquid = 7,
        /// <summary>
        /// 开盖补液
        /// </summary>
        OpenLidAddLiquid = 8,
        /// <summary>
        ///  稀释中
        /// </summary>
        Dilute = 9,
        /// <summary>
        /// 过滤中
        /// </summary>
        Filter = 10,
    }

    /// <summary>
    /// 反应温度选择
    /// </summary>
    public enum SampleBottleReactTemperatureEnum
    {
        高温反应,
        低温反应,
        无,
    }

    /// <summary>
    /// 反应模块的操作类型  
    /// </summary>
    public enum ReactOperationEnum
    {
        取样,
        补液,
        无,
    }

    /// <summary>
    /// 取样方式
    /// </summary>
    public enum SampleMethod
    {
        穿刺取样,
        开盖取样,
    }

    /// <summary>
    /// 补液方式
    /// </summary>
    public enum AddLiquidMethod
    {
        穿刺补液,
        开盖补液,
    }

    /// <summary>
    /// 稀释方式
    /// </summary>
    public enum DiluteMethod
    {
        移液枪稀释,
        穿刺针稀释,
        加样针稀释,
    }


    /// <summary>
    /// 样品瓶盖子的状态
    /// </summary>
    public enum SampleBottleLidStateEnum
    {
        Close,
        Open,
    }

    /// <summary>
    /// 样品瓶类型
    /// </summary>
    public enum SampleBottleTypeEnum
    {
        _5mL = 1,
        _12mL = 2,
    }


    [Serializable]
    /// <summary>
    /// 样品瓶属性
    /// </summary>
    public class SampleBottleModel : ObservableObject
    {
        private long sampleSeqId;
        public long SampleSeqId
        {
            get { return sampleSeqId; }
            set { SetProperty(ref sampleSeqId, value); }
        }

        private SampleBottleTypeEnum sampleBottleType = SampleBottleTypeEnum._5mL;
        /// <summary>
        /// 样品瓶类型  5ML/12ML
        /// </summary>
        public SampleBottleTypeEnum SampleBottleType
        {
            get { return sampleBottleType; }
            set { SetProperty(ref sampleBottleType, value); }
        }

        private string _boxsn = string.Empty;
        /// <summary>
        /// 样品瓶所在的工装的二维码信息
        /// </summary>
        public string BoxSNCode
        {
            get => _boxsn;
            set { SetProperty(ref _boxsn, value); }
        }

        private string _sn = string.Empty;
        /// <summary>
        /// 样品瓶的二维码信息
        /// </summary>
        public string SNCode
        {
            get => _sn;
            set { SetProperty(ref _sn, value); }
        }

        private bool _haveBottle = false;
        /// <summary>
        /// 是否有样品瓶子
        /// </summary>
        public bool HaveBottle
        {
            get => _haveBottle;
            set { SetProperty(ref _haveBottle, value); }
        }

        private bool bdosefinish = false;
        /// <summary>
        /// 是否配置完成
        /// </summary>
        public bool bDoseFinish
        {
            get { return bdosefinish; }
            set { bdosefinish = value; }
        }

        private int _posId_inbox = 1;
        /// <summary>
        /// 样品瓶在工装中的第几个位置(1-12)
        /// </summary>
        public int PosId_InBox
        {
            get => _posId_inbox;
            set { SetProperty(ref _posId_inbox, value); }
        }

        #region 投料站
        private string _sampleContentName = string.Empty;
        /// <summary>
        /// 样品瓶投料结果内容名称
        /// </summary>
        public string SampleContentName
        {
            get { return _sampleContentName; }
            set { SetProperty(ref _sampleContentName, value); }
        }

        private SampleBottleStateEnum _sampleBottleState = SampleBottleStateEnum.Empty;
        /// <summary>
        /// 样品瓶状态  正在哪一步操作中
        /// </summary>
        public SampleBottleStateEnum SampleBoxState
        {
            get => _sampleBottleState;
            set
            {
                if (_sampleBottleState != value)
                {
                    _sampleBottleState = value;
                    OnPropertyChanged(nameof(SampleBoxState));
                    OnPropertyChanged(nameof(DisplayColor_Dose));
                    OnPropertyChanged(nameof(DisplayColor_React));
                }
            }
        }

        /// <summary>
        /// 样品瓶状态，在投料站中展示颜色
        /// </summary>
        public Brush DisplayColor_Dose
        {
            get
            {
                switch (SampleBoxState)
                {
                    case SampleBottleStateEnum.Empty:
                        if (HaveBottle) return (Brush)new BrushConverter().ConvertFrom("#aff0b3");
                        else return (Brush)new BrushConverter().ConvertFrom("#dcd8d8");     //return "#d1caca".FromHex(); // 未配置：白色
                    case SampleBottleStateEnum.IsDosing:
                        return (Brush)new BrushConverter().ConvertFrom("#f6ee73");  // 状态：黄色
                    case SampleBottleStateEnum.HighTemperaturReact:
                    case SampleBottleStateEnum.LowTemperaturReact:
                        return (Brush)new BrushConverter().ConvertFrom("#e3612e");  // 状态：橘色
                    case SampleBottleStateEnum.OpenLidSampling:
                    case SampleBottleStateEnum.PunctureSampling:
                        return (Brush)new BrushConverter().ConvertFrom("#5f84ee"); // 紫色
                    case SampleBottleStateEnum.Dilute:
                    case SampleBottleStateEnum.Filter:
                        return (Brush)new BrushConverter().ConvertFrom("#61b2f5"); // 蓝色
                    default:
                        if (HaveBottle) return (Brush)new BrushConverter().ConvertFrom("#aff0b3");
                        else return (Brush)new BrushConverter().ConvertFrom("#dcd8d8");
                }
            }
        }

        /// <summary>
        /// 样品瓶状态，在反应站展示颜色
        /// </summary>
        public Brush DisplayColor_React
        {
            get
            {
                switch (SampleBoxState)
                {
                    case SampleBottleStateEnum.Empty:
                        if (HaveBottle) return (Brush)new BrushConverter().ConvertFrom("#aff0b3");
                        else return (Brush)new BrushConverter().ConvertFrom("#dcd8d8");     //return "#d1caca".FromHex(); // 未配置：白色
                    //case SampleBottleStateEnum.IsDosing:
                    //    return (Brush)new BrushConverter().ConvertFrom("#f6ee73");  // 状态：黄色
                    case SampleBottleStateEnum.HighTemperaturReact:
                    case SampleBottleStateEnum.LowTemperaturReact:
                        return (Brush)new BrushConverter().ConvertFrom("#e3612e");  // 状态：橘色
                    case SampleBottleStateEnum.OpenLidSampling:
                    case SampleBottleStateEnum.PunctureSampling:
                        return (Brush)new BrushConverter().ConvertFrom("#5f84ee"); // 紫色
                    case SampleBottleStateEnum.Dilute:
                    case SampleBottleStateEnum.Filter:
                        return (Brush)new BrushConverter().ConvertFrom("#61b2f5"); // 蓝色
                    default:
                        if (HaveBottle) return (Brush)new BrushConverter().ConvertFrom("#aff0b3");
                        else return (Brush)new BrushConverter().ConvertFrom("#dcd8d8");
                }
            }
        }

        private SampleBottleLidStateEnum _sampleBottleLidState = SampleBottleLidStateEnum.Close;
        /// <summary>
        /// 样品瓶盖子的状态
        /// </summary>
        public SampleBottleLidStateEnum SampleBottleLidState
        {
            get => _sampleBottleLidState;
            set => _sampleBottleLidState = value;
        }

        private ObservableCollection<MaterialDoseFunctionFlow> _materialDoseFlowList = new ObservableCollection<MaterialDoseFunctionFlow>();
        /// <summary>
        /// 样品瓶中投料站流程集合，用于显示datagridView控件中
        /// </summary>
        public ObservableCollection<MaterialDoseFunctionFlow> MaterialDoseFlowList
        {
            get
            {
                return _materialDoseFlowList;
            }
            set
            {
                // 设置新值
                if (value != null && SetProperty(ref _materialDoseFlowList, value))
                {
                    // 先取消旧集合的事件订阅
                    _materialDoseFlowList.CollectionChanged -= LiquidFlowList_CollectionChanged;
                    // 订阅新集合的事件
                    _materialDoseFlowList.CollectionChanged += LiquidFlowList_CollectionChanged;
                    // 手动触发一次计算
                    LiquidFlowList_CollectionChanged(this, null);
                }
            }
        }

        #region 预估时间相关

        private double _estimatedDuration = 0;
        /// <summary>
        /// 预估投料耗时时间，单位min
        /// </summary>
        public double EstimatedDuration
        {
            get => _estimatedDuration;
            set
            {
                SetProperty(ref _estimatedDuration, value, nameof(EstimatedDuration));
                {
                    if (remainDuration == 0) // 假设初始值为0，代表未初始化
                    {
                        remainDuration = value;
                        OnPropertyChanged(nameof(RemainDuration));
                    }
                    OnPropertyChanged(nameof(ProgressPercentage));
                }
            }
        }

        // 液体流程集合项变化时更新时间
        private void LiquidFlowList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            EstimatedDuration = 1 * _materialDoseFlowList.Count;
        }

        private double remainDuration = 0;
        /// <summary>
        /// 预估原样瓶配置剩余时间，单位min
        /// </summary>
        public double RemainDuration
        {
            get { return remainDuration; }
            set
            {
                SetProperty(ref remainDuration, value);
                OnPropertyChanged(nameof(ProgressPercentage));
            }
        }

        /// <summary>
        /// 用于进度绑定的属性
        /// </summary>
        public double ProgressPercentage
        {
            get
            {
                if (EstimatedDuration <= 0) return 1.0;
                double usedRatio = 1.0 - (RemainDuration / (EstimatedDuration)); // 注意单位转换
                return Math.Max(0, Math.Min(1, usedRatio));
            }
        }
        #endregion

        public SampleBottleModel()
        {
            // ObservableCollection 会在项添加 / 删除时触发 CollectionChanged 事件，需在 ReactBottleModel 中订阅该事件，确保集合项变化时重新计算时间。
            // 初始化集合时订阅 CollectionChanged 事件
            _materialDoseFlowList.CollectionChanged += LiquidFlowList_CollectionChanged;

        }

        #endregion

        #region 反应站
        private int _highTempModuleNo = 0;
        /// <summary>
        /// 样品瓶在高温模块的编号（1-4）
        /// </summary>
        public int HighTempModuleNo
        {
            get => _highTempModuleNo;
            set => _highTempModuleNo = value;
        }

        private int _pos_inhightemperatur = 0;
        /// <summary>
        /// 样品瓶在高温模块中的位置（1-24）
        /// </summary>
        public int Pos_InHighTemperatur
        {
            get => _pos_inhightemperatur;
            set => _pos_inhightemperatur = value;
        }

        private int _lowTempModuleNo = 0;
        /// <summary>
        /// 样品瓶在低温模块的编号（1-2）
        /// </summary>
        public int LowTempModuleNo
        {
            get => _lowTempModuleNo;
            set => _lowTempModuleNo = value;
        }

        private int _pos_inlowtemperatur = 0;
        /// <summary>
        /// 样品瓶在低温模块中的位置（1-24）
        /// </summary>
        public int Pos_InLowTemperatur
        {
            get => _pos_inlowtemperatur;
            set => _pos_inlowtemperatur = value;
        }

        private SampleBottleReactTemperatureEnum reactTemperatureType = SampleBottleReactTemperatureEnum.高温反应;
        /// <summary>
        /// 反应温度方式
        /// </summary>
        public SampleBottleReactTemperatureEnum ReactTemperatureType
        {
            get { return reactTemperatureType; }
            set
            {
                SetProperty(ref reactTemperatureType, value);
                OnPropertyChanged(nameof(CurrentTemperatureModel));
            }
        }

        private SampleBottleTemperatureModel tempmodel = new SampleBottleTemperatureModel();
        public SampleBottleTemperatureModel CurrentTemperatureModel
        {
            get => tempmodel;
            set => SetProperty(ref tempmodel, value);
        }

        private SampleMethod samplemethod = SampleMethod.穿刺取样;
        /// <summary>
        /// 取样方式
        /// </summary>
        public SampleMethod SampleMethod
        {
            get { return samplemethod; }
            set
            {
                SetProperty(ref samplemethod, value);
            }
        }

        private AddLiquidMethod addliquidmethod = AddLiquidMethod.穿刺补液;
        /// <summary>
        /// 补液方式
        /// </summary>
        public AddLiquidMethod AddLiquidType
        {
            get { return addliquidmethod; }
            set
            {
                SetProperty(ref addliquidmethod, value);
            }
        }

        private double height = 0;
        /// <summary>
        /// 穿刺取样高度
        /// </summary>
        public double PunchHeight
        {
            get { return height; }
            set { SetProperty(ref height, value); }
        }


        private ObservableCollection<ReactSampleFunctionFlow> sampleFunctionFlowList = new ObservableCollection<ReactSampleFunctionFlow>();
        /// <summary>
        /// 取样流程
        /// </summary>
        public ObservableCollection<ReactSampleFunctionFlow> SampleFunctionFlowList
        {
            get { return sampleFunctionFlowList; }
            set { SetProperty(ref sampleFunctionFlowList, value); }
        }

        private ObservableCollection<AddLiquidFunctionFlow> _addLiquidFlowList = new ObservableCollection<AddLiquidFunctionFlow>();
        /// <summary>
        /// 加液流程集合，用于显示datagridView控件中
        /// </summary>
        public ObservableCollection<AddLiquidFunctionFlow> AddLiquidFunctionFlowList
        {
            get { return _addLiquidFlowList; }
            set { SetProperty(ref _addLiquidFlowList, value); }
        }
        #endregion
    }
}
