using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using GxPress.Common.Exceptions;
using GxPress.Common.Http;
using GxPress.Request.GroupChat;
using GxPress.Common.Extensions;
using GxPress.Common.Tools;

namespace GxPress.Service.Implement.IM
{
    /// <summary>
    /// 群禁言 移除禁言
    /// </summary>
    public partial class IMService
    {
        /// <summary>
        /// 加入群禁言
        /// </summary>
        /// <param name="groupChatImId"></param>
        /// <returns></returns>
        public async Task<bool> InMute(string groupChatImId)
        {
            var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
            var groupChat = await _groupChatRepository.GetGroupChatByImIdAsync(groupChatImId);
            var token = await _cache.GetAsync<string>("ImToken");
            var url = $"{imServiceUrl}/chatgroups/{groupChatImId}/mute";
            var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
            //获取成员
            var groupChatUsers = await _groupChatUserRepository.FindGroupChatUserByImId(groupChatImId);
            var users = await _userRepository.UserListInsAsync(groupChatUsers.Select(n => n.UserId).ToList());
            var dataObj = new { usernames = users.Select(n => n.ImId).ToList(), mute_duration = -1000 };
            var response =
                await HttpClientHelper.PostAsync(url, dataObj, headers);
            if (response.IsSuccessStatusCode)
            {
                //禁言
                var request = new GroupChatUpIsWordsRequest { Id = groupChat.Id };
                return await _groupChatRepository.UpIsWordsAsync(request);
            }

            if (response.StatusCode == HttpStatusCode.Forbidden)
                throw new BusinessException("该成员不在此群组");
            if (response.StatusCode == HttpStatusCode.NotFound)
                throw new BusinessException("此群组ID不存在");
            if (response.StatusCode == HttpStatusCode.Unauthorized)
                throw new BusinessException("未授权[无token、token错误、token过期]");
            if (response.StatusCode == HttpStatusCode.BadRequest)
                throw new BusinessException("owner用户不存在");
            if (response.StatusCode == HttpStatusCode.TooManyRequests)
                throw new BusinessException("接口被限流");
            return false;
        }
        /// <summary>
        /// 群解除禁言
        /// </summary>
        /// <param name="groupChatImId"></param>
        /// <returns></returns>
        public async Task<bool> RemoveMute(string groupChatImId)
        {
            var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
            var groupChat = await _groupChatRepository.GetGroupChatByImIdAsync(groupChatImId);
            var token = await _cache.GetAsync<string>("ImToken");
            var jsonValue = "";
            var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
            //获取成员
            var groupChatUsers = await _groupChatUserRepository.FindGroupChatUserByImId(groupChatImId);
            var users = await _userRepository.UserListInsAsync(groupChatUsers.Select(n => n.UserId).ToList());
            foreach (var user in users)
            {
                jsonValue += user.ImId + ",";
            }
            if (!string.IsNullOrEmpty(jsonValue))
                jsonValue = jsonValue.Remove(jsonValue.Length - 1, 1);
            var url = $"{imServiceUrl}/chatgroups/{groupChatImId}/mute/{jsonValue}";
            var response =
                await HttpClientHelper.DeleteAsync(url, headers);
            if (response.IsSuccessStatusCode)
            {
                //解除禁言
                var request = new GroupChatUpIsWordsRequest { Id = groupChat.Id };
                return await _groupChatRepository.UpIsWordsAsync(request);
            }

            if (response.StatusCode == HttpStatusCode.Forbidden)
                throw new BusinessException("该成员不在此群组");
            if (response.StatusCode == HttpStatusCode.NotFound)
                throw new BusinessException("此群组ID不存在");
            if (response.StatusCode == HttpStatusCode.Unauthorized)
                throw new BusinessException("未授权[无token、token错误、token过期]");
            if (response.StatusCode == HttpStatusCode.BadRequest)
                throw new BusinessException("owner用户不存在");
            if (response.StatusCode == HttpStatusCode.TooManyRequests)
                throw new BusinessException("接口被限流");
            throw new BusinessException("禁言失败");
            return false;
        }
    }
}
