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; } = 3000;

        public Blowing mBlowing;

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

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

        public int[] AirDatas;

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

            mBlowing = new Blowing();

            FilmWidth = (int)mBlowing.FilmWidth;
            FilmBegin = 300;
            SenserWidth = 30;
            AirDatas = new int[TotalLength];
            NewAirDatas();
        }
        void NewAirDatas() 
        {
            int a = 20;
            Random r = new Random();
            for (int i = 0; i < AirDatas.Count(); i++)
            {
                AirDatas[i] = r.Next(a) - a/2;
            }
        }
        public void OnPoll(DateTime now) 
        {
            //NewAirDatas();
            mBlowing.OnPoll(now);

        }
        public int GetAD(int mm) 
        {
            return GetAD_1(mm);
            //return GetAD_2(pos);
        }
        int GetAD_1(int mm) 
        {                
                Random r = new Random();
                int data = r.Next(50) - 25;

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

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

            for (int i = 0; i < SenserWidth; i++)
            {
                datas[i] = GetAD_1(pos - 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)
        {
            
        }
    }
}


