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

namespace FLY.Simulation.Casting
{
    public class GageAD : ISimulationGageAD, INotifyPropertyChanged
    {
        CurveCollection curve;
        //这是整个机架的数据
        List<int> datas_horizontal = new List<int>();//横向AD数据, 1mm 一个AD值

        /// <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>
        public double Mmpp { get; set; } = 0.0945;

        /// <summary>
        /// 膜速度 m/min
        /// </summary>
        public double FilmVelocity { get; set; } = 23;

        /// <summary>
        /// 膜长 m
        /// </summary>
        public double FilmLength { get; set; }

        /// <summary>
        /// 运转中
        /// </summary>
        public bool IsRunning { get; set; } = true;

        public GageAD()
        {
            curve = new CurveCollection("curve.xml");
            Load();
        }

        public int GetAD(int position)
        {
            int idx = (int)(position * Mmpp);
            if (idx < 0)
                idx = 0;
            else if (idx >= datas_horizontal.Count())
                idx = datas_horizontal.Count() - 1;
            int ad = datas_horizontal[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;
        public void OnPoll(DateTime now)
        {
            if (dtlast == DateTime.MinValue) 
            {
                dtlast = now;
                return;
            }


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

            
            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 LoadHorizontal()
        {
            datas_horizontal.Clear();
            using (StreamReader sr = new StreamReader(@"casting\datas_horizontal.csv"))
            {
                string header = sr.ReadLine();//位置(mm),数据

                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_horizontal.Add(ad);
                }
                sr.Close();
            }
        }

        void Load()
        {

            LoadHorizontal();
        }


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

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

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
