using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
namespace FLY.Simulation.Flyad7.OBJProxy
{
    class PUSH_DATA:FObj
    {
        DateTime push_dt = DateTime.MinValue;
        DateTime ad1_dt;
        DateTime ad2_dt;
        
        FLYAD7 Data;
        int position;
        int speed;
        int position2;
        int speed2;
        UInt16 istatus;
        UInt16 ostatus;
        
        
        public PUSH_DATA(int objsys_idx, FLYAD7 data):base(objsys_idx)
        {
            ID = FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.ID;
            Data = data;
            PollModule.Current.Poll_Config(
                PollModule.POLL_CONFIG.ADD,
                new PollModule.PollHandler(OnPoll),
                new TimeSpan((long)(TimeSpan.TicksPerMillisecond * 1.28)));


            Data.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Data_PropertyChanged);

            Data.mPosGrid.PushEvent += new FLYAD7.PosGrid.PushEventHandler(mPosGrid_PushEvent);

            Data.TimeGridEvent += new FlyADBase.TimeGridEventHandler(Data_TimeGridEvent);
        }

        void Data_TimeGridEvent(object sender, FlyADBase.TimeGridEventArgs e)
        {
            CurrObjSys.PushObjInfoEx(
                this,
                FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_TIMEGRID,
                new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushTGrid_2()
                {
                    data = e.Data,
                    systick = Data.GetSysTick(e.Time),
                }.ToBytes());
        }


        void mPosGrid_PushEvent(bool isMini, Misc.DIRECTION dir, int grid_start, int[] data)
        {
            FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid_2 p = new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushGrid_2();
            p.grid_start = grid_start;
            p.direction = dir;
            p.data = data;
            p.systick = Data.GetSysTick(Data.Now);
            p.marker = Data.Marker;
            if (isMini)
                CurrObjSys.PushObjInfoEx(this, FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_MINIGRID,
                        p.ToBytes());
            else
                CurrObjSys.PushObjInfoEx(this, FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_GRID,
                        p.ToBytes());
        }

 
        void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Data.IStatus)) 
            {
                ostatus = Data.OStatus;
                UInt16 inchange = (UInt16)(istatus ^ Data.IStatus);
                istatus = Data.IStatus;

                CurrObjSys.PushObjInfoEx(this, FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_IO,
                    new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushIO_2()
                    {
                        inChange = inchange,
                        istatus = Data.IStatus,
                        ostatus = Data.OStatus,
                        systick = Data.GetSysTick(),
                        pos1 = Data.Position,
                        pos2 = Data.Position2
                    }.ToBytes());
            }
            else if ((e.PropertyName == nameof(Data.DriveStatus))||(e.PropertyName == nameof(Data.Marker)))
            {
                CurrObjSys.PushObjInfoEx(this, FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_STATUS,
                    new FLYAD7_OBJ_INTERFACE.DRIVE_MAN_INTERFACE.Pack_DriveState_2
                    {
                        order = Data.DriveOrder,
                        status = Data.DriveStatus,
                        marker = Data.Marker,
                        systick = Data.GetSysTick(),
                        mode = 1
                    }.ToBytes());
            }
        }

        

        void OnPoll() 
        {
            DateTime dt=Data.Now;
            if (push_dt == DateTime.MinValue)
            {
                push_dt = dt;
                return;
            }
            if (dt.Subtract(push_dt).TotalSeconds >= 0.1) 
            {
                TimeSpan ts = dt - push_dt;
                push_dt = push_dt.AddSeconds(0.1);
                speed = (int)((Data.Position - position) / ts.TotalSeconds);
                if((position != Data.Position)||(speed!=Data.Speed))
                {
                    position = Data.Position;
                    Data.Speed = speed;
                    Data.PosForShow = Data.Position;

                    CurrObjSys.PushObjInfoEx(this, FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS1,
                        new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos1_2()
                        {
                            pos1 = Data.Position,
                            speed1 = Data.Speed,
                            systick = Data.GetSysTick()
                        }.ToBytes());

                }
                if ((position2 != Data.Position2) || (speed2 != Data.Speed2))
                {
                    position2 = Data.Position2;
                    speed2 = Data.Speed2;
                    Data.Pos2ForShow = Data.Position2;
                    CurrObjSys.PushObjInfoEx(this, FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_POS2,
                        new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushPos2_2()
                        {
                            pos2 = Data.Position2,
                            speed2 = Data.Speed2,
                            systick = Data.GetSysTick()
                        }.ToBytes());

                }
            }
            if (dt.Subtract(ad1_dt).Milliseconds >= 1000)
            {
                ad1_dt = ad1_dt.AddMilliseconds(1000);
                CurrObjSys.PushObjInfoEx(this, FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.PUSH_AD1,
                    new FLYAD7_OBJ_INTERFACE.PUSH_DATA_INTERFACE.Pack_PushAD1()
                    {
                        AD = Data.ADForShow,
                    }.ToBytes());
                
            }
        }
    }
}
