using System.Linq;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.Repository.Interface;
using GxPress.Request.GroupChatUser;
using GxPress.Result.GroupChatUser;
using GxPress.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 群聊组员
    /// </summary>
    [Route("api/app/group-chat-user")]
    [ApiController]
    [Authorize]
    public class GroupChatUserController : ControllerBase
    {
        private readonly IGroupChatUserRepository _groupChatUserRepository;
        private readonly IGroupChatUserService _groupChatUserService;
        private readonly IGroupChatRepository _groupChatRepository;
        private readonly IOftenContactRepository _oftenContactRepository;
        private readonly ILoginContext _loginContext;

        public GroupChatUserController(IGroupChatUserRepository groupChatUserRepository,
            IGroupChatUserService groupChatUserService, IGroupChatRepository groupChatRepository,
            ILoginContext loginContext, IOftenContactRepository oftenContactRepository)
        {
            _groupChatUserRepository = groupChatUserRepository;
            _groupChatUserService = groupChatUserService;
            _groupChatRepository = groupChatRepository;
            _loginContext = loginContext;
            _oftenContactRepository = oftenContactRepository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<bool> Insert(GroupChatUserInRequest request)
        {
            return await _groupChatUserRepository.InsertAsync(request);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<bool> Update(GroupChatUserUpRequest request)
        {
            return await _groupChatUserRepository.UpdateAsync(request);
        }



        /// <summary>
        /// 根据群ID查询群
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("group-chat-user-search")]
        public async Task<GroupChatUserByGroupChatIdResultList> GetGroupChatUserByGroupChatId(
            GroupChatUserSearchRequest request)
        {
            var userId = _loginContext.AccountId;
            var groupChat = await _groupChatRepository.GetGroupChatByImIdAsync(request.GroupChatImId);
            if (groupChat == null)
                throw new BusinessException("群不存在！");
            var result = new GroupChatUserByGroupChatIdResultList
            {
                GroupChat = groupChat,
                Item = await _groupChatUserRepository.GetGroupChatUserByGroupChatIdAsync(request)
            };
            //判断是否是管理员
            result.GroupChat.IsAdmin = result.GroupChat.UserId == _loginContext.AccountId;
            //result.GroupChat.AvatarUrl = StringUtils.AddDomainMin(result.GroupChat.AvatarUrl);
             //result.GroupChat.QRCoder = StringUtils.AddDomain(result.GroupChat.QRCoder);
            result.IsAdmin = result.GroupChat.UserId == _loginContext.AccountId;
            result.IsUser = result.Item.Any(n => n.UserId == userId);
            result.IsTop = await _oftenContactRepository.GetIsTopAsync(userId, groupChat.GroupChatImId);
            var groupChatUserIsDisturbRequest = new GroupChatUserIsDisturbRequest
            {
                UserId = userId,
                GroupChatId = groupChat.Id
            };
            result.IsDisturb = await _groupChatUserRepository.IsDisturbAsync(groupChatUserIsDisturbRequest);
            return result;
        }

        /// <summary>
        /// 根据群GUID查询群
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        [HttpGet("{guid}")]
        public async Task<GroupChatUserByGroupChatIdResultList> GetGroupChatUserByGroupChatGuid(string guid)
        {
            var userId = _loginContext.AccountId;
            var groupChatUserSearchRequest = new GroupChatUserSearchRequest();
            var groupChat = await _groupChatRepository.GetGroupChatByGuidAsync(guid);
            if (groupChat == null)
                throw new BusinessException("群不存在！");
            groupChatUserSearchRequest.GroupChatImId = groupChat.GroupChatImId;
            var result = new GroupChatUserByGroupChatIdResultList
            {
                GroupChat = groupChat,
                Item = await _groupChatUserRepository.GetGroupChatUserByGroupChatIdAsync(groupChatUserSearchRequest)
            };
            //判断是否是管理员
            result.GroupChat.IsAdmin = result.GroupChat.UserId == _loginContext.AccountId;
            result.GroupChat.AvatarUrl = StringUtils.AddDomainMin(result.GroupChat.AvatarUrl);
            result.GroupChat.QRCoder = StringUtils.AddDomain(result.GroupChat.QRCoder);
            result.IsAdmin = result.GroupChat.UserId == _loginContext.AccountId;
            result.IsUser = result.Item.Any(n => n.UserId == userId);
            result.IsTop = await _oftenContactRepository.GetIsTopAsync(userId, groupChat.GroupChatImId);
            var groupChatUserIsDisturbRequest = new GroupChatUserIsDisturbRequest
            {
                UserId = userId,
                GroupChatId = groupChat.Id
            };
            result.IsDisturb = await _groupChatUserRepository.IsDisturbAsync(groupChatUserIsDisturbRequest);
            return result;
        }

        /// <summary>
        /// 是否免打扰
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("up-is-disturb")]
        public async Task<bool> UpIsDisturb(GroupChatUserIsDisturbRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _groupChatUserRepository.UpIsDisturbAsync(request);
        }

        /// <summary>
        /// 是否置顶
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("up-is-top")]
        public async Task<bool> UpIsTop(GroupChatUserIsTopRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _groupChatUserRepository.UpIsTopAsync(request);
        }
    }
}