using Hotline.Permissions;
using Hotline.Push;
using Hotline.Push.FWMessage;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Push;
using Hotline.Share.Dtos.Push.FWMessage;
using Hotline.Share.Enums.Push;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using XF.Domain.Repository;
using XF.Utility.EnumExtensions;

namespace Hotline.Api.Controllers
{
    public class PushMessageController : OriginController
    {
        #region 注入

        private readonly IRepository<Message> _messageRepository;
        private readonly IMapper _mapper;
        private readonly IPushDomainService _pushDomainService;
        private readonly IMessageCodeDomainService _messageCodeDomainService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="messageRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="pushDomainService"></param>
        /// <param name="messageCodeDomainService"></param>
        public PushMessageController(IRepository<Message> messageRepository,
            IMapper mapper,
            IPushDomainService pushDomainService,
            IMessageCodeDomainService messageCodeDomainService)
        {
            _messageRepository = messageRepository;
            _mapper = mapper;
            _pushDomainService = pushDomainService;
            _messageCodeDomainService = messageCodeDomainService;
        }

        #endregion

        /// <summary>
        /// 发送登录验证码
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet("send_login_message_code")]
        [AllowAnonymous]
        public async Task<string> SendLoginMessageCode(string UserName)
        {
            return await _messageCodeDomainService.GenerateVerificationCode(UserName, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 验证白名单
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet("get_verify_whitelist")]
        [AllowAnonymous]
        public async Task<bool> VerifyWhitelist(string UserName)
        {
            return await _messageCodeDomainService.VerifyWhitelistAsync(UserName);
        }


        //[HttpGet("checkdcode")]
        //public async Task CheckdCode(string UserName, string phoneNum, string Code)
        //{
        //    await _messageCodeDomainService.CheckdCode(UserName, phoneNum, Code, HttpContext.RequestAborted);
        //}

        #region 新增

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut("add")]
        [AllowAnonymous]
        public async Task AddMessage([FromBody] MessageDto dto)
        {
            //  await _pushDomainService.PushAsync(dto, HttpContext.RequestAborted);
            await _pushDomainService.PushMsgAsync(dto, HttpContext.RequestAborted);
        }


        #endregion

        #region 查询短信

        /// <summary>
        /// 查询短信
        /// </summary>
        /// <param name="pagedDto"></param>
        /// <returns></returns>
        [Permission(EPermission.AllMessage)]
        [HttpGet("getlist")]
        public async Task<PagedDto<MessageDataDto>> AllMessage([FromQuery] MessagePagedDto pagedDto)
        {
            var (total, items) = await _messageRepository
                .Queryable()
                .Includes(it => it.Order)
                .WhereIF(pagedDto.PushBusiness.HasValue, d => d.PushBusiness == pagedDto.PushBusiness)
                .WhereIF(pagedDto.Status.HasValue, d => d.SendState == pagedDto.Status)
                .WhereIF(!string.IsNullOrEmpty(pagedDto.Keyword),
                    d => d.Name.Contains(pagedDto.Keyword!) || d.Content.Contains(pagedDto.Keyword!) ||
                         d.TelNumber.Contains(pagedDto.Keyword!) || d.CreatorName.Contains(pagedDto.Keyword!) ||
                         d.CreatorOrgName.Contains(pagedDto.Keyword!))
                .WhereIF(pagedDto.StartTime.HasValue, d => d.SendTime >= pagedDto.StartTime)
                .WhereIF(pagedDto.EndTime.HasValue, d => d.SendTime <= pagedDto.EndTime)
                .WhereIF(!string.IsNullOrEmpty(pagedDto.UserName), d => d.Name == pagedDto.UserName)
                 .WhereIF(!string.IsNullOrEmpty(pagedDto.TelNumber), d => d.TelNumber == pagedDto.TelNumber)
                .OrderByDescending(it => it.CreationTime)
                .ToPagedListAsync(pagedDto.PageIndex, pagedDto.PageSize, HttpContext.RequestAborted);

            return new PagedDto<MessageDataDto>(total, _mapper.Map<IReadOnlyList<MessageDataDto>>(items));
        }

        /// <summary>
        /// 列表页面基础数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("list-data")]
        public async Task<object> ListData()
        {
            var rsp = new
            {
                EPushBusinessData = EnumExts.GetDescriptions<EPushBusiness>(),
                ESendStateData = EnumExts.GetDescriptions<ESendState>()
            };
            return rsp;
        }

        #endregion
    }
}