using FLY.Thick.Base.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace FLY.Thick.FilmCasting.WebAPI.Controllers
{
    /// <summary>
    /// 剔除
    /// </summary>
    public class RejectController : ApiController
    {
        IRejectService rejectService;

        /// <summary>
        /// 
        /// </summary>
        public RejectController(
            IRejectService rejectService
            )
        {
            this.rejectService = rejectService;
        }
        /// <summary>
        /// 获取服务的参数及状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Data GetValue()
        {
            return new Data()
            {
                Enable = rejectService.Enable,
                LimitRangeRatio = rejectService.LimitRangeRatio,
                ThresholdRatio = rejectService.ThresholdRatio,
                SmoothRange = rejectService.SmoothRange,
                RejectRange = rejectService.RejectRange,
                DebugUpdateTime = rejectService.DebugUpdateTime
            };
        }

        /// <summary>
        /// 设置服务参数
        /// </summary>
        /// <param name="data"></param>
        [HttpPost]
        public void SetValue([FromBody] Data data)
        {
            if (data.Enable != null)
                rejectService.Enable = (bool)data.Enable;
            if (data.LimitRangeRatio != null)
                rejectService.LimitRangeRatio = (int)data.LimitRangeRatio;
            if (data.ThresholdRatio != null)
                rejectService.ThresholdRatio = (int)data.ThresholdRatio;
            if (data.SmoothRange != null)
                rejectService.SmoothRange = (int)data.SmoothRange;
            if (data.RejectRange != null)
                rejectService.RejectRange = (int)data.RejectRange;
        }

        /// <summary>
        /// 应用 & 保存 其它参数
        /// </summary>
        [HttpPost]
        public void Apply()
        {
            rejectService.Apply();
        }

        /// <summary>
        /// 获取调试数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<RejectDebugData> GetDebugData() 
        {
            var tcs = new TaskCompletionSource<RejectDebugData>();

            rejectService.GetDebugData((asyncContext, retData) =>
            {
                var response = retData as RejectDebugData;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }


        /// <summary>
        /// 
        /// </summary>
        public class Data
        {
            /// <summary>
            /// 参数:使能
            /// </summary>
            public bool? Enable { get; set; }

            /// <summary>
            /// 参数:规格范围，Math.Abs(value-Target) > UpperLimitRatio*Target 都要被删除
            /// </summary>
            public double? LimitRangeRatio { get; set; }

            /// <summary>
            /// 参数:剔除阈值，低于 ThresholdRatio*Target 都要被删除
            /// </summary>
            public double? ThresholdRatio { get; set; }

            /// <summary>
            /// 参数:滤波范围，单位脉冲
            /// </summary>
            public int? SmoothRange { get; set; }

            /// <summary>
            /// 参数:当前数据需要剔除，那么它的前后Range2个脉冲的数据都是删除，单位脉冲
            /// </summary>
            public int? RejectRange { get; set; }

            /// <summary>
            /// 状态:调试数据更新时间，用于让客户端 决定什么时候 下载调试数据
            /// </summary>
            public DateTime? DebugUpdateTime { get; set; }
        }
    }
}
