using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;
using System.Windows.Threading;
using FlyADBase;
using FObjBase;

namespace Flyad7_WPF
{
    public class FlyADClientUI : INotifyPropertyChanged
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_ACCESS = 2;
        #endregion
        public DateTime Now { get; set; }



        public int Position { get; set; }


        public int Speed { get; set; }


        public int Position2 { get; set; }
        
        public int Speed2 { get; set; }


        public int AD { get; set; }


        private DispatcherTimer timer = new DispatcherTimer();
        private FlyAD7 flyad;
        public FlyADClientUI(FlyAD7 flyad)
        {
            Init(flyad);
        }
        void Init(FlyAD7 flyad)
        {
            this.flyad = flyad;
            timer.Interval = TimeSpan.FromSeconds(0.1);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            Position = flyad.Position;
            Position2 = flyad.Position2;
            AD = flyad.AD;
            Speed = flyad.Speed;
            Speed2 = flyad.Speed2;
            Now = flyad.Now;
        }
        public event PropertyChangedEventHandler PropertyChanged;

        class AsyncDelegateObj
        {
            public AsyncCBHandler asyncDelegate;
            public object asyncContext;
        }
        List<AsyncDelegateObj> setAccessRetList = new List<AsyncDelegateObj>();

        PropertyChangedEventHandler mFlyAD_PropertyChangedHandler = null;

        /// <summary>
        /// 设置授权码 返回类型typeof(AccessInfo)
        /// </summary>
        /// <param name="access"></param>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void SetAccess(byte[] access, AsyncCBHandler asyncDelegate, object asyncContext)
        {
            flyad.SetAccess(access);
            setAccessRetList.Add(new AsyncDelegateObj() { asyncDelegate = asyncDelegate, asyncContext = asyncContext });

            if (mFlyAD_PropertyChangedHandler == null)
            {
                mFlyAD_PropertyChangedHandler = new PropertyChangedEventHandler(delegate (object sender, PropertyChangedEventArgs e)
                {
                    if ((e.PropertyName == nameof(flyad.AreaStatus)) ||
                        (e.PropertyName == nameof(flyad.Code)) ||
                        (e.PropertyName == nameof(flyad.Surplus)) ||
                        (e.PropertyName == nameof(flyad.Access)) ||
                        (e.PropertyName == nameof(flyad.AreaRet)))
                    {
                        FObjBase.PollModule.Current.Poll_JustOnce(
                                new PollModule.PollHandler(delegate ()
                                {
                                    flyad.PropertyChanged -= mFlyAD_PropertyChangedHandler;
                                    AccessInfo ainfo = new AccessInfo()
                                    {
                                        status = (AREA_STATUS)flyad.AreaStatus,
                                        ret = (AREA_ERR)flyad.AreaRet,
                                        surplus = flyad.Surplus,
                                        code = flyad.Code,
                                        access = flyad.Access
                                    };
                                    for (int i = 0; i < setAccessRetList.Count(); i++)
                                    {
                                        setAccessRetList[i].asyncDelegate(setAccessRetList[i].asyncContext, ainfo);
                                    }
                                    setAccessRetList.Clear();
                                }), this, MARKNO_PUSH_ACCESS);
                    };
                });
            }
            flyad.PropertyChanged += mFlyAD_PropertyChangedHandler;
        }


        /// <summary>
        /// 设置初始化 返回类型typeof(AccessInfo)
        /// </summary>
        public void InitArea(AsyncCBHandler asyncDelegate, object asyncContext) 
        {
            flyad.InitArea();
            setAccessRetList.Add(new AsyncDelegateObj() { asyncDelegate = asyncDelegate, asyncContext = asyncContext });

            if (mFlyAD_PropertyChangedHandler == null)
            {
                mFlyAD_PropertyChangedHandler = new PropertyChangedEventHandler(delegate (object sender, PropertyChangedEventArgs e)
                {
                    if ((e.PropertyName == nameof(flyad.AreaStatus)) ||
                        (e.PropertyName == nameof(flyad.Code)) ||
                        (e.PropertyName == nameof(flyad.Surplus)) ||
                        (e.PropertyName == nameof(flyad.Access)) ||
                        (e.PropertyName == nameof(flyad.AreaRet)))
                    {
                        FObjBase.PollModule.Current.Poll_JustOnce(
                                new PollModule.PollHandler(delegate ()
                                {
                                    flyad.PropertyChanged -= mFlyAD_PropertyChangedHandler;
                                    AccessInfo ainfo = new AccessInfo()
                                    {
                                        status = (AREA_STATUS)flyad.AreaStatus,
                                        ret = (AREA_ERR)flyad.AreaRet,
                                        surplus = flyad.Surplus,
                                        code = flyad.Code,
                                        access = flyad.Access
                                    };
                                    for (int i = 0; i < setAccessRetList.Count(); i++)
                                    {
                                        setAccessRetList[i].asyncDelegate(setAccessRetList[i].asyncContext, ainfo);
                                    }
                                    setAccessRetList.Clear();
                                }), this, MARKNO_PUSH_ACCESS);
                    };
                });
            }
            flyad.PropertyChanged += mFlyAD_PropertyChangedHandler;
        }
    }

    /// <summary>
    /// 授权码包
    /// </summary>
    public class AccessInfo
    {
        /// <summary>
        /// 密码区状态
        /// </summary>
        public AREA_STATUS status;

        /// <summary>
        /// 操作结果
        /// </summary>
        public AREA_ERR ret;

        /// <summary>
        /// 序列码
        /// </summary>
        public byte[] code = new byte[7];

        /// <summary>
        /// 剩余时间
        /// </summary>
        public int surplus;

        /// <summary>
        /// 授权码
        /// </summary>
        public byte[] access = new byte[8];
    }
}
