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 BoltMapFilmCastingController : ApiController
    {
        IBoltMapFilmCastingService boltMapFilmCastingService;

        /// <summary>
        /// 
        /// </summary>
        public BoltMapFilmCastingController(
            IBoltMapFilmCastingService boltMapFilmCastingService
            )
        {
            this.boltMapFilmCastingService = boltMapFilmCastingService;
        }

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

            return new Data()
            {
                BoltWidth = boltMapFilmCastingService.BoltWidth,
                BoltCnt = boltMapFilmCastingService.BoltCnt,
                IsReversed = boltMapFilmCastingService.IsReversed,
                Smooth = boltMapFilmCastingService.Smooth,
                LeftEmptyBolts = boltMapFilmCastingService.LeftEmptyBolts,
                RightEmptyBolts = boltMapFilmCastingService.RightEmptyBolts,
                UniformShrinkPercent = boltMapFilmCastingService.UniformShrinkPercent,
                MidOffset = boltMapFilmCastingService.MidOffset,
                NBolts = boltMapFilmCastingService.NBolts,
                FirstBoltNo = boltMapFilmCastingService.FirstBoltNo,
                DebugUpdate = boltMapFilmCastingService.DebugUpdate
            };
        }

        /// <summary>
        /// 设置服务参数
        /// </summary>
        /// <param name="data"></param>
        [HttpPost]
        public void SetValue([FromBody] Data data) 
        {
            if (data.BoltWidth != null)
                boltMapFilmCastingService.BoltWidth = (double)data.BoltWidth;
            if (data.BoltCnt != null)
                boltMapFilmCastingService.BoltCnt = (int)data.BoltCnt;
            if (data.IsReversed != null)
                boltMapFilmCastingService.IsReversed = (bool)data.IsReversed;
            if (data.Smooth != null)
                boltMapFilmCastingService.Smooth = (int)data.Smooth;
        }

        /// <summary>
        /// 应用
        /// </summary>
        [HttpPost]
        public void Apply() 
        {
            boltMapFilmCastingService.Apply();
        }


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

            boltMapFilmCastingService.GetDebugData((asyncContext, retData) =>
            {
                var response = retData as BoltMapFilmCastingDebugData;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }

        /// <summary>
        /// 
        /// </summary>
        public class Data 
        {
            #region 设定
            /// <summary>
            /// 设定:螺丝宽度 mm, 是double, 那就可以设置英寸
            /// </summary>
            public double? BoltWidth { get; set; }

            /// <summary>
            /// 设定:总螺丝数 
            /// </summary>
            public int? BoltCnt { get; set; }

            /// <summary>
            /// 设定:与机架方向相反
            /// </summary>
            public bool? IsReversed { get; set; }

            /// <summary>
            /// 设定:平滑，以前后分区平均滤波 ，=0，只有自己，1，左右各一个
            /// </summary>
            public int? Smooth { get; set; }

            #endregion



            #region 状态

            /// <summary>
            /// 状态:左单边不工作螺丝位
            /// </summary>
            public int? LeftEmptyBolts { get; set; }

            /// <summary>
            /// 状态:右单边不工作螺丝位
            /// </summary>
            public int RightEmptyBolts { get; set; }

            /// <summary>
            /// 状态:收缩率  Uniform(均匀区)Shrink(收缩)Percent(比例)
            /// </summary>
            public double UniformShrinkPercent { get; set; }

            /// <summary>
            /// 状态:膜中心位置偏移 mm
            /// </summary>
            public int MidOffset { get; set; }

            /// <summary>
            /// 状态:总分区数
            /// </summary>
            public int NBolts { get; set; }

            /// <summary>
            /// 状态:第1个分区号
            /// </summary>
            public int FirstBoltNo { get; set; }

            #endregion


            /// <summary>
            /// 状态:调试数据更新时间
            /// </summary>
            public DateTime DebugUpdate { get; set; }
        }
    }
}
