using MainShell.Common.Extension;
using MainShell.DeviceMaintance.Model;
using MainShell.Hardware;
using MainShell.Parameter;
using MainShell.Recipe.Models;
using MaxwellFramework.Core.Interfaces;
using MwFramework.Device;
using MwFramework.ManagerService;
using MXJM.Parameter.Maintance;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainShell.HeightMeasure.Model
{
    public class DiastimeterHeightMeasure : IHeightMeasure
    {
        
        public HeightBaseItem HeightBaseItem { get; set; }

        public DiaHeightItem DiaHeightItem { get; set; }
        public CapHeightItem CapHeightItem { get; set; }

        private HardwareManager _hardwareManager;
        private readonly IDiastimeter _diastimeter;
        private readonly IParamList _paramlist;
        public DiastimeterHeightMeasure(GlobalParameterContext globalParam, HardwareManager hardwareManager, IParameterManager paramList)
        {
            _hardwareManager = hardwareManager;
            _diastimeter = _hardwareManager.Diastimeter;
            _paramlist = paramList as IParamList;
            DiaHeightItem = globalParam.HeightBaseSetting.DiaHeightItem;
            HeightBaseItem = globalParam.HeightBaseSetting.HeightBaseItem;

        }

        public double GetDistance()
        {
            _hardwareManager.Diastimeter.GetLastSamples(out double Distance);
            if (double.IsNaN(Distance))
            {
                throw new Exception("测距仪反馈数据为NAN，请检查测距仪状态");
            }
            return Distance;
        }

        public void CalculateHeight()
        {
            double glassHeightAverage = HeightBaseItem.GlassHeightMeasureList.Average(m => m.Height);
            double glassZAverage = HeightBaseItem.GlassHeightMeasureList.Average(m => m.Z);
            double waferHeightAverage = HeightBaseItem.WaferHeightMeasureList.Average(m => m.Height);
            double waferZAverage = HeightBaseItem.WaferHeightMeasureList.Average(m => m.Z);
            HeightBaseItem.GlassZHeight = glassZAverage + glassHeightAverage;
            HeightBaseItem.WaferZHeight = waferZAverage + waferHeightAverage;

            if (_paramlist == null)
            {
                throw new InvalidOperationException("IParamList 未初始化，无法计算测高标定参数。");
            }

            var needleZCalibrationItem = _paramlist.GetParameter<NeedleCalibrationSetting>().NeedleZCalibrationItem;
            DiaHeightItem.NeedleTouchHeight = needleZCalibrationItem.NeedleTouchHeight;
            DiaHeightItem.KnifeOffset = needleZCalibrationItem.KnifeOffset;

            //Gap2
            HeightBaseItem.FromDieToPadDistance = GetGap2(HeightBaseItem.DieThickness, HeightBaseItem.WaferFilmThickness);

            //激光与刺晶头偏差
            DiaHeightItem.LaserToZ1Offset = DiaHeightItem.NeedleTouchHeight -
                (DiaHeightItem.CaliZHeightLaserReading + DiaHeightItem.CaliZHeightPositionZ) + DiaHeightItem.KnifeOffset;

            //刺晶头接触到方片高度
            HeightBaseItem.NeedleTouchWaferZHeight = HeightBaseItem.WaferZHeight + DiaHeightItem.LaserToZ1Offset;
        }

        public double GetGap2(double dieThickness, double waferFilmThickness)
        {
            double height = 0.0;
            height = HeightBaseItem.WaferZHeight - HeightBaseItem.GlassZHeight
                - dieThickness - waferFilmThickness;
            return Math.Abs(height);
        }

        //public double GetZStageTheoryWorkHeight(double glassThickness, double carrierThickness, WaferRecipeContentItem currentWaferRecipe = null)
        //{
        //    double calibWaferToStageDis = HeightBaseItem.WaferFilmThickness + HeightBaseItem.DieThickness +
        //        HeightBaseItem.FromDieToPadDistance + HeightBaseItem.GlassThickness + HeightBaseItem.CarrierThickness;

        //    double actualWaferToStageDis = currentWaferRecipe.WaferFilmThickness + currentWaferRecipe.DieThickness +
        //        currentWaferRecipe.FromDieToPadDistance + glassThickness + carrierThickness;

        //    double height = HeightBaseItem.StageDatumHeight + calibWaferToStageDis - actualWaferToStageDis;
        //    return height;
        //}

        //public double GetZ1WorkHeight(BondingCraftRecipeContentItem currentWaferRecipe = null)
        //{
        //    double height = 0.0;

        //    //2.针尖的工作高度
        //    height = HeightBaseItem.NeedleTouchWaferZHeight + currentWaferRecipe.Gap1;

        //    return height;
        //}
    }
}
