using FLY.Thick.FilmCasting.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 ScanWarningController : ApiController
    {
        IScanWarningService scanWarningService;

        /// <summary>
        /// 
        /// </summary>
        public ScanWarningController(
            IScanWarningService scanWarningService
            )
        {
            this.scanWarningService = scanWarningService;
        }

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

            return new Data()
            {
                Enable = scanWarningService.Enable,
                AlarmCnt = scanWarningService.AlarmCnt,
                AlarmPercent = scanWarningService.AlarmPercent,
                Mix = scanWarningService.Mix
            };
        }

        /// <summary>
        /// 设置服务参数
        /// </summary>
        /// <param name="data"></param>
        [HttpPost]
        public void SetValue([FromBody] Data data)
        {
            if (data.Enable != null)
                scanWarningService.Enable = (bool)data.Enable;
            if (data.AlarmCnt != null)
                scanWarningService.AlarmCnt = (int)data.AlarmCnt;
            if (data.AlarmPercent != null)
                scanWarningService.AlarmPercent = (double)data.AlarmPercent;
            if (data.Mix != null)
                scanWarningService.Mix = (int)data.Mix;
        }

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

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

            /// <summary>
            /// 参数:连续N个点，大于规格线（公差）才算报警
            /// </summary>
            public int? AlarmCnt { get; set; }

            /// <summary>
            /// 参数:是公差的多少倍 报警 单位 % 也就是  AlarmPercent = 100, 倍数为1
            /// </summary>
            public double? AlarmPercent { get; set; }
            /// <summary>
            /// 参数:混合数
            /// </summary>
            public int? Mix { get; set; }
        }
    }
}
