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>
    //[Route("api/thk/ScanCorr")]
    public class ScanCorrController : ApiController
    {
        IScanCorrService scanCorrService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="scanCorrService"></param>
        public ScanCorrController(
            FLY.Thick.Base.IService.IScanCorrService scanCorrService
            )
        {
            this.scanCorrService = scanCorrService;
        }


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

            return new Data()
            {
                Enable = scanCorrService.Enable,
                CurrGroupIndex =scanCorrService.CurrGroupIndex,
                Progress = scanCorrService.Progress,
                UpdateTimes = scanCorrService.UpdateTimes,
                IsRunning = scanCorrService.IsRunning,
                ScanCnt = scanCorrService.ScanCnt,
                SmoothFactor = scanCorrService.SmoothFactor
            };
        }

        /// <summary>
        /// 获取服务的数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public void SetValue([FromBody] Data data)
        {
            if (data.Enable != null)
                scanCorrService.Enable = (bool)data.Enable;
        }

        public class StartRequest 
        {
            /// <summary>
            /// 组序号
            /// </summary>
            public int groupIndex;
            /// <summary>
            /// 扫描次数
            /// </summary>
            public int scanCnt;
            /// <summary>
            /// 平滑
            /// </summary>
            public int smoothFactor;
        }
        /// <summary>
        /// 开始机架修正
        /// </summary>
        [HttpPost]
        public void Start([FromBody] StartRequest request) 
        {
            scanCorrService.Start(request.groupIndex, request.scanCnt, request.smoothFactor);
        }


        public class ClearRequest 
        {
            /// <summary>
            /// 组序号
            /// </summary>
            public int groupIndex;
        }

        /// <summary>
        /// 清空组数据
        /// </summary>
        [HttpPost]
        public void Clear([FromBody] ClearRequest request) 
        {
            scanCorrService.Clear(request.groupIndex);
        }

        public class SetCorrDataRequest 
        {
            /// <summary>
            /// 组序号
            /// </summary>
            public int groupIndex;
            /// <summary>
            /// 修正数据
            /// </summary>
            public int[][] corrDatas;
            /// <summary>
            /// 均值
            /// </summary>
            public int avg;

            /// <summary>
            /// 原始数据
            /// </summary>
            public int[][] orgDatas;
        }
        /// <summary>
        /// 设置修正曲线
        /// </summary>
        [HttpPost]
        public void SetCorrData([FromBody] SetCorrDataRequest request)
        {
            scanCorrService.SetCorrData(request.groupIndex, request.corrDatas, request.avg, request.orgDatas);
        }


        public class GetScanCorrGroupRequest 
        {
            public int groupIndex;
        }

        /// <summary>
        /// 获取修正曲线
        /// </summary>
        [HttpPost]
        public async Task<GetScanCorrGroupResponse> GetScanCorrGroup([FromBody] GetScanCorrGroupRequest request)
        {
            var tcs = new TaskCompletionSource<GetScanCorrGroupResponse>();

            scanCorrService.GetScanCorrGroup(request.groupIndex, (asyncContext, retData) =>
            {
                var response = retData as GetScanCorrGroupResponse;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }

        /// <summary>
        /// 数据
        /// </summary>
        public class Data
        {
            /// <summary>
            /// 使能修正
            /// </summary>
            public bool? Enable { get; set; }

            /// <summary>
            /// 当前修正的组
            /// </summary>
            public int? CurrGroupIndex { get; set; }
            /// <summary>
            /// 当前进度 0 - 100 
            /// </summary>
            public int? Progress { get; set; }

            /// <summary>
            /// 数据更新时间
            /// </summary>
            public DateTime[] UpdateTimes { get; set; }

            /// <summary>
            /// 正在机架修正中
            /// </summary>
            public bool? IsRunning { get; set; }

            /// <summary>
            /// 来回次数
            /// </summary>
            public int? ScanCnt { get; set; }

            /// <summary>
            /// 平滑
            /// </summary>
            public int? SmoothFactor { get; set; }
        }
    }
}
