using FLY.Thick.Base.Common;
using FLY.Thick.Base.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Simulation.Battery.RayLaser
{
    public class GageAd_Laser : ISimulationGageAD
    {
        CurveCollection curve;

        /// 编码器2 mm/p 胶轮周长200mm，编码器 转1圈800脉冲 正确应该0.25
        /// </summary>
        const double Mmpp2 = 0.24925;
        const int INVALID_AD = 8875;
        /// <summary>
        /// 编码器1 mm/p
        /// </summary>
        public double Mmpp { get; set; } = 0.2;

        public double VSignOffset { get; set; }

        /// <summary>
        /// 机架总长
        /// </summary>
        public int TotalLength { get; set; }

        GageAd gageAd;
        public GageAd_Laser()
        {

        }
        public void Init(GageAd gageAd)
        {
            this.gageAd = gageAd;
            curve = gageAd.curve_laser;
            VSignOffset = gageAd.VSignOffsetLaser;
            TotalLength = gageAd.TotalLength;

        }

        int GetAdMm(int mm, int mm_v_offset)
        {
            int posLength = (int)(gageAd.FilmPosition*1000 - VSignOffset + mm_v_offset);
            
            double value = gageAd.GetValue(true, mm, posLength, out GageAd.PosType posType);

            if (posType == GageAd.PosType.Hold)
            {
                return INVALID_AD;
            }
            else if (posType == GageAd.PosType.Air) {
                return INVALID_AD;
            }

            int ad = curve.Value2Ad(value, AD2ValueFlag.NoRevised);
            if (ad > 65535)
                ad = 65535;
            else if (ad < 0)
                ad = 0;

            return ad;
        }

        public int GetAD(int mm)
        {
            //需要对探头直径范围的数据求均值
            return GetAdMm(mm, 0);
        }

        public void OnPoll(DateTime now)
        {
            //不实现
        }

        public UInt16 GetInput()
        {
            UInt16 istatus = 0x0fff;

            if (gageAd.IsSyncLight)
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_SYNC);

            return istatus;
        }

        public int GetPosition2()
        {
            return 0;

        }
        public int GetSpeed2()
        {
            return 0;
        }

        public void SetOutput(ushort output)
        {

        }
    }
}
