using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlyAd2021.Inc
{
    /// <summary>
    /// 实现基本的AD盒2021版 基本实现
    /// </summary>
    public interface IFlyAd2021Base : INotifyPropertyChanged
    {
        #region 测量速度
        bool IsMeasuring { get; }

        /// <summary>
        ///  通讯速度 byte/s
        /// </summary>
        double CommSpeed { get; }

        /// <summary>
        /// 传输速度 单位 pack/s
        /// </summary>
        double PackSpeed { get; }

        /// <summary>
        /// 开始测量速度
        /// </summary>
        void StartMeasure();

        /// <summary>
        ///结束测量速度
        /// </summary>
        void StopMeasure();
        #endregion



        /// <summary>
        /// 包出错次数
        /// </summary>
        int ErrCnt { get; }

        /// <summary>
        /// 连接成功；
        /// 当命令多次发送失败，IsConnected = false
        /// </summary>
        bool IsConnected { get; }

        /// <summary>
        /// 在接收事件中调用；
        /// 全部接收事件 及 超时事件也是在这里触发
        /// </summary>
        /// <param name="recBuf"></param>
        void RecMsg(byte[] recBuf);
        /// <summary>
        /// 获取 发送队列 第1条msg
        /// </summary>
        /// <returns></returns>
        byte[] GetSendMsg();

       
        /// <summary>
        /// 数据推送事件
        /// </summary>
        event PushDataEventHandler PushDataEvent;
        #region IOComm IO指令

        [CallBack(typeof(UInt16))]
        void GetIn(CallBackHandler asyncDelegate, object asyncContext);


        [CallBack(typeof(UInt16))]
        void GetOut(CallBackHandler asyncDelegate, object asyncContext);

        [CallBack(typeof(Int32))]
        void GetEnc1(CallBackHandler asyncDelegate, object asyncContext);

        [CallBack(typeof(Int32))]
        void GetEnc2(CallBackHandler asyncDelegate, object asyncContext);

        [CallBack(typeof(GetEncAll_Reponse))]
        void GetEncAll(CallBackHandler asyncDelegate, object asyncContext);

        void SetOutPorts(UInt16 mask, UInt16 value, CallBackHandler asyncDelegate, object asyncContext);
        #endregion

        #region RunComm 运行指令 SetRunParam

        void SetRunParamV(UInt32 velocity, CallBackHandler asyncDelegate, object asyncContext);

        void SetRunParamSV(UInt32 sv, CallBackHandler asyncDelegate, object asyncContext);

        void SetRunParamAccTime(UInt32 accTime, CallBackHandler asyncDelegate, object asyncContext);

        void SetRunParamDecTime(UInt32 decTime, CallBackHandler asyncDelegate, object asyncContext);

        void SetRunParamHSpd1(UInt32 homespd1, CallBackHandler asyncDelegate, object asyncContext);

        void SetRunParamHSpd2(UInt32 homespd2, CallBackHandler asyncDelegate, object asyncContext);
        #endregion

        #region RunComm 运行指令 GetRunParam
        void GetRunParamV(CallBackHandler asyncDelegate, object asyncContext);

        void GetRunParamSV(CallBackHandler asyncDelegate, object asyncContext);

        void GetRunParamAccTime(CallBackHandler asyncDelegate, object asyncContext);

        void GetRunParamDecTime(CallBackHandler asyncDelegate, object asyncContext);

        void GetRunParamHSpd1(CallBackHandler asyncDelegate, object asyncContext);

        void GetRunParamHSpd2(CallBackHandler asyncDelegate, object asyncContext);

        #endregion

        #region RunComm 运行指令

        [CallBack(typeof(GetRunResult_Reponse))]
        void GetRunResult(CallBackHandler asyncDelegate, object asyncContext);

        void Forw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext);

        void Backw(Int32 serial, CallBackHandler asyncDelegate, object asyncContext);
        void Org(Int32 serial, CallBackHandler asyncDelegate, object asyncContext);

        void RunTo(Int32 serial, int targetPos, CallBackHandler asyncDelegate, object asyncContext);
        
        void Stop(CallBackHandler asyncDelegate, object asyncContext);

        void EStop(CallBackHandler asyncDelegate, object asyncContext);
        #endregion

        #region 同步运行指令
        void SyncRun_SetHShift(int hShift, CallBackHandler asyncDelegate, object asyncContext);
        void SyncRun_SetVShift(int vOShift, CallBackHandler asyncDelegate, object asyncContext);

        void SyncRun_Start(CallBackHandler asyncDelegate, object asyncContext);
        void SyncRun_Stop(CallBackHandler asyncDelegate, object asyncContext);
        void SyncRun_Clear(CallBackHandler asyncDelegate, object asyncContext);

        /// <summary>
        /// 同步扫描至;
        /// D+0xE0+开始主轴位置+结束主轴位置+结束横向脉冲位置（逻辑位置）+脉冲开关（1B）+命令识标号（4B）
        /// </summary>
        /// <param name="pos2_begin"></param>
        /// <param name="pos2_end"></param>
        /// <param name="pos1lc"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
        void SyncRun_RunAtLC(int pos2_begin, int pos2_end, int pos1lc, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext);


        /// <summary>
        /// 位于队列头时运行，归零；
        /// D+0xE1+命令识标号（4B）
        /// </summary>
        /// <param name="marker"></param>
        void SyncRun_Origin(UInt32 serial, CallBackHandler asyncDelegate, object asyncContext);


        /// <summary>
        /// 位于队列头时运行，以速度运行至物理位置；
        /// D+0xE2+横向脉冲位置（4B：int32，物理位置）+速度（4B：int32）+脉冲开关（1B）+命令识标号（4B）
        /// </summary>
        /// <param name="pos1"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
        void SyncRun_RunTo(int pos1, UInt32 velocity, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext);


        /// <summary>
        /// 位于队列头时运行，以速度运行至逻辑位置；
        /// D+0xE3+横向脉冲位置（4B：int32，逻辑位置）+速度（4B：int32）+脉冲开关（1B）+命令识标号（4B）
        /// </summary>
        /// <param name="pos1lc"></param>
        /// <param name="velocity"></param>
        /// <param name="hasDataGrid"></param>
        /// <param name="marker"></param>
        void SyncRun_RunToLC(int pos1lc, UInt32 velocity, UInt32 serial, CallBackHandler asyncDelegate, object asyncContext);


        /// <summary>
        /// 获取队列长度
        /// </summary>
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        void SyncRun_GetListCount(CallBackHandler asyncDelegate, object asyncContext);
        #endregion

        #region SysParamComm 系统参数指令
        //[CallBack(typeof(UInt32))]
        //void GetSysTick(CallBackHandler asyncDelegate, object asyncContext);
        //void InitArea0()
        #endregion
        #region GetSysParam 读运行参数

        #endregion

        #region SetSysParam 设置运行参数

        #endregion
    }


    public class PushDataEventArgs : EventArgs
    {
        public byte SysTick { get; set; }
        public UInt16 AD { get; set; }
        public UInt16? ENC1 { get; set; }
        public UInt16? ENC2 { get; set; }
        public UInt16? In { get; set; }
        public UInt16? InChange { get; set; }
        public UInt16? Out { get; set; }

        public PushDataEventArgs()
        {

        }
    }
    public delegate void PushDataEventHandler(object sender, PushDataEventArgs e);

    /// <summary>
    /// 异常不回复
    /// </summary>
    /// <param name="asyncContext"></param>
    /// <param name="retData"></param>
    public delegate void CallBackHandler(object asyncContext, object retData);


    public sealed class CallBackAttribute : Attribute
    {
        public Type RetType;
        public CallBackAttribute(Type type) 
        {
            RetType = type;
        }
    }

    public class GetEncAll_Reponse
    {
        public int pos1;
        public int pos2;
    }
    public class GetRunResult_Reponse
    {
        public byte result;
        public UInt32 serial;
    }
}
