using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using GxPress.Common.Exceptions;
using GxPress.Common.Http;
using GxPress.Entity;
using GxPress.Common.Extensions;
using GxPress.Common.Tools;

namespace GxPress.Service.Implement.IM
{
    public partial class IMService
    {
        /// <summary>
        /// 添加用户黑名单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="blacklistUserId"></param>
        /// <returns></returns>
        public async Task<bool> AddBlacklistUserAsync(int userId, int blacklistUserId)
        {
            var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
            var resultBool = await _blacklistUserRepository.FindUserIsBlack(userId, blacklistUserId);
            if (resultBool)
                return true;
            var token = await _cache.GetAsync<string>("ImToken");
            var user = await _userRepository.GetAsync(userId);
            var blacklistUserEntity = await _userRepository.GetAsync(blacklistUserId);
            //环信添加黑名单
            var url = $"{imServiceUrl}/users/{user.ImId}/blocks/users";
            var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
            var imIds = new List<string> { blacklistUserEntity.ImId };
            var jsonObj = new { usernames = imIds };
            var response =
                await HttpClientHelper.PostAsync(url, jsonObj, headers);
            if (response.IsSuccessStatusCode)
            {
                //设置成功
                var blacklistUser = new BlacklistUser
                {
                    UserId = userId,
                    AvatarUrl = blacklistUserEntity.AvatarUrl,
                    BlackUserName = blacklistUserEntity.Name,
                    ImId = blacklistUserEntity.ImId,
                    BlackUserId = blacklistUserId
                };
                await _blacklistUserRepository.InsertAsync(blacklistUser);
                //关系界面
                await _oftenContactRepository.SetBlackUserAsync(userId, blacklistUserId);
                return true;
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
                throw new BusinessException("用户不存在");
            if (response.StatusCode == HttpStatusCode.Unauthorized)
                throw new BusinessException("未授权[无token、token错误、token过期]");
            if (response.StatusCode == HttpStatusCode.TooManyRequests)
                throw new BusinessException("接口被限流");
            return true;
        }
        /// <summary>
        /// 移除黑名单
        /// </summary>
        /// <param name="blacklistId"></param>
        /// <returns></returns>
        public async Task<bool> ReMoveBlacklistUserAsync(int blackUserId, int userId)
        {
            var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
            var blacklistUser = await _blacklistUserRepository.GetBlackUserAsync(userId, blackUserId);
            if (blacklistUser == null)
                return false;
            var token = await _cache.GetAsync<string>("ImToken");
            var user = await _userRepository.GetAsync(blacklistUser.UserId);
            //环信添加黑名单
            var url = $"{imServiceUrl}/users/{user.ImId}/blocks/users/{blacklistUser.ImId}";
            var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
            var response =
                await HttpClientHelper.DeleteAsync(url, headers);
            if (response.IsSuccessStatusCode)
            {
                await _blacklistUserRepository.DeleteAsync(blacklistUser.Id);
                //关系界面
                await _oftenContactRepository.SetBlackUserAsync(blacklistUser.UserId, blacklistUser.BlackUserId);
                return true;
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
                throw new BusinessException("用户不存在");
            if (response.StatusCode == HttpStatusCode.Unauthorized)
                throw new BusinessException("未授权[无token、token错误、token过期]");
            if (response.StatusCode == HttpStatusCode.TooManyRequests)
                throw new BusinessException("接口被限流");
            return true;
        }
    }
}