using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.EnumConst;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.AdminVerify;
using GxPress.Request.App.GroupUser;
using GxPress.Result.App.AdminVerify;
using GxPress.Service.Interface;
using GxPress.Service.Interface.AdminVerify;
using GxPress.Service.Interface.Group;
using GxPress.Service.Interface.Middle;

namespace GxPress.Service.Implement.AdminVerify
{
    public class AdminVerifyService : IAdminVerifyService
    {
        private readonly IAdminVerifyRepository _adminVerifyRepository;
        private readonly IUserRepository _userRepository;
        private readonly IDepartmentRepository _departmentRepository;
        private readonly IGroupChatRepository _groupChatRepository;
        private readonly IGroupChatUserService _groupChatUserService;
        private readonly IGroupService _groupService;
        private readonly IMiddleService _middleService;
        private readonly IGroupRepository _groupRepository;
        private readonly IGroupUserRepository _groupUserRepository;
        private readonly IGroupChatUserRepository _groupChatUserRepository;
        private readonly IMapper _mapper;

        public AdminVerifyService(IAdminVerifyRepository adminVerifyRepository, IUserRepository userRepository,
            IDepartmentRepository departmentRepository, IGroupChatRepository groupChatRepository,
            IGroupService groupService, IMiddleService middleService, IGroupChatUserService groupChatUserService,
            IGroupRepository groupRepository, IMapper mapper, IGroupUserRepository groupUserRepository, IGroupChatUserRepository groupChatUserRepository)
        {
            _adminVerifyRepository = adminVerifyRepository;
            _userRepository = userRepository;
            _departmentRepository = departmentRepository;
            _groupChatRepository = groupChatRepository;
            _groupService = groupService;
            _middleService = middleService;
            _groupChatUserService = groupChatUserService;
            _groupRepository = groupRepository;
            _mapper = mapper;
            _groupUserRepository = groupUserRepository;
            _groupChatUserRepository = groupChatUserRepository;
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAllAsync(int userId)
        {
            return await _adminVerifyRepository.DeleteAllAsync(userId);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="disposeTyeConst"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(int id, AdminVerifyTypeConst disposeTyeConst)
        {
            var adminVerify = await GetAsync(id);
            if (disposeTyeConst == AdminVerifyTypeConst.Approve)
            {
                //小组
                if (adminVerify.VerifyType == GroupTypeConst.Group)
                {
                    var request = new GroupUserInRequest();
                    request.GroupId = adminVerify.SourceId;
                    //添加用户
                    if (adminVerify.SourceType == 2)
                        request.UserIds.Add(adminVerify.AdminId);
                    else
                        request.UserIds.Add(adminVerify.UserId);
                    await _groupService.AddUsersAsync(request);
                }
            }
            //修改状态
            await _adminVerifyRepository.UpdateAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), adminVerify.AdminId).Where(nameof(Entity.AdminVerify.UserId), adminVerify.UserId).Where(nameof(Entity.AdminVerify.SourceType), adminVerify.SourceType).Where(nameof(Entity.AdminVerify.SourceId), adminVerify.SourceId).Set(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.Approve.GetHashCode()));
            await _adminVerifyRepository.UpdateAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), adminVerify.UserId).Where(nameof(Entity.AdminVerify.UserId), adminVerify.AdminId).Where(nameof(Entity.AdminVerify.SourceType), adminVerify.SourceType).Where(nameof(Entity.AdminVerify.SourceId), adminVerify.SourceId).Set(nameof(Entity.AdminVerify.DisposeType), disposeTyeConst.GetHashCode()));
            return true;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entity.AdminVerify> GetAsync(int id)
        {
            return await _adminVerifyRepository.GetAsync(id);
        }

        /// <summary>
        /// 根据用户查询
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<AdminVerifySearchResult>> GetAllAsync(int userId)
        {
            var user = await _userRepository.GetAsync(userId);
            var adminVerify = await _adminVerifyRepository.GetAllAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), userId).OrderByDesc(nameof(Entity.AdminVerify.CreatedDate)));
            var result = adminVerify.Select(n => _mapper.Map<AdminVerifySearchResult>(n));
            var adminVerifySearchResults = result as AdminVerifySearchResult[] ?? result.ToArray();
            foreach (var item in adminVerifySearchResults)
            {
                item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
                //是否小组
                if (item.VerifyType == GroupTypeConst.Group)
                {
                    //获取小组成员
                    var groupUsers = await _groupUserRepository.GetAllAsync(Q.Where(nameof(GroupUser.GroupId), item.SourceId));
                    //不在小组
                    if (groupUsers.All(n => n.UserId != userId))
                        item.IsVisit = false;
                    var roleId = new List<int> { 1, 2 };
                    var groupUsersRole = groupUsers.Where(n => roleId.Contains(n.GroupUserRoleId.GetHashCode()));
                    //判断是否有操作权限
                    if (item.SourceType == 1)
                    {
                        if (groupUsersRole.Any(n => n.UserId == item.AdminId) && item.DisposeType == 0)
                            item.IsOperate = true;
                        else
                            item.IsOperate = false;
                    }
                    else
                    {
                        if (groupUsersRole.Any(n => n.UserId == item.AdminId))
                            item.IsOperate = false;
                        else if (item.DisposeType == 0)
                            item.IsOperate = true;
                    }
                }
            }
            return adminVerifySearchResults;
        }

        /// <summary>
        /// 获取未处理条数
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<int> GetUDisposeCount(int userId)
        {
            return await _adminVerifyRepository.GetUDisposeCount(userId);
        }
    }
}
