using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.App.IM;
using GxPress.Request.GroupChat;
using GxPress.Result.App.GroupChat;
using GxPress.Service.Interface;
using GxPress.Service.Interface.AdminVerify;
using GxPress.Service.Interface.IM;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 群
    /// </summary>
    [Route("api/app/group-chat")]
    [ApiController]
    [Authorize]
    public class GroupChatController : ControllerBase
    {
        private readonly IGroupChatRepository _groupChatRepository;
        private readonly IGroupChatUserRepository _groupChatUserRepository;
        private readonly IIMService _imService;
        private readonly ILoginContext _loginContext;
        private readonly IAdminVerifyService _adminVerifyService;
        private readonly IGroupChatUserService _groupChatUserService;

        public GroupChatController(IGroupChatRepository groupChatRepository, ILoginContext loginContext,
            IIMService imService, IAdminVerifyService adminVerifyService,
            IGroupChatUserRepository groupChatUserRepository, IGroupChatUserService groupChatUserService)
        {
            _groupChatRepository = groupChatRepository;
            _imService = imService;
            _loginContext = loginContext;
            _adminVerifyService = adminVerifyService;
            _groupChatUserRepository = groupChatUserRepository;
            _groupChatUserService = groupChatUserService;
        }
        ///// <summary>
        ///// 添加
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost("add")]
        //public async Task<bool> Insert(GroupChatInRequest request)
        //{
        //    request.UserId = _loginContext.AccountId;
        //    return await _groupChatRepository.InsertAsync(request);
        //}

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

        /// <summary>
        /// 群组解散群
        /// </summary>
        /// <param name="groupChatImId"></param>
        /// <returns></returns>
        [HttpDelete("{groupChatImId}")]
        public async Task<bool> DeleteGroupChat(string groupChatImId)
        {
            return await _imService.DeleteGroupChatAsync(groupChatImId, _loginContext.AccountId);
        }
        /// <summary>
        /// 根据UserId查询群
        /// </summary>
        /// <returns></returns>
        [HttpPost("group-chat-search")]
        public async Task<IEnumerable<GroupChat>> GetGroupChatByUserId()
        {
            var request = new GroupChatSearchRequest { UserId = _loginContext.AccountId };
            return await _groupChatRepository.GetGroupChatByUserIdAsync(request);
        }

        /// <summary>
        /// 是否管理员同意
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("up-is-admin")]
        public async Task<bool> UpIsAdmin(GroupChatUpIsAdminRequest request)
        {
            return await _groupChatRepository.UpIsAdminAsync(request);
        }

        /// <summary>
        /// 创建群
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<GroupChatInfoResult> CreateGroupChat(GroupChatInRequest request)
        {
            request.UserId = _loginContext.AccountId;
            //添加群成员
            var insertGroupChatUsersRequest = new InsertGroupChatUsersRequest();
            insertGroupChatUsersRequest.UserId = request.UserId;
            insertGroupChatUsersRequest.UserIds = request.UserIds;
            insertGroupChatUsersRequest.SourceType = 2;
            //创建群
            var result = await _imService.CreateGroupChatAsync(request);
            if (result.Success)
            {
                insertGroupChatUsersRequest.GroupChatImId = result.GroupChatImId;
                insertGroupChatUsersRequest.IsFound = true;
                await _groupChatUserService.InsertAsync(insertGroupChatUsersRequest);
                //获取群信息
                return await _groupChatRepository.GetGroupChatInfoByImIdAsync(result.GroupChatImId);
            }
            return new GroupChatInfoResult();
        }

        /// <summary>
        /// 添加群用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add-users")]
        public async Task<bool> InsertGroupChatUsers(InsertGroupChatUsersRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _groupChatUserService.InsertAsync(request);
        }

        /// <summary>
        /// 群删除成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("remove-users")]
        public async Task<bool> RemoveGroupChatUsers(InsertGroupChatUsersRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _imService.RemoveGroupChatUsersAsync(request);
        }
        /// <summary>
        /// 禁言
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("in-mute")]
        public async Task<bool> InMute(SetGroupChatMuteRequest request)
        {
            return await _imService.InMute(request.GroupChatImId);
        }
        /// <summary>
        /// 解除禁言
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("remove-mute")]
        public async Task<bool> RemoveMute(SetGroupChatMuteRequest request)
        {
            return await _imService.RemoveMute(request.GroupChatImId);
        }
        /// <summary>
        /// 根据groupChatGuid获取群信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("guid")]
        public async Task<GroupChat> GetGroupChatByGuid(GroupChatByGuidRequest request)
        {
            return await _groupChatRepository.GetGroupChatByGuidAsync(request.GroupChatGuid);
        }

        /// <summary>
        /// set群是否开启
        /// </summary>
        /// <param name="groupChatImId"></param>
        /// <returns></returns>
        [HttpPut("{groupChatImId}")]
        public async Task<bool> SetIsOpenQrCoder(string groupChatImId)
        {
            return await _groupChatRepository.SetIsOpenQrCoderAsync(groupChatImId, _loginContext.AccountId);
        }
    }
}