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

using System.ComponentModel;

using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using Misc;

namespace FLY.Thick.Base.Server
{
    public class GM_Fix : IGageMode, IFixService
    {
        #region IGageMode
        public CTRL_STATE GMState
        {
            get { return CTRL_STATE.FIX; }
        }

        public bool IsRunning { get; protected set; }

        #endregion
        /// <summary>
        /// 闲置时（IsRunning==false） 忽略数据
        /// </summary>
        public bool IsIgnoreDataWhenIdle { get; set; } = true;
        protected FlyADBase.FlyAD7 mFlyAD;
        protected DynArea mDynArea;
        protected AD2ThickHandler AD2Thick;

        public GM_Fix(FlyADBase.FlyAD7 flyad) 
        {
            mFlyAD = flyad;
            flyad.TimeGridEvent += new FlyADBase.TimeGridEventHandler(flyad_TimeGridEvent);
        }
        public void Init(DynArea dynarea, AD2ThickHandler func_ad2thick)
        {

            mDynArea = dynarea;

            AD2Thick = func_ad2thick;
        }
        /// <summary>
        /// 1s 的AD值，就只是用来显示到主界面左上面角而已 DynArea.AD
        /// </summary>
        List<int> Data1s = new List<int>();


        void flyad_TimeGridEvent(object sender, FlyADBase.TimeGridEventArgs e)
        {
            //if (IsIgnoreDataWhenIdle && !IsRunning)
            //    return;

            DateTime dt = e.Time;
            TimeSpan ts = e.Ts;
            List<int> data = new List<int>(e.Data);

            //1秒数据
            Data1s.AddRange(data);
            if (TimeSpan.FromTicks(Data1s.Count * ts.Ticks) >= TimeSpan.FromSeconds(1)) 
            {
                int ad = (int)Data1s.Average();
                Data1s.Clear();

                mDynArea.AD = ad;
                mDynArea.Thick = AD2Thick(ad);
            }


            //转换为thick
            List<double> thicks = new List<double>();
            foreach(int ad in data)
            {
                double thick;
                int t = AD2Thick(ad);
                if (Misc.MyBase.ISVALIDATA(t))
                    thick = t / 100.0;
                else
                    thick = double.NaN;
                thicks.Add(thick);
            }

            //需要限制大小，push 不能太大
            //分拆为多个包
            int size = 200;
            while (data.Count()>0)
            {
                int cnt = (data.Count() > size) ? size : data.Count();
                int[] ADs2 = data.Take(cnt).ToArray();
                double[] thicks2 = thicks.Take(cnt).ToArray();

                data.RemoveRange(0, cnt);
                thicks.RemoveRange(0, cnt);

                FixEvent?.Invoke(this,
                    new FixEventArgs()
                    {
                        time = dt,
                        ts = ts,
                        ADs = ADs2,
                        thicks = thicks2
                    });
                dt += TimeSpan.FromTicks(cnt * ts.Ticks);
            }
        }

        #region IGageMode
        public bool Start()
        {
            IsRunning = true;
            return true;
        }

        public void Stop()
        {
            IsRunning = false;
        }

        #endregion


        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;


        event FixEventHandler FixEvent;
        /// <summary>
        /// 注册定点数据事件 FixEvent += handler;
        /// </summary>
        /// <param name="handler"></param>
        public void RegistTimeGridEvent(FixEventHandler handler)
        {
            FixEvent += handler;
        }

        /// <summary>
        /// 取消注册定点数据事件 FixEvent -= handler;
        /// </summary>
        /// <param name="handler"></param>
        public void UnRegistTimeGridEvent(FixEventHandler handler)
        {
            FixEvent -= handler;
        }
    }
}
