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

namespace FLY.Simulation.Coating
{
    /// <summary>
    /// 电池涂布
    /// </summary>
    public class GageAD : ISimulationGageAD
    {
        public Coating mCoating;
        CurveCollection curve;
        /// <summary>
        /// 单位厚度
        /// </summary>
        List<double> Datas_GageInfo = new List<double>();//机架信息

        /// <summary>
        /// 编码器2 1脉冲 = ? mm    辊直径120mm，编码器 转1圈800脉冲
        /// </summary>
        const double Mmpp2 = 0.25;

        /// <summary>
        /// 膜开始位置 mm
        /// </summary>
        const int FilmBegin=200;

        /// <summary>
        /// 1脉冲 = ? mm
        /// </summary>
        public double Mmpp { get; set; } = 0.2;


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

        public GageAD() 
        {
            curve = new CurveCollection();
            mCoating = new Coating();
            Load();
        }

        public int GetAD(int mm)
        {
            if (mm >= Datas_GageInfo.Count())
            {
                mm = Datas_GageInfo.Count() - 1;
            }
            else if (mm < 0)
            {
                mm = 0;
            }

            double filmthick = 0;
            if (mm >= FilmBegin && mm < (FilmBegin + mCoating.FilmWidth))
            {
                filmthick = mCoating.GetData30m(mm - FilmBegin);
                if (filmthick < 0)
                    filmthick = 0;

                if ((mCoating.Datas_Horizontal[mm - FilmBegin].IsLight) || mCoating.VSignal)
                    mCoating.HSignal = true;
                else 
                    mCoating.HSignal = false;

                if ((mCoating.Datas_Horizontal[mm - FilmBegin].IsLight2) || mCoating.VSignal2)
                    mCoating.HSignal2 = true;
                else
                    mCoating.HSignal2 = false;
            }
            else 
            {
                mCoating.HSignal = false;
                mCoating.HSignal2 = false;
            }

            


            double thick = Datas_GageInfo[mm] + filmthick;

            return curve.Value2Ad(thick, AD2ValueFlag.NoRevised);
        }

        public void OnPoll(DateTime now)
        {
            mCoating.OnPoll(now);
        }

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

            if (mCoating.HSignal)
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_HSIGN);

            if (mCoating.HSignal2)
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_HSIGN2);

            if (mCoating.RSignal)
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_RSENSOR);

            if (mCoating.VSignal)
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_VSENSOR);

            if (mCoating.VSignal2)
                Misc.MyBase.CLEARBIT(ref istatus, IODefinition.IN_VSENSOR2);


            return istatus;
        }

        void Load_GageInfo()
        {
            Datas_GageInfo.Clear();
            using (StreamReader sr = new StreamReader("datas_gageinfo.csv"))
            {
                string header = sr.ReadLine();//标题 , 位置(mm), AD

                while (!sr.EndOfStream)
                {
                    string s = sr.ReadLine();
                    string[] ss = s.Split(',');

                    int pos_mm = int.Parse(ss[0]);
                    int ad = int.Parse(ss[1]);
                    //转为 thick

                    double thk = curve.AD2Value(ad, AD2ValueFlag.NoRevised);
                    Datas_GageInfo.Add(thk);
                }
                sr.Close();
            }
            TotalLength = Datas_GageInfo.Count();
        }

        void Load()
        {

            Load_GageInfo();
        }


        public int GetPosition2()
        {
            return (int)(mCoating.FilmLength*1000 / Mmpp2);
            
        }
        public int GetSpeed2() 
        {
            if (mCoating.DeviceState)
                return (int)(mCoating.FilmVelocity * 1000 / 60 / Mmpp2);
            else
                return 0;
        }

        public void SetOutput(ushort output)
        {
        }
    }
}
