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

namespace FLY.Simulation.Blowing
{
    public class GageAD:ISimulationGageAD
    {
        /// <summary>
        /// 1脉冲 = ? mm
        /// </summary>
        public double Mmpp { get; set; } = 0.0943;

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

        public Blowing mBlowing;

        /// <summary>
        /// 膜宽,mm
        /// </summary>
        public int FilmWidth;
        /// <summary>
        /// 膜开始位置, mm
        /// </summary>
        public int FilmBegin;

        /// <summary>
        /// 传感器直径,单位mm
        /// </summary>
        public int SenserWidth;



        CurveCollection curve;
        
        public GageAD() 
        {
            curve = new CurveCollection();

            mBlowing = new Blowing();

            FilmWidth = (int)mBlowing.FilmWidth;
            FilmBegin = 300;
            SenserWidth = 30;
        }

        public void OnPoll(DateTime now) 
        {
            //NewAirDatas();
            mBlowing.OnPoll(now);

        }
        public int GetAD(int mm) 
        {
            return GetAD_1(mm);
            //return GetAD_2(mm);
        }
        int GetAD_1(int mm) 
        {                
            Random r = new Random();
            int data = r.Next(30*2) - 30;// 单位 0.01um, ±0.3um波动

            if ((mm >= FilmBegin) && (mm<(FilmBegin+FilmWidth))) 
            {
                int position_mm = mm-FilmBegin;
                FilmData fd = mBlowing.GetData(position_mm);
                if (fd != null)
                {
                    data += fd.data1 + fd.data2;
                }
            }

            //求平均值
            double thick = data / 100.0;
            return curve.Value2Ad(thick, AD2ValueFlag.NoRevised);
        }
        int GetAD_2(int mm)
        {
            int[] datas = new int[SenserWidth];

            for (int i = 0; i < SenserWidth; i+=5)
            {
                datas[i] = GetAD_1(mm - SenserWidth / 2 + i);
            }
            //求平均值
            return (int)datas.Average();
        }
        /// <summary>
        /// 获取输入口
        /// </summary>
        /// <returns></returns>
        public UInt16 GetInput() 
        {
            UInt16 istatus = 0x0fff;
            if (!mBlowing.IsShieldI9)
            {
                if (mBlowing.CheckLimit0())
                    Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RENZIJIA_0);
            }

            if (mBlowing.CheckLimit1())
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RENZIJIA_1);


            if (mBlowing.CheckOrg())
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RENZIJIA_ORG);


            if (mBlowing.CheckRoll())
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RSENSOR);

            return istatus;
        }


        public int GetPosition2()
        {
            double p = mBlowing.GlobalAngle / 360;
            return (int)(mBlowing.PosOfR * p);
        }
        public int GetSpeed2() 
        {
            return (int)(mBlowing.PosOfR * mBlowing.CurrAngleVelocity / 360);
        }

        public void SetOutput(ushort output)
        {
            
        }
    }
}