using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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.AdminVerify;
using GxPress.Request.App.Middle;
using GxPress.Service.Interface.AdminVerify;
using GxPress.Service.Interface.Middle;

namespace GxPress.Service.Implement.AdminVerify
{
    public class AdminVerifyInsetService : IAdminVerifyInsertService
    {
        private readonly IAdminVerifyRepository _adminVerifyRepository;
        private readonly IUserRepository _userRepository;
        private readonly IDepartmentRepository _departmentRepository;
        private readonly IMiddleService _middleService;
        private readonly IGroupRepository _groupRepository;
        private readonly IGroupChatRepository _groupChatRepository;
        private readonly IMiddleRepository _middleRepository;
        private readonly IGroupUserRepository _groupUserRepository;
        public AdminVerifyInsetService(IAdminVerifyRepository adminVerifyRepository, IUserRepository userRepository,
            IDepartmentRepository departmentRepository, IMiddleService middleService, IGroupRepository groupRepository,
            IGroupChatRepository groupChatRepository, IMiddleRepository middleRepository, IGroupUserRepository groupUserRepository)
        {
            _adminVerifyRepository = adminVerifyRepository;
            _userRepository = userRepository;
            _departmentRepository = departmentRepository;
            _groupRepository = groupRepository;
            _middleService = middleService;
            _groupChatRepository = groupChatRepository;
            _middleRepository = middleRepository;
            _groupUserRepository = groupUserRepository;
        }

        /// <summary>
        /// 添加成员
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(AdminVerifyInsertRequest request)
        {
            var adminVerifys = new List<Entity.AdminVerify>();
            //获取邀请人信息
            var userDto = await _userRepository.GetAsync(request.UserId);
            //获取员工所有信息
            var users = await _userRepository.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), request.UserIds));
            var departments = await _departmentRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), users.Select(n => n.DepartmentId)));
            foreach (var userId in request.UserIds)
            {
                var adminVerify = new Entity.AdminVerify();
                var adminVerifyDto = new Entity.AdminVerify();
                //获取用户
                var user = users.FirstOrDefault(n => n.Id == userId);
                if (user == null)
                    continue;
                var department = departments.FirstOrDefault(n => n.Id == user.DepartmentId);
                var group = await _groupRepository.GetAsync(request.SourceId);
                //查询是否存在
                adminVerify = await _adminVerifyRepository.GetAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), userId).Where(nameof(Entity.AdminVerify.SourceType), request.SourceType).Where(nameof(Entity.AdminVerify.SourceId), group.Id).WhereIn(nameof(Entity.AdminVerify.DisposeType), new List<int> { 0, 1 }));
                if (adminVerify != null)
                    continue;
                adminVerify = new Entity.AdminVerify();
                //申请
                if (request.SourceType == 1)
                {
                    var ids = new List<int> { GroupUserRoleTyeConst.Admin.GetHashCode(), GroupUserRoleTyeConst.SuperAdmin.GetHashCode() };
                    //创建人
                    var groupUsers = await _groupUserRepository.GetAllAsync(Q.Where(nameof(GroupUser.GroupId), group.Id).WhereIn(nameof(GroupUser.GroupUserRoleId), ids));
                    foreach (var groupUser in groupUsers)
                    {
                        //设置群主验证
                        await InsertMiddleVerify(groupUser.UserId);
                        adminVerifyDto.AvatarUrl = StringUtils.RemoveDomain(user.AvatarUrl);
                        adminVerifyDto.SourceName = group.Name;
                        adminVerifyDto.Name = user.Name;
                        adminVerifyDto.DisposeType = AdminVerifyTypeConst.UDispose.GetHashCode();
                        adminVerifyDto.VerifyType = request.VerifyType;
                        adminVerifyDto.AdminId = groupUser.UserId;
                        adminVerifyDto.SourceType = request.SourceType;
                        adminVerifyDto.SourceId = request.SourceId;
                        adminVerifyDto.UserId = userId;
                        adminVerifyDto.IsDelete = "";
                        //获取部门
                        adminVerifyDto.Remark = department.Name + "-" + user.Name;
                        //await _adminVerifyRepository.InsertAsync(adminVerifyDto);
                        adminVerifys.Add(adminVerifyDto);
                    }
                }
                //邀请
                if (request.SourceType == 2)
                {
                    //await _adminVerifyRepository.InsertAsync(adminVerifyDto);
                    adminVerifys.Add(adminVerifyDto);
                    adminVerify.AvatarUrl = StringUtils.RemoveDomain(userDto.AvatarUrl);
                    adminVerify.Name = userDto.Name;
                    adminVerify.DisposeType = AdminVerifyTypeConst.UDispose.GetHashCode();
                    adminVerify.VerifyType = request.VerifyType;
                    adminVerify.AdminId = userId;
                    adminVerify.SourceType = request.SourceType;
                    adminVerify.SourceId = request.SourceId;
                    adminVerify.UserId = request.UserId;
                    adminVerify.SourceName = group.Name;
                    adminVerify.IsDelete = "";
                    //获取部门
                    adminVerify.Remark = department.Name + "-" + userDto.Name;
                    //await _adminVerifyRepository.InsertAsync(adminVerify);
                    adminVerifys.Add(adminVerify);
                    await InsertMiddleVerify(userId);
                }
            }
            await _adminVerifyRepository.InsertAsync(adminVerifys);
            return true;
        }
        /// <summary>
        /// 设置用户通知验证
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private async Task InsertMiddleVerify(int userId)
        {
            var middle = await _middleRepository.GetMiddleAsync(Q
                .Where(nameof(Entity.Middle.Middle.AttributeValue), 3)
                .Where(nameof(Entity.Middle.Middle.UserId), userId));
            if (middle == null)
            {
                //添加验证信息
                var middleInsertTypeRequest = new MiddleInsertTypeRequest
                {
                    AttributeValue = 3,
                    FolderId = 0,
                    FolderType = 1,
                    MiddleId = 0,
                    MiddleSonId = 0,
                    UserId = userId
                };
                await _middleService.InsertAsync(middleInsertTypeRequest);
            }
            else if (middle.IsDelete)
            {
                middle.IsDelete = false;
                await _middleRepository.UpdateAsync(middle);
            }
        }
    }
}
