using Hotline.Api.Filter;
using Hotline.Application.Bigscreen;
using Hotline.Caching.Interfaces;
using Hotline.CallCenter.Tels;
using Hotline.CallCenter.Tels.CallTelDomain;
using Hotline.Settings;
using Hotline.Share.Dtos.CallCenter;
using Hotline.Share.Dtos.TrCallCenter;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hotline.Api.Controllers.Bigscreen
{
    [ServiceFilter(typeof(ClientIpFilterAttribute))]
    public class SeatController : BaseController
    {
        private readonly ISeatStateDataService _seatStateDataService;
        private readonly ICallTelClient _callTelClient;
        private readonly ISystemSettingCacheManager _systemSettingCacheManager;

        public SeatController(ISeatStateDataService seatStateDataService, ISystemSettingCacheManager systemSettingCacheManager, ICallTelClient callTelClient)
        {
            _seatStateDataService = seatStateDataService;
            _systemSettingCacheManager = systemSettingCacheManager;
            _callTelClient = callTelClient;
        }

        /// <summary>
        /// 首次进入大屏获取数据
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("base_data")]
        public async Task<object> GetSeatStateData()
        {
            var call24 = await _seatStateDataService.GetCall24(HttpContext.RequestAborted);
            var callTop10 = await _seatStateDataService.GetCallTop10(HttpContext.RequestAborted);
            var callList = await _seatStateDataService.GetCallList(HttpContext.RequestAborted);
            var callAverage = await _seatStateDataService.GetCallAverage(HttpContext.RequestAborted);


            return new object[] { call24, callTop10, callList, callAverage };
        }


        /// <summary>
        /// 获取监听分机
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("query-listentels")]
        public async Task<List<TelOutDto>> GetListenTels()
        {
            var listenTels = _systemSettingCacheManager.GetSetting(SettingConstants.ListenTels)?.SettingValue;
            var callOuttQueueId = _systemSettingCacheManager.GetSetting(SettingConstants.CallOutQueueId)?.SettingValue;
            var list = (await _callTelClient.QueryTelsAsync(new QueryTelRequest(), HttpContext.RequestAborted))
                .Where(m => listenTels.Contains(m.TelNo))
                .ToList()
                .Adapt<List<TelOutDto>>();
            list.ForEach(x =>
            {
                x.Queue = callOuttQueueId[0];
            });
            return list;
        }

        /// <summary>
        /// 获取监听分机
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("query-listentels-xingtang")]
        public async Task<List<TelOutDto>> GetListenTelsXT()
        {
            var listenTels = _systemSettingCacheManager.GetSetting(SettingConstants.ListenTels)?.SettingValue;
            var callOuttQueueId = _systemSettingCacheManager.GetSetting(SettingConstants.CallOutQueueId)?.SettingValue;
            List<TelOutDto> list = [];
            if (listenTels != null && callOuttQueueId != null)
            {
                foreach (var item in listenTels)
                {
                    list.Add(new TelOutDto()
                    {
                        Queue = callOuttQueueId[0],
                        TelNo = item
                    });
                }
            }
            return list;
        }
    }
}
