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

namespace GxPress.Service.Implement.IM
{
    /// <summary>
    /// 移除群成员
    /// </summary>
    public partial class IMService
    {
        /// <summary>
        /// 删除群成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> RemoveGroupChatUsersAsync(InsertGroupChatUsersRequest request)
        {
            var token = await _cache.GetAsync<string>("ImToken");
            var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
            string userValues = string.Empty;
            //获取成员
            var users = await _userRepository.UserListInsAsync(request.UserIds);
            var enumerable = users as User[] ?? users.ToArray();
            foreach (var value in enumerable.Select(n => n.ImId))
            {
                userValues += value + ",";
            }
            if (userValues.Length > 0)
                userValues = userValues.Remove(userValues.Length - 1, 1);
            //创建群
            var url = $"{imServiceUrl}/chatgroups/{request.GroupChatImId}/users/{userValues}";
            var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
            var response =
                await HttpClientHelper.DeleteAsync(url, headers);
            if (response.IsSuccessStatusCode)
            {
                //删除成员
                return await _groupChatRepository.DeleteUsersIntoGroupChatAsync(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>
        /// 根据IMID删除用户
        /// </summary>
        /// <param name="imId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteUserAsync(string imId)
        {
            var imServiceUrl = ConfigHelper.GetValue("ServiceAddress:ImUrl");
            var token = await _cache.GetAsync<string>("ImToken");
            var url = $"{imServiceUrl}/users/{imId}";
            var headers = new Dictionary<string, string> { { "Authorization", "Bearer " + token } };
            var response =
                await HttpClientHelper.DeleteAsync(url, headers);
            if (response.IsSuccessStatusCode)
            {
                //删除成员
                return true;
            }

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