using MwFramework.Controls.UIControl;
using MwFramework.ManagerService;
using Stylet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MaxwellFramework.Core.Attributes;
using System.ComponentModel.Composition;

namespace MXJM.Parameter.Maintance
{
    [Export(typeof(IParameter))]
    public class HeightBaseSetting : ParameterBase
    {
        public HeightBaseItem HeightBaseItem { get; set; } = new HeightBaseItem();
        public DiaHeightItem DiaHeightItem { get; set; } = new DiaHeightItem();

        public CapHeightItem CapHeightItem { get; set; } = new CapHeightItem();

        public override void Copy(IParameter source)
        {
            HeightBaseSetting setting = source as HeightBaseSetting;
            if (setting != null)
            {
                ReflectionExtension.Copy<HeightBaseItem>(HeightBaseItem, setting.HeightBaseItem);
                ReflectionExtension.Copy<DiaHeightItem>(DiaHeightItem, setting.DiaHeightItem);
                ReflectionExtension.Copy<CapHeightItem>(CapHeightItem, setting.CapHeightItem);
            }
        }
    }

    public class HeightBaseItem : PropertyChangedBase, IParameterItem
    {
        private double _measureWaferHeightX11;
        /// <summary>
        /// 测量方片时X11坐标
        /// </summary>
        [WatchValue("测量方片时X11坐标")]
        public double MeasureWaferHeightX11
        {
            get { return _measureWaferHeightX11; }
            set { if (_measureWaferHeightX11 != value) { _measureWaferHeightX11 = value; OnPropertyChanged(nameof(MeasureWaferHeightX11)); } }
        }

        private double _measureWaferHeightY11;
        /// <summary>
        /// 测量方片时Y11坐标
        /// </summary>
        [WatchValue("测量方片时Y11坐标")]
        public double MeasureWaferHeightY11
        {
            get { return _measureWaferHeightY11; }
            set { if (_measureWaferHeightY11 != value) { _measureWaferHeightY11 = value; OnPropertyChanged(nameof(MeasureWaferHeightY11)); } }
        }

        //龙门2避让位
        private double _avoidancePositionX21;
        [WatchValue("龙门X21的避让位")]
        public double AvoidancePositionX21
        {
            get { return _avoidancePositionX21; }
            set { if (_avoidancePositionX21 != value) { _avoidancePositionX21 = value; OnPropertyChanged(nameof(AvoidancePositionX21)); } }
        }

        private double _avoidancePositionY21;
        [WatchValue("龙门Y21的避让位")]
        public double AvoidancePositionY21
        {
            get { return _avoidancePositionY21; }
            set { if (_avoidancePositionY21 != value) { _avoidancePositionY21 = value; OnPropertyChanged(nameof(AvoidancePositionY21)); } }
        }

        private double _stageDatumHeight;
        [WatchValue("载台基准高度")]
        /// <summary>
        /// 载台基准高度
        /// </summary>
        public double StageDatumHeight
        {
            get { return _stageDatumHeight; }
            set { if (_stageDatumHeight != value) { _stageDatumHeight = value; OnPropertyChanged(nameof(StageDatumHeight)); } }
        }

        private double _needleTouchWaferZHeight;
        [WatchValue("刺晶头接触到Wafer高度")]
        /// <summary>
        /// 刺晶头接触到Wafer高度
        /// </summary>
        public double NeedleTouchWaferZHeight
        {
            get { return _needleTouchWaferZHeight; }
            set { if (_needleTouchWaferZHeight != value) { _needleTouchWaferZHeight = value; OnPropertyChanged(nameof(NeedleTouchWaferZHeight)); } }
        }


        private double _cZCalibrationHeight;
        /// <summary>
        /// CZ标定高度
        /// </summary>
        [WatchValue("CZ标定高度")]
        public double CZCalibrationHeight
        {
            get { return _cZCalibrationHeight; }
            set { if (_cZCalibrationHeight != value) { _cZCalibrationHeight = value; OnPropertyChanged(nameof(CZCalibrationHeight)); } }
        }


        private double _waferFilmThickness;
        /// <summary>
        /// 方片膜厚度
        /// </summary>
        [WatchValue("方片膜厚度")]
        public double WaferFilmThickness
        {
            get { return _waferFilmThickness; }
            set { if (_waferFilmThickness != value) { _waferFilmThickness = value; OnPropertyChanged(nameof(WaferFilmThickness)); } }
        }
        private double _dieThickness;
        /// <summary>
        /// 晶粒厚度
        /// </summary>
        [WatchValue("晶粒厚度")]
        public double DieThickness
        {
            get { return _dieThickness; }
            set { if (_dieThickness != value) { _dieThickness = value; OnPropertyChanged(nameof(DieThickness)); } }
        }

        private double _glassThickness = 0.5;
        [WatchValue("Glass厚度")]
        /// <summary>
        /// //Glass厚度
        /// </summary>
        public double GlassThickness
        {
            get { return _glassThickness; }
            set { if (_glassThickness != value) { _glassThickness = value; OnPropertyChanged(nameof(GlassThickness)); } }
        }

        private double _carrierThickness = 0.5;
        [WatchValue("Carrier厚度")]
        /// <summary>
        /// Carrier厚度
        /// </summary>
        public double CarrierThickness
        {
            get { return _carrierThickness; }
            set { if (_carrierThickness != value) { _carrierThickness = value; OnPropertyChanged(nameof(CarrierThickness)); } }
        }

        private double _fromDieToPadDistance;
        /// <summary>
        /// Gap2:Die到Pad的距离
        /// </summary>
        [WatchValue("Gap2:Die到Pad的距离")]
        public double FromDieToPadDistance
        {
            get { return _fromDieToPadDistance; }
            set { if (_fromDieToPadDistance != value) { _fromDieToPadDistance = value; OnPropertyChanged(nameof(FromDieToPadDistance)); } }
        }

        private double _gap1;
        /// <summary>
        /// Gap1
        /// </summary>
        [WatchValue("Gap1")]
        public double Gap1
        {
            get { return _gap1; }
            set { if (_gap1 != value) { _gap1 = value; OnPropertyChanged(nameof(Gap1)); } }
        }

        private double _glassZHeight;
        /// <summary>
        /// 测Glass时Z高度
        /// </summary>
        [WatchValue("测Glass时Z高度")]
        public double GlassZHeight
        {
            get { return _glassZHeight; }
            set { if (_glassZHeight != value) { _glassZHeight = value; OnPropertyChanged(nameof(GlassZHeight)); } }
        }


        private double _waferZHeight;
        /// <summary>
        /// 测Wafer时Z高度
        /// </summary>
        [WatchValue("测Wafer时Z高度")]
        public double WaferZHeight
        {
            get { return _waferZHeight; }
            set { if (_waferZHeight != value) { _waferZHeight = value; OnPropertyChanged(nameof(WaferZHeight)); } }
        }

        private ObservableCollection<TeachPoint> _glassHeightMeasureList = new ObservableCollection<TeachPoint>();

        public ObservableCollection<TeachPoint> GlassHeightMeasureList
        {
            get { return _glassHeightMeasureList; }
            set { if (_glassHeightMeasureList != value) { _glassHeightMeasureList = value; OnPropertyChanged(nameof(GlassHeightMeasureList)); } }
        }

        private ObservableCollection<TeachPoint> _waferHeightMeasureList = new ObservableCollection<TeachPoint>();

        public ObservableCollection<TeachPoint> WaferHeightMeasureList
        {
            get { return _waferHeightMeasureList; }
            set { if (_waferHeightMeasureList != value) { _waferHeightMeasureList = value; OnPropertyChanged(nameof(WaferHeightMeasureList)); } }
        }


        public IParameterItem Clone()
        {
            HeightBaseItem clone = new HeightBaseItem();
            if (this.GlassHeightMeasureList != null && this.GlassHeightMeasureList.Count != 0)
            {
                foreach (var item in this.GlassHeightMeasureList)
                {
                    clone.GlassHeightMeasureList.Add(item.Clone() as TeachPoint);
                }
            }
            if (this.WaferHeightMeasureList != null && this.WaferHeightMeasureList.Count != 0)
            {
                foreach (var item in this.WaferHeightMeasureList)
                {
                    clone.WaferHeightMeasureList.Add(item.Clone() as TeachPoint);
                }
            }
            clone.NeedleTouchWaferZHeight = this.NeedleTouchWaferZHeight;

            return clone;
        }

    }

    public class DiaHeightItem : PropertyChangedBase, IParameterItem
    {
        public IParameterItem Clone()
        {
            return this.MemberwiseClone() as IParameterItem;
            //DiaHeightItem clone = new DiaHeightItem();
            //clone.CaliZHeightPositionX = this.CaliZHeightPositionX;
            //clone.CaliZHeightPositionY = this.CaliZHeightPositionY;
            //clone.CaliZHeightPositionZ = this.CaliZHeightPositionZ;
            //clone.GlassZHeight = this.GlassZHeight;
            //clone.WaferZHeight = this.WaferZHeight;
            //return clone;
        }

        private double _needleTouchHeight;
        /// <summary>
        /// 标定时对刀高度
        /// </summary>
        [WatchValue("标定时对刀高度")]
        public double NeedleTouchHeight
        {
            get { return _needleTouchHeight; }
            set { if (_needleTouchHeight != value) { _needleTouchHeight = value; OnPropertyChanged(nameof(NeedleTouchHeight)); } }
        }

        private double _knifeOffset;
        [WatchValue("对刀偏差值")]
        /// <summary>
        /// 对刀偏差值
        /// </summary>
        public double KnifeOffset
        {
            get { return _knifeOffset; }
            set { if (_knifeOffset != value) { _knifeOffset = value; OnPropertyChanged(nameof(KnifeOffset)); } }
        }

