using System;
using System.Threading.Tasks;
using System.Transactions;
using Datory;
using GxPress.Request.App.IM;
using GxPress.Service.Interface.GroupChat;
using GxPress.Repository.Interface;
using System.Collections.Generic;
using System.Linq;

namespace GxPress.Service.Implement.GroupChat
{
    public class GroupChatService : IGroupChatService
    {
        private readonly IOftenContactRepository oftenContactRepository;
        private readonly IGroupChatRepository groupChatRepository;
        private readonly IMiddleRepository middleRepository;
        private readonly IGroupChatUserRepository groupChatUserRepository;

        public GroupChatService(IOftenContactRepository oftenContactRepository, IGroupChatRepository groupChatRepository, IMiddleRepository middleRepository, IGroupChatUserRepository groupChatUserRepository)
        {
            this.oftenContactRepository = oftenContactRepository;
            this.groupChatRepository = groupChatRepository;
            this.middleRepository = middleRepository;
            this.groupChatUserRepository = groupChatUserRepository;
        }
        /// <summary>
        /// 群添加用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> InsertUsersIntoGroupChatAsync(InsertGroupChatUsersRequest request)
        {
            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    var groupChat =
                        await groupChatRepository.GetAsync(Q.Where(nameof(Entity.GroupChat.GroupChatImId), request.GroupChatImId));
                    if (groupChat == null)
                        return false;
                    //添加用户
                    var oftenContacts = new List<Entity.OftenContact>();
                    var middles = new List<Entity.Middle.Middle>();
                    var groupChatUsers = new List<Entity.GroupChatUser>();
                    foreach (var user in request.Users)
                    {
                        if (user.Id == groupChat.UserId)
                            continue;
                        var oftenContact = new Entity.OftenContact
                        {
                            ChatType = 2,
                            ContactUserId = groupChat.GroupChatImId,
                            IndividualGroupId = 0,
                            UserId = user.Id
                        };
                        oftenContacts.Add(oftenContact);
                    }
                    //绑定关系
                    await oftenContactRepository.InsertAsync(oftenContacts);
                    //查询关系
                    var oftenContactList = await oftenContactRepository.GetAllAsync(Q.Where(nameof(Entity.OftenContact.ChatType), 2).Where(nameof(Entity.OftenContact.ContactUserId), groupChat.GroupChatImId));
                    foreach (var oftenContact in oftenContactList)
                    {
                        if (oftenContact.UserId == groupChat.UserId)
                            continue;
                        //var oftenContactId = await oftenContactRepository.InsertAsync(oftenContact);
                        //添加middle
                        var middle = new Entity.Middle.Middle();
                        middle.IsAdmin = true;
                        middle.MiddleId = oftenContact.Id;
                        middle.FolderType = 10;
                        middle.IsDefault = false;
                        middle.IsDelete = false;
                        middle.UserId = oftenContact.UserId;
                        middle.IsRead = false;
                        middle.IsTop = false;
                        middle.ParentId = 0;

                        //await middleRepository.InsertAsync(middle);
                        //查询用户信息
                        var user = request.Users.FirstOrDefault(n => n.Id == oftenContact.UserId);
                        if (user == null)
                            continue;
                        var groupChatUser = new Entity.GroupChatUser
                        {
                            GroupChatId = groupChat.Id,
                            UserId = oftenContact.UserId,
                            ImId = user.ImId,
                            GroupChatImId = request.GroupChatImId,
                            IsAdmin = false
                        };
                        groupChatUsers.Add(groupChatUser);
                        middles.Add(middle);
                        //创建环信Mi群
                        // var id = await groupChatUserRepository.InsertAsync(groupChatUser);
                    }
                    await middleRepository.InsertAsync(middles);
                    //
                    await groupChatUserRepository.InsertAsync(groupChatUsers);
                    transactionScope.Complete();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return true;
        }

        public async Task<bool> DeleteAsync(int groupChatId)
        {
            return await groupChatRepository.DeleteAsync(groupChatId);
        }
    }
}