using FLY.Thick.Base.IService;
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>
    /// 边界查找
    /// </summary>
    public class BorderSearchController : ApiController
    {
        FLY.Thick.Base.IService.IBorderSearchService borderSearchService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="borderSearchService"></param>
        public BorderSearchController(
            FLY.Thick.Base.IService.IBorderSearchService borderSearchService
            )
        {
            this.borderSearchService = borderSearchService;
        }


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

            return new Data()
            {
                Enable = borderSearchService.Enable,
                TempADBySet = borderSearchService.TempADBySet,
                TempAD = borderSearchService.TempAD,
                TempRange = borderSearchService.TempRange,
                IsTempRangeByPercent = borderSearchService.IsTempRangeByPercent,
                TempRangePercent = borderSearchService.TempRangePercent,
                N = borderSearchService.N,
                SensorWidth = borderSearchService.SensorWidth,
                N2 = borderSearchService.N2,
                N3 = borderSearchService.N3,

                Border_Backw = borderSearchService.Border_Backw,
                Border_Forw = borderSearchService.Border_Forw,
                Border = borderSearchService.Border,
                Width = borderSearchService.Width,
                Mid = borderSearchService.Mid,
                UpdateTime = borderSearchService.UpdateTime
            };
        }

        /// <summary>
        /// 设置服务参数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public void SetValue([FromBody]Data data) 
        {
            if (data.Enable != null)
                borderSearchService.Enable = (bool)data.Enable;
            if (data.TempADBySet != null)
                borderSearchService.TempADBySet = (bool)data.TempADBySet;
            if (data.TempAD != null)
                borderSearchService.TempAD = (int)data.TempAD;
            if (data.TempRange != null)
                borderSearchService.TempRange = (int)data.TempRange;
            if (data.IsTempRangeByPercent != null)
                borderSearchService.IsTempRangeByPercent = (bool)data.IsTempRangeByPercent;
            if (data.TempRangePercent != null)
                borderSearchService.TempRangePercent = (double)data.TempRangePercent;
            if (data.N != null)
                borderSearchService.N = (int)data.N;
            if (data.SensorWidth != null)
                borderSearchService.SensorWidth = (int)data.SensorWidth;
            if (data.N2 != null)
                borderSearchService.N2 = (int)data.N2;
            if (data.N3 != null)
                borderSearchService.N3 = (int)data.N3;
        }


        /// <summary>
        /// 应该 及 保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public void Apply()
        {
            borderSearchService.Apply();
        }

        /// <summary>
        /// 获取混合后的横向数据；没有把横向留白剔除；只把纵向留白剔除。
        /// 数据单位 mm
        /// </summary>
        [HttpPost]
        public async Task<BorderSearchGetViewReponse> GetView() 
        {
            var tcs = new TaskCompletionSource<BorderSearchGetViewReponse>();

            borderSearchService.GetView((asyncContext, retData) =>
            {
                var response = retData as BorderSearchGetViewReponse;
                
                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }

        /// <summary>
        /// 数据
        /// </summary>
        public class Data 
        {
            #region 参数
            /// <summary>
            /// 参数:启动与否
            /// </summary>
            public bool? Enable { get; set; }

            /// <summary>
            /// 参数:手动设置温修AD值
            /// </summary>
            public bool? TempADBySet { set; get; }

            /// <summary>
            /// 参数:温修AD值
            /// </summary>
            public int? TempAD { get; set; }

            /// <summary>
            /// 参数:AD超过了范围， 就认为开始找到边界
            /// </summary>
            public int? TempRange { get; set; }
            /// <summary>
            /// 参数:温修范围是温修的百分比
            /// </summary>
            public bool? IsTempRangeByPercent { get; set; }

            /// <summary>
            ///  参数:温修范围百分比
            /// </summary>
            public double? TempRangePercent { get; set; }
            /// <summary>
            /// 参数:有滤波器，只有非空的连续N个pos以上，才开始算边界开始
            /// </summary>
            public int? N { get; set; }

            /// <summary>
            /// 参数:探头直径，单位脉冲， 膜宽 = 边界范围 - 探头直径
            /// </summary>
            public int? SensorWidth { get; set; }

            /// <summary>
            /// 参数:找到边界后，边界 + 探头半径  + N2个脉冲。 这个是数据有效的开始
            /// </summary>
            public int? N2 { get; set; }

            /// <summary>
            /// 参数:记录两个边界以后扫描，以它们再外扩N3个脉冲，作为扫描范围 
            /// </summary>
            public int? N3 { get; set; }
            #endregion

            #region 状态
            /// <summary>
            /// 状态:反向边界
            /// </summary>
            public Range Border_Backw { get; set; }
            /// <summary>
            /// 状态:正向边界
            /// </summary>
            public Range Border_Forw { get; set; }
            /// <summary>
            /// 状态:边界
            /// </summary>
            public RangeStruct? Border { get; set; }
            /// <summary>
            /// 状态:当前膜宽，测量出来的，单位是 脉冲
            /// </summary>
            public int? Width { get; set; }
            /// <summary>
            /// 状态:膜中间位置 单位 脉冲
            /// </summary>
            public int? Mid { get; set; }

            /// <summary>
            /// 状态:数据更新时间
            /// </summary>
            public DateTime? UpdateTime { get; set; }
            #endregion
        }
    }
}
