using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace FLY.Simulation.HeaderAndTailer
{
    public class GageAD : ISimulationGageAD, INotifyPropertyChanged
    {
        CurveCollection curve;
        List<int> datas_vertical = new List<int>();//纵向AD数据,1.28ms 一个数据

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

        /// <summary>
        /// 1脉冲 = ? mm
        /// </summary>
        double mmpp = 0.1133;
        public double Mmpp
        {
            get { return mmpp; }
            set
            {
                mmpp = value;
            }
        }

        double filmvelocity = 18;
        /// <summary>
        /// 膜速度 m/min
        /// </summary>
        public double FilmVelocity
        {
            get
            {
                return filmvelocity;
            }
            set
            {
                if (filmvelocity != value)
                {
                    filmvelocity = value;
                    NotifyPropertyChanged("FilmVelocity");
                }
            }
        }

        double filmlength = 0;
        /// <summary>
        /// 膜长 m
        /// </summary>
        public double FilmLength
        {
            get
            {
                return filmlength;
            }
            set
            {
                if (filmlength != value)
                {
                    filmlength = value;
                    NotifyPropertyChanged("FilmLength");
                }
            }
        }

        bool isRunning = true;
        /// <summary>
        /// 运转中
        /// </summary>
        public bool IsRunning
        {
            get
            {
                return isRunning;
            }
            set
            {
                if (isRunning != value)
                {
                    isRunning = value;
                    NotifyPropertyChanged("IsRunning");
                }
            }
        }
        public GageAD()
        {
            curve = new CurveCollection("ht_curve.xml");
            Load();
        }

        public int GetAD(int position)
        {
            int idx = (int)datas_vertical_index;
            if (idx < 0)
                idx = 0;
            else if (idx >= datas_vertical.Count())
                idx = datas_vertical.Count() - 1;
            int ad = datas_vertical[idx];


            //Random r = new Random();
            //int data = r.Next(50) - 25;

            //thick += data;

            return ad;// curve.Value2AD(thick, CurveCollection.AD2ValueFlag.NoRevised);
        }


        DateTime dtlast = DateTime.MinValue;
        double datas_vertical_index = 0;
        public void OnPoll(DateTime now)
        {
            if (dtlast == DateTime.MinValue) 
            {
                dtlast = now;
                return;
            }


            if (IsRunning) 
            {
                double min = (now - dtlast).TotalMinutes;
                FilmLength += min * FilmVelocity;

                double ms = (double)((now - dtlast).Ticks) / TimeSpan.TicksPerMillisecond;
                datas_vertical_index += ms/1.28;

                if (datas_vertical_index >= datas_vertical.Count()) 
                {
                    datas_vertical_index -= datas_vertical.Count();
                }
            }

            
            dtlast = now;
        }

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

            double p = FilmLength * 1000 % RLen;
            if(p<RLen/10)
                Misc.MyBase.CLEARBIT(ref istatus, 10);

            return istatus;
        }

        void LoadVertical()
        {
            datas_vertical.Clear();
            using (StreamReader sr = new StreamReader(@"headerAndTailer\datas_fix_1.28.csv"))
            {
                string header = sr.ReadLine();//时间(ms),数据

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

                    int thick = (int)(double.Parse(ss[1]) * 100);
                    int ad = curve.ValueAD(thick);
                    datas_vertical.Add(ad);
                }
                sr.Close();
            }
        }

        void Load()
        {

            LoadVertical();
        }


        public int GetPosition2()
        {
            return (int)(FilmLength * 1000 / Mmpp2);

        }
        public int GetSpeed2()
        {
            if (IsRunning)
                return (int)(FilmVelocity * 1000 / 60 / Mmpp2);
            else
                return 0;
        }

        protected void NotifyPropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
