using FLY.OBJComponents.Common;
using FLY.OBJComponents.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 WarningSystemController : ApiController
    {
        IWarningSystem2Service warningSystemService;

        /// <summary>
        /// 
        /// </summary>
        public WarningSystemController(
            IWarningSystem2Service warningSystemService
            )
        {
            this.warningSystemService = warningSystemService;
        }

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

            return new Data()
            {
                Enable = warningSystemService.Enable,
                IsRinging = warningSystemService.IsRinging,
                ReasonList = warningSystemService.ReasonList,
            };
        }

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


        /// <summary>
        /// 复位，把全部报警消失
        /// </summary>
        [HttpPost]
        public void Reset() 
        {
            warningSystemService.Reset();
        }

        /// <summary>
        /// 静音，不消失报警，只是静音，新的报警来，还是会响
        /// </summary>
        [HttpPost]
        public void Silence() 
        {
            warningSystemService.Silence();
        }



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

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

                tcs.SetResult(response);

            }, null);
            return await tcs.Task;
        }

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

            /// <summary>
            /// 状态:正在响铃
            /// </summary>
            public bool? IsRinging { get; set; }

            /// <summary>
            /// 当前报警列表
            /// </summary>
            public FlyData_WarningHistory[] ReasonList { get; set; }

            /// <summary>
            /// 最后一条数据Id
            /// </summary>
            public long? LastId { get; set; }
        }
    }
}
