using FLY.Thick.FilmCasting.IService;
using FLY.Thick.FilmCasting.Server.Model;
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 BulkDbController : ApiController
    {
        IBulkDbService bulkDbService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bulkDbService"></param>
        public BulkDbController(
            IBulkDbService bulkDbService
            )
        {
            this.bulkDbService = bulkDbService;
        }

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

            return new Data()
            {
                LastScanDataId = bulkDbService.LastScanDataId,
                LastProfileId = bulkDbService.LastProfileId,
                LastTimestamp = bulkDbService.LastTimestamp,
                StartTime = bulkDbService.StartTime,
                EndTime = bulkDbService.EndTime,
                IsFinished = bulkDbService.IsFinished
            };
        }

        /// <summary>
        /// 获取临时数据
        /// </summary>
        [HttpPost]
        public async Task<BulkDbTempFrameChangedEventArgs> GetTempFrame() 
        {
            TaskCompletionSource<BulkDbTempFrameChangedEventArgs> tcs = new TaskCompletionSource<BulkDbTempFrameChangedEventArgs>();

            bulkDbService.GetTempFrame((asyncContext, retData) =>
            {
                var response = retData as FLY.Thick.FilmCasting.IService.BulkDbTempFrameChangedEventArgs;

                tcs.SetResult(new BulkDbTempFrameChangedEventArgs()
                {
                    Time = response.Time,
                    EndTime = response.EndTime,
                    StartIndex = response.StartIndex,
                    D = response.D
                });

            }, null);

            return await tcs.Task;
        }

        #region 统计

        /// <summary>
        /// 获取历史profile
        /// </summary>
        [HttpPost]
        public async Task<Pack_GetProfileReponse> GetProfile([FromBody] Pack_GetProfileRequest request) 
        {
            TaskCompletionSource<Pack_GetProfileReponse> tcs = new TaskCompletionSource<Pack_GetProfileReponse>();

            bulkDbService.GetProfile(request, (asyncContext, retData) =>
            {
                var response = retData as Pack_GetProfileReponse;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }


        /// <summary>
        /// 获取一幅数据
        /// </summary>

        [HttpPost]
        public async Task<Pack_GetFrameReponse> GetFrame([FromBody] Pack_GetFrameRequest request) 
        {
            var tcs = new TaskCompletionSource<Pack_GetFrameReponse>();

            bulkDbService.GetFrame(request, (asyncContext, retData) =>
            {
                var response = retData as Pack_GetFrameReponse;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }


        /// <summary>
        /// 获取一幅数据 且 对应的Profile
        /// </summary>
        [HttpPost]
        public async Task<Pack_GetFrame2Reponse> GetFrame2([FromBody] Pack_GetFrameRequest request)
        {
            var tcs = new TaskCompletionSource<Pack_GetFrame2Reponse>();

            bulkDbService.GetFrame2(request, (asyncContext, retData) =>
            {
                var response = retData as Pack_GetFrame2Reponse;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }



        /// <summary>
        /// 获取近期的大量数据
        /// </summary>
        [HttpPost]
        public async Task<Pack_GetRecentFramesReponse> GetRecentFrames([FromBody] Pack_GetRecentFramesRequest request) 
        {
            var tcs = new TaskCompletionSource<Pack_GetRecentFramesReponse>();

            bulkDbService.GetRecentFrames(request, (asyncContext, retData) =>
            {
                var response = retData as Pack_GetRecentFramesReponse;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }

        /// <summary>
        /// 获取纵向趋势图
        /// </summary>
        [HttpPost]
        public async Task<Pack_GetTrendReponse> GetTrend([FromBody] Pack_GetTrendRequest request)
        {
            var tcs = new TaskCompletionSource<Pack_GetTrendReponse>();

            bulkDbService.GetTrend(request, (asyncContext, retData) =>
            {
                var response = retData as Pack_GetTrendReponse;

                tcs.SetResult(response);

            }, null);

            return await tcs.Task;
        }



        #endregion


        /// <summary>
        /// 完成,下辊。。。
        /// </summary>
        [HttpPost]
        public void Finish() 
        {
            bulkDbService.Finish();
        }
        /// <summary>
        /// 数据
        /// </summary>
        public class Data
        {
            /// <summary>
            /// 最后一条扫描数据Id
            /// </summary>
            public long LastScanDataId { get; set; }
            /// <summary>
            /// 最后一条产品Id
            /// </summary>
            public long LastProfileId { get; set; }
            /// <summary>
            /// 最后一条产品被修改的时间
            /// </summary>
            public DateTime LastTimestamp { get; set; }



            /// <summary>
            /// 开始产生数据时间
            /// </summary>
            public DateTime StartTime { get; set; }

            /// <summary>
            /// 最后产生数据时间
            /// </summary>
            public DateTime EndTime { get; set; }

            /// <summary>
            /// 当前生产，已经结束，已经下辊了
            /// </summary>
            public bool IsFinished { get; set; }
        }
    }
}
