using MainShell.Common;
using MwFramework.Controls.UIControl;
using MwFramework.ManagerService;
using Newtonsoft.Json;
using Stylet;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using static MainShell.ParaSetting.Model.SpeedSetting;

namespace MainShell.ParaSetting.Model
{
    [Export(typeof(IParameter))]
    public class RunSetting : ParameterBase
    {
        public ProductLoadProcessParameter ProductLoadProcessParameter { get; set; } = new ProductLoadProcessParameter();
        public ProductUnloadProcessParameter ProductUnloadProcessParameter { get; set; } = new ProductUnloadProcessParameter();
        public ProductPositionProcessParameter ProductPositionProcessParameter { get; set; } = new ProductPositionProcessParameter();
        public ProductHeightMeasureProcessParameter ProductHeightMeasureProcessParameter { get; set; } = new ProductHeightMeasureProcessParameter();
        public WaferLoadProcessParameter WaferLoadProcessParameter { get; set; } = new WaferLoadProcessParameter();
        public WaferUnloadProcessParameter WaferUnloadProcessParameter { get; set; } = new WaferUnloadProcessParameter();
        public WaferStraightenProcessParameter WaferStraightenProcessParameter { get; set; } = new WaferStraightenProcessParameter();
        public DieRecognizeProcessParameter DieRecognizeProcessParameter { get; set; } = new DieRecognizeProcessParameter();
        public BondingProcessParameter BondingProcessParameter { get; set; } = new BondingProcessParameter();
        public RecheckProcessParameter RecheckProcessParameter { get; set; } = new RecheckProcessParameter();
        public GlobalRunParameter GlobalRunParameter { get; set; } = new GlobalRunParameter();

        [JsonIgnore]
        [XmlIgnore]
        public FlowControlItem FlowControlItem
        {
            get
            {
                FlowControlItem item = new FlowControlItem();
                item.SubStrateLoadEnable = ProductLoadProcessParameter.EnableProcess;
                item.SubStrateUnLoadEnable = ProductUnloadProcessParameter.EnableProcess;
                item.SubstratePositionEnable = ProductPositionProcessParameter.EnableProcess;
                item.SubstrateHeightMeasureEnable = ProductHeightMeasureProcessParameter.EnableProcess;
                item.WaferLoadEnable = WaferLoadProcessParameter.EnableProcess;
                item.WaferUnLoadEnable = WaferUnloadProcessParameter.EnableProcess;
                item.WaferStraightenEnable = WaferStraightenProcessParameter.EnableProcess;
                item.WaferPositionEnable = DieRecognizeProcessParameter.EnableProcess;
                item.BondingEnable = BondingProcessParameter.EnableProcess;
                item.SubstrateRecheckEnable = RecheckProcessParameter.EnableProcess;
                return item;
            }
            set
            {
                if (value == null)
                {
                    return;
                }

                ProductLoadProcessParameter.EnableProcess = value.SubStrateLoadEnable;
                ProductUnloadProcessParameter.EnableProcess = value.SubStrateUnLoadEnable;
                ProductPositionProcessParameter.EnableProcess = value.SubstratePositionEnable;
                ProductHeightMeasureProcessParameter.EnableProcess = value.SubstrateHeightMeasureEnable;
                WaferLoadProcessParameter.EnableProcess = value.WaferLoadEnable;
                WaferUnloadProcessParameter.EnableProcess = value.WaferUnLoadEnable;
                WaferStraightenProcessParameter.EnableProcess = value.WaferStraightenEnable;
                DieRecognizeProcessParameter.EnableProcess = value.WaferPositionEnable;
                BondingProcessParameter.EnableProcess = value.BondingEnable;
                RecheckProcessParameter.EnableProcess = value.SubstrateRecheckEnable;
            }
        }

        public override void Copy(IParameter source)
        {
            if (source is RunSetting setting)
            {
                ReflectionExtension.Copy(ProductLoadProcessParameter, setting.ProductLoadProcessParameter);
                ReflectionExtension.Copy(ProductUnloadProcessParameter, setting.ProductUnloadProcessParameter);
                ReflectionExtension.Copy(ProductPositionProcessParameter, setting.ProductPositionProcessParameter);
                ReflectionExtension.Copy(ProductHeightMeasureProcessParameter, setting.ProductHeightMeasureProcessParameter);
                ReflectionExtension.Copy(WaferLoadProcessParameter, setting.WaferLoadProcessParameter);
                ReflectionExtension.Copy(WaferUnloadProcessParameter, setting.WaferUnloadProcessParameter);
                ReflectionExtension.Copy(WaferStraightenProcessParameter, setting.WaferStraightenProcessParameter);
                ReflectionExtension.Copy(DieRecognizeProcessParameter, setting.DieRecognizeProcessParameter);
                ReflectionExtension.Copy(BondingProcessParameter, setting.BondingProcessParameter);
                ReflectionExtension.Copy(RecheckProcessParameter, setting.RecheckProcessParameter);
                ReflectionExtension.Copy(GlobalRunParameter, setting.GlobalRunParameter);
            }
        }
    }

    public class FlowControlItem : PropertyChangedBase, IParameterItem
    {
        private bool _substrateLoadEnable;

        public bool SubStrateLoadEnable
        {
            get { return _substrateLoadEnable; }
            set { SetAndNotify(ref _substrateLoadEnable, value); }
        }

        private bool _substrateUnLoadEnable;

        public bool SubStrateUnLoadEnable
        {
            get { return _substrateUnLoadEnable; }
            set { SetAndNotify(ref _substrateUnLoadEnable, value); }
        }

        private bool _substratePositionEnable;

        public bool SubstratePositionEnable
        {
            get { return _substratePositionEnable; }
            set { SetAndNotify(ref _substratePositionEnable, value); }
        }

        private bool _substrateHeightMeasureEnable;

        public bool SubstrateHeightMeasureEnable
        {
            get { return _substrateHeightMeasureEnable; }
            set { SetAndNotify(ref _substrateHeightMeasureEnable, value); }
        }

        private bool _waferLoadEnable;

        public bool WaferLoadEnable
        {
            get { return _waferLoadEnable; }
            set { SetAndNotify(ref _waferLoadEnable, value); }
        }

        private bool _waferUnLoadEnable;

        public bool WaferUnLoadEnable
        {
            get { return _waferUnLoadEnable; }
            set { SetAndNotify(ref _waferUnLoadEnable, value); }
        }

        private bool _waferStraightenEnable;

        public bool WaferStraightenEnable
        {
            get { return _waferStraightenEnable; }
            set { SetAndNotify(ref _waferStraightenEnable, value); }
        }

        private bool _waferPositionEnable;

        public bool WaferPositionEnable
        {
            get { return _waferPositionEnable; }
            set { SetAndNotify(ref _waferPositionEnable, value); }
        }

        private bool _bondingEnable;

        public bool BondingEnable
        {
            get { return _bondingEnable; }
            set { SetAndNotify(ref _bondingEnable, value); }
        }

        private bool _substrateRecheckEnable;

        public bool SubstrateRecheckEnable
        {
            get { return _substrateRecheckEnable; }
            set { SetAndNotify(ref _substrateRecheckEnable, value); }
        }

        public IParameterItem Clone()
        {
            return this.MemberwiseClone() as IParameterItem;
        }
    }
}
