using FLY.Thick.Base.Common;
using Misc;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;


namespace FLY.Thick.FilmCasting.WebAPI.Controllers
{
    /// <summary>
    /// AD盒
    /// </summary>
    public class FlyAdController : ApiController
    {
        FLY.Thick.Base.IService.IFlyADService flyADService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyADService"></param>
        public FlyAdController(
            FLY.Thick.Base.IService.IFlyADService flyADService
            )
        {
            this.flyADService = flyADService;
        }



        /// <summary>
        /// 获取服务的参数及状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Data GetValue()
        {

            return new Data()
            {
                PosOfGrid = flyADService.PosOfGrid,
                MotorType = flyADService.MotorType,
                Ratio01 = flyADService.Ratio01,
                Ratio02 = flyADService.Ratio02,
                PosOffset = flyADService.PosOffset,
                JogVelocity = flyADService.JogVelocity,
                ADLag = flyADService.ADLag,
                HardwareVersion = flyADService.HardwareVersion
            };
        }


        /// <summary>
        /// 设置服务参数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public void SetValue([FromBody] Data data)
        {
            if (data.PosOfGrid != null)
                flyADService.PosOfGrid = (UInt16)data.PosOfGrid;

            if (data.MotorType != null)
                flyADService.MotorType = (MOTORTYPE)data.MotorType;
            if (data.Ratio01 != null)
                flyADService.Ratio01 = (UInt16)data.Ratio01;
            if (data.Ratio02 != null)
                flyADService.Ratio02 = (UInt16)data.Ratio02;
            if (data.PosOffset != null)
                flyADService.PosOffset = (Int16)data.PosOffset;
            if (data.JogVelocity != null)
                flyADService.JogVelocity = (UInt32)data.JogVelocity;
            if (data.ADLag != null)
                flyADService.ADLag = (int)data.ADLag;
            if (data.HardwareVersion != null)
                flyADService.HardwareVersion = (int)data.HardwareVersion;
        }
        /// <summary>
        /// 应用 & 保存
        /// </summary>
        public void Apply() 
        {
           flyADService.Apply();
        }
        /// <summary>
        /// 获取grid
        /// </summary>
        [HttpPost]
        public async Task<GridInfo> GetGrid([FromBody] GetGridRequest request) 
        {
            var tcs = new TaskCompletionSource<GridInfo>();

            flyADService.GetGrid(request.direction, (asyncContext, retData) =>
            {
                var response = retData as GridInfo;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }
        /// <summary>
        /// 
        /// </summary>
        public class GetGridRequest 
        {
            /// <summary>
            /// 方向
            /// </summary>
            public Misc.DIRECTION direction;
        }

        /// <summary>
        /// 
        /// </summary>
        public class Data
        {
            /// <summary>
            /// pos / grid
            /// </summary>
            public UInt16? PosOfGrid { get; set; }

            /// <summary>
            /// 电机类型
            /// </summary>
            public MOTORTYPE? MotorType { set; get; }

            /// <summary>
            /// 脉冲比例 分母(电机脉冲)
            /// </summary>
            public UInt16? Ratio01 { get; set; }
            
            /// <summary>
            /// 脉冲比例 分子(编码器脉冲)
            /// </summary>
            public UInt16? Ratio02 { get; set; }

            /// <summary>
            /// 脉冲平移(0位处脉冲值)
            /// </summary>
            public Int16? PosOffset { set; get; }

            /// <summary>
            /// 手动速度 pps
            /// </summary>
            public UInt32? JogVelocity { set; get; }


            /// <summary>
            /// AD滞后 ms
            /// </summary>
            public int? ADLag { get; set; }
            
            /// <summary>
            /// 硬件版本
            /// </summary>
            public int? HardwareVersion { get; set; }
        }
    }
}
