using FLY.OBJComponents.IService;
using FLY.Thick.FilmCasting.Common;
using FLY.Thick.FilmCasting.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace FLY.Thick.FilmCasting.WebAPI.Controllers
{
    /// <summary>
    /// 产品
    /// </summary>
    public class FilmCastingProfileController : ApiController
    {
        IFilmCastingProfileService filmCastingProfileService;

        /// <summary>
        /// 
        /// </summary>
        public FilmCastingProfileController(
            IFilmCastingProfileService filmCastingProfileService
            )
        {
            this.filmCastingProfileService = filmCastingProfileService;
        }

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

            return new Data()
            {
                Param = filmCastingProfileService.Param,
                RollDown_AvgChanged = filmCastingProfileService.RollDown_AvgChanged,
                RollDown_FilmWidthChanged = filmCastingProfileService.RollDown_FilmWidthChanged,
                RollDown_FilmLength = filmCastingProfileService.RollDown_FilmLength,
                RollDown_TimeoutHour = filmCastingProfileService.RollDown_TimeoutHour,
                RollDown_StopTimeMin = filmCastingProfileService.RollDown_StopTimeMin,
            };
        }

        /// <summary>
        /// 设置服务参数
        /// </summary>
        /// <param name="data"></param>
        [HttpPost]
        public void SetValue([FromBody] Data data)
        {
            if (data.RollDown_AvgChanged != null)
                filmCastingProfileService.RollDown_AvgChanged = (double)data.RollDown_AvgChanged;
            if (data.RollDown_FilmWidthChanged != null)
                filmCastingProfileService.RollDown_FilmWidthChanged = (double)data.RollDown_FilmWidthChanged;
            if (data.RollDown_FilmLength != null)
                filmCastingProfileService.RollDown_FilmLength = (double)data.RollDown_FilmLength;
            if (data.RollDown_TimeoutHour != null)
                filmCastingProfileService.RollDown_TimeoutHour = (double)data.RollDown_TimeoutHour;
            if (data.RollDown_StopTimeMin != null)
                filmCastingProfileService.RollDown_StopTimeMin = (double)data.RollDown_StopTimeMin;
        }

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

        /// <summary>
        /// 应用 & 保存 列表数据
        /// </summary>
        [HttpPost]
        public void ApplyParam([FromBody]FilmCastingProfileParam param) 
        {
            filmCastingProfileService.ApplyParam(param);
        }


        /// <summary>
        /// 获取产品列表
        /// </summary>
        [HttpPost]
        public async Task<List<string>> GetList() 
        {
            var tcs = new TaskCompletionSource<List<string>>();

            filmCastingProfileService.GetList((asyncContext, retData) =>
            {
                var response = retData as List<string>;
                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }
        /// <summary>
        /// 删除指定产品
        /// </summary>
        [HttpPost]
        public void Del([FromBody] ReadRequest request) 
        {
            filmCastingProfileService.Del(request.productname);
        }
        /// <summary>
        /// 读取指定产品
        /// </summary>
        [HttpPost]
        public async Task<FilmCastingProfileParam> Read([FromBody] ReadRequest request) 
        {
            var tcs = new TaskCompletionSource<FilmCastingProfileParam>();

            filmCastingProfileService.Read(request.productname,(asyncContext, retData) =>
            {
                var response = retData as FilmCastingProfileParam;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }
        public class ReadRequest
        {
            /// <summary>
            /// 产品名称
            /// </summary>
            public string productname;
        }
        /// <summary>
        /// 
        /// </summary>
        public class Data 
        {
            /// <summary>
            /// 状态：产品配方
            /// </summary>
            public FilmCastingProfileParam Param { get; set; }


            /// <summary>
            /// 参数：自动下辊条件 平均值变化 10um
            /// </summary>
            public double? RollDown_AvgChanged { get; set; }
            /// <summary>
            /// 参数：自动下辊条件 膜宽度变化 100mm
            /// </summary>
            public double? RollDown_FilmWidthChanged { get; set; }
            /// <summary>
            /// 参数：自动下辊条件 生产长度 大于 10000m
            /// </summary>
            public double? RollDown_FilmLength { get; set; }
            /// <summary>
            /// 参数：自动下辊条件 生产时间 2小时
            /// </summary>
            public double? RollDown_TimeoutHour { get; set; }
            /// <summary>
            /// 参数：自动下辊条件 停止时间 大于 10min
            /// </summary>
            public double? RollDown_StopTimeMin { get; set; }
        }
    }
}
