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

using System.ComponentModel;
using FLY.Thick.BulkDataModule;

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


namespace FLY.Thick.Base.Server
{
    public class GM_Fix : IGageMode, IFixService
    {
        #region IGageMode
        public CTRL_STATE GMState
        {
            get { return CTRL_STATE.FIX; }
        }
        private bool isrunning = false;
        public bool IsRunning
        {
            get { return isrunning; }
            protected set {
                if (isrunning != value)
                {
                    isrunning = value;
                    NotifyPropertyChanged("IsRunning");
                }
            }
        }
        #endregion
        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值
        /// </summary>
        List<int> Data1s = new List<int>();


        void flyad_TimeGridEvent(object sender, FlyADBase.TimeGridEventArgs e)
        {
            DateTime dt = e.Time;
            TimeSpan ts = e.Ts;
            int[] data2 = e.Data;

            int[] data = (int[])data2.Clone();

            Data1s.AddRange(data);
            if ((Data1s.Count * ts.Ticks) / TimeSpan.TicksPerSecond >= 1) 
            {
                int ad = Misc.MyMath.Avg(Data1s.ToArray());
                Data1s.Clear();

                mDynArea.AD = ad;

                mDynArea.Thick = AD2Thick(ad);
            }
            //转换为thick
            for (int i = 0; i < data.Length; i++)
                data[i] = AD2Thick(data[i]);
            
            if (TimeGridEvent != null)
                TimeGridEvent(this, new TimeGridEventArgs() { time = e.Time, ts = e.Ts, data = data });
        }

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

        public void Stop()
        {
            IsRunning = false;
        }

        #endregion


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


        event TimeGridEventHandler TimeGridEvent;
        public void RegistTimeGridEvent(TimeGridEventHandler handler)
        {
            TimeGridEvent += handler;
        }


        public void UnRegistTimeGridEvent(TimeGridEventHandler handler)
        {
            TimeGridEvent -= handler;
        }
    }
}