        private double _laserToZ1Offset;
        [WatchValue("激光与刺晶头偏差")]
        /// <summary>
        /// 激光与刺晶头偏差
        /// </summary>
        public double LaserToZ1Offset
        {
            get { return _laserToZ1Offset; }
            set { if (_laserToZ1Offset != value) { _laserToZ1Offset = value; OnPropertyChanged(nameof(LaserToZ1Offset)); } }
        }

        private double _caliZHeightPositionX;
        [WatchValue("激光打到对刀仪上X坐标")]
        /// <summary>
        /// 激光打到对刀仪上X坐标
        /// </summary>
        public double CaliZHeightPositionX
        {
            get { return _caliZHeightPositionX; }
            set { if (_caliZHeightPositionX != value) { _caliZHeightPositionX = value; OnPropertyChanged(nameof(CaliZHeightPositionX)); } }
        }

        private double _caliZHeightPositionY;
        [WatchValue("激光打到对刀仪上Y坐标")]
        /// <summary>
        /// 激光打到对刀仪上Y坐标
        /// </summary>
        public double CaliZHeightPositionY
        {
            get { return _caliZHeightPositionY; }
            set { if (_caliZHeightPositionY != value) { _caliZHeightPositionY = value; OnPropertyChanged(nameof(CaliZHeightPositionY)); } }
        }

        public double CaliZHeight;

        private double _caliZHeightPositionZ;
        [WatchValue("激光打到对刀仪上Z坐标")]
        /// <summary>
        /// 激光打到对刀仪上Z坐标
        /// </summary>
        public double CaliZHeightPositionZ
        {
            get { return _caliZHeightPositionZ; }
            set { if (_caliZHeightPositionZ != value) { _caliZHeightPositionZ = value; OnPropertyChanged(nameof(CaliZHeightPositionZ)); } }
        }

        private double _caliZHeightLaserReading;
        [WatchValue("激光打到对刀仪上时激光读数")]
        /// <summary>
        /// 激光打到对刀仪上时激光读数
        /// </summary>
        public double CaliZHeightLaserReading
        {
            get { return _caliZHeightLaserReading; }
            set { if (_caliZHeightLaserReading != value) { _caliZHeightLaserReading = value; OnPropertyChanged(nameof(CaliZHeightLaserReading)); } }
        }
    }

    public class CapHeightItem : PropertyChangedBase, IParameterItem
    {
        public IParameterItem Clone()
        {
            return this.MemberwiseClone() as IParameterItem;
        }

        private double _capInitialValue;
        /// <summary>
        /// 电容初始值
        /// </summary>
        [WatchValue("电容初始值")]
        public double CapInitialValue
        {
            get { return _capInitialValue; }
            set { if (_capInitialValue != value) { _capInitialValue = value; OnPropertyChanged(nameof(CapInitialValue)); } }
        }
    }

    public class TeachPoint : PropertyChangedBase, IParameterItem
    {
        private int _id = 1;
        public int Id
        {
            get { return _id; }
            set { if (_id != value) { _id = value; OnPropertyChanged(nameof(Id)); } }
        }

        private double _x = 0.0;
        public double X
        {
            get { return _x; }
            set { if (_x != value) { _x = value; OnPropertyChanged(nameof(X)); } }
        }

        private double _y = 0.0;
        public double Y
        {
            get { return _y; }
            set { if (_y != value) { _y = value; OnPropertyChanged(nameof(Y)); } }
        }

        private double _z = 5.0;
        public double Z
        {
            get { return _z; }
            set { if (_z != value) { _z = value; OnPropertyChanged(nameof(Z)); } }
        }

        private double _laserZ = 0.0;
        public double LaserZ
        {
            get { return _laserZ; }
            set { if (_laserZ != value) { _laserZ = value; OnPropertyChanged(nameof(LaserZ)); } }
        }

        private double _height = 0.0;
        public double Height
        {
            get { return _height; }
            set { if (_height != value) { _height = value; OnPropertyChanged(nameof(Height)); } }
        }

        private double _laserAndZHeightSum = 0.0;
        public double LaserAndZHeightSum
        {
            get { return _laserAndZHeightSum; }
            set { if (_laserAndZHeightSum != value) { _laserAndZHeightSum = value; OnPropertyChanged(nameof(LaserAndZHeightSum)); } }
        }

        private int _row = 1;

        public int Row
        {
            get { return _row; }
            set { SetAndNotify(ref _row, value); }
        }
        private int _column = 1;

        public int Column
        {
            get { return _column; }
            set { SetAndNotify(ref _column, value); }
        }

        private int _regionIndex;

        public int RegionIndex
        {
            get { return _regionIndex; }
            set { SetAndNotify(ref _regionIndex, value); }
        }

        public TeachPoint(double x, double y)
        {
            _x = x;
            _y = y;
        }
        public TeachPoint()
        {

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