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
{
    public class GetSampleController : ApiController
    {
        IGetSampleService getSampleService;

        /// <summary>
        /// 
        /// </summary>
        public GetSampleController(
            IGetSampleService getSampleService
            )
        {
            this.getSampleService = getSampleService;
        }

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

            return new Data()
            {
                Enable = getSampleService.Enable,
                SampleRange = getSampleService.SampleRange,
                Window = getSampleService.Window,
                IsCheckByPercent = getSampleService.IsCheckByPercent,
                ErrPercent = getSampleService.ErrPercent,
                ErrValue = getSampleService.ErrValue,
                SampleOrgAD = getSampleService.Samples[0].OrgAD,
                SamplePosition = getSampleService.Samples[0].Position,
                SampleAD = getSampleService.Samples[0].AD,
                SampleValue = getSampleService.Samples[0].SampleValue
            };
        }

        /// <summary>
        /// 设置服务参数
        /// </summary>
        /// <param name="data"></param>
        [HttpPost]
        public void SetValue([FromBody] Data data)
        {
            if (data.Enable != null)
                getSampleService.Enable = (bool)data.Enable;
            if (data.SampleRange != null)
                getSampleService.SampleRange = (int)data.SampleRange;
            if (data.Window != null)
                getSampleService.Window = (int)data.Window;
            if (data.IsCheckByPercent != null)
                getSampleService.IsCheckByPercent = (bool)data.IsCheckByPercent;
            if (data.ErrPercent != null)
                getSampleService.ErrPercent = (double)data.ErrPercent;
            if (data.ErrValue != null)
                getSampleService.ErrValue = (int)data.ErrValue;
            if (data.SampleOrgAD != null)
                getSampleService.Samples[0].OrgAD = (int)data.SampleOrgAD;
            if (data.SamplePosition != null)
                getSampleService.Samples[0].Position = (int)data.SamplePosition;
        }

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


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

            /// <summary>
            /// 参数：样品点半径 单位:脉冲
            /// 样品点直径为 Range*2
            /// </summary>
            public int? SampleRange { get; set; }

            /// <summary>
            /// 参数：移动滤波 ，窗口值 单位:min
            /// </summary>
            public int? Window { get; set; }

            /// <summary>
            /// 使用%方式检查异常
            /// 1.滤波前AD 与 滤波后AD比较
            /// 2.当前样品AD 与 上一次样品AD比较
            /// 3.当前 样品X_AD/样品0_AD 与 上一次比较 
            /// </summary>
            public bool? IsCheckByPercent { get; set; }

            /// <summary>
            /// 异常比例 单位不是%
            /// </summary>
            public double? ErrPercent { get; set; }

            /// <summary>
            /// 异常值
            /// </summary>
            public int? ErrValue { get; set; }

            /// <summary>
            /// 参数：原始AD值
            /// </summary>
            public int? SampleOrgAD { get; set; }

            /// <summary>
            /// 参数：位置
            /// </summary>
            public int? SamplePosition { get; set; }

            /// <summary>
            /// 状态：当前测量的AD值
            /// </summary>
            public int? SampleAD { get; set; }


            /// <summary>
            /// 状态：当前测量的AD值 对应的 样品值
            /// </summary>
            public double? SampleValue { get; set; }
        }
    }
}
