using System;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
using Datory;
using GxPress.Common.Exceptions;
using GxPress.Request.App.OftenContact;
using GxPress.Result.App.OftenContact;

namespace GxPress.Service.Implement.OftenContact
{
    public partial class OftenContactService
    {
        public async Task<OftenContactDto> InsertAsync(OftenContactInRequest request, int userId)
        {
            var result = new OftenContactDto();
            if (string.IsNullOrEmpty(request.ContactUserId))
                throw new BusinessException("缺少必要字段");
            if (request.ChatType == 0)
                throw new BusinessException("类型必须");
            var count = await _repository.CountAsync(Q.Where(nameof(Entity.OftenContact.UserId), userId)
                .Where(nameof(Entity.OftenContact.ContactUserId), request.ContactUserId));
            if (count > 0)
            {
                //修改好友关系
                var oftenContact = await _repository.GetAsync(Q.Where(nameof(Entity.OftenContact.UserId), userId)
                    .Where(nameof(Entity.OftenContact.ContactUserId), request.ContactUserId));
                if (oftenContact != null)
                {
                    if (request.ChatType == 1)
                    {
                        var middleDto = await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id).Where(nameof(Entity.Middle.Middle.FolderType), 10).Where(nameof(Entity.Middle.Middle.UserId), userId));
                        result.Id = middleDto != null ? middleDto.Id : 0;
                        result.IsTop = middleDto != null ? middleDto.IsTop : false;
                        //修改middle
                        await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), false).Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id).Where(nameof(Entity.Middle.Middle.FolderType), 10));
                        //修改文件夹
                        var middles = await _middleRepository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id).Where(nameof(Entity.Middle.Middle.FolderType), 10));
                        foreach (var middle in middles)
                        {
                            if (middle.ParentId > 0)
                            {
                                await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middle.ParentId).Set(nameof(Entity.Middle.Middle.IsDelete), false));
                            }
                        }
                        //修改聊天时间
                        await _repository.UpdateAsync(Q.Where(nameof(Entity.OftenContact.UserId), userId)
                    .Where(nameof(Entity.OftenContact.ContactUserId), request.ContactUserId).Set(nameof(Entity.OftenContact.IsDelete), false));
                        oftenContact = await _repository.GetAsync(Q.Where(nameof(Entity.OftenContact.UserId), int.Parse(request.ContactUserId))
                            .Where(nameof(Entity.OftenContact.ContactUserId), userId));
                        if (oftenContact != null)
                        {
                            await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), false).Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id).Where(nameof(Entity.Middle.Middle.FolderType), 10));
                            await _repository.UpdateAsync(Q.Where(nameof(Entity.OftenContact.UserId), int.Parse(request.ContactUserId))
                            .Where(nameof(Entity.OftenContact.ContactUserId), userId).Set(nameof(Entity.OftenContact.IsDelete), false));
                            //修改文件夹
                            middles = await _middleRepository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id).Where(nameof(Entity.Middle.Middle.FolderType), 10));
                            foreach (var middle in middles)
                            {
                                if (middle.ParentId > 0)
                                {
                                    await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middle.ParentId).Set(nameof(Entity.Middle.Middle.IsDelete), false));
                                }
                            }
                        }
                    }
                    if (request.ChatType == 2)
                    {
                        var oftenContacts = await _repository.GetAllAsync(Q.Where(nameof(Entity.OftenContact.ContactUserId), request.ContactUserId));
                        await _repository.UpdateAsync(Q.Set(nameof(Entity.OftenContact.IsDelete), false).Where(nameof(Entity.OftenContact.ContactUserId), request.ContactUserId));
                        //修改middle
                        await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsDelete), false).WhereIn(nameof(Entity.Middle.Middle.MiddleId), oftenContacts.Select(n => n.Id)).Where(nameof(Entity.Middle.Middle.FolderType), 10));
                        //修改文件夹
                        var middles = await _middleRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Middle.Middle.MiddleId), oftenContacts.Select(n => n.Id)).Where(nameof(Entity.Middle.Middle.FolderType), 10));
                        foreach (var middle in middles)
                        {
                            if (middle.ParentId > 0)
                            {
                                await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.Id), middle.ParentId).Set(nameof(Entity.Middle.Middle.IsDelete), false));
                            }
                        }
                    }
                }
                return result;
            }
            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    if (request.ChatType == 1)
                    {
                        var oftenContact = new Entity.OftenContact
                        {
                            IndividualGroupId = request.IndividualGroupId,
                            ContactUserId = request.ContactUserId,
                            UserId = userId,
                            ChatType = request.ChatType,
                            IsBlack = false,
                            IsDelete = false
                        };
                        var oftenContactId = await _repository.InsertAsync(oftenContact);
                        //添加middle
                        var middle = new Entity.Middle.Middle();
                        middle.IsAdmin = true;
                        middle.MiddleId = oftenContactId;
                        middle.FolderType = 10;
                        middle.IsDefault = false;
                        middle.IsDelete = false;
                        middle.IsRead = false;
                        middle.IsTop = false;
                        middle.UserId = userId;
                        middle.IsUpload = false;
                        middle.ParentId = request.IndividualGroupId;
                        await _middleRepository.InsertAsync(middle);
                        oftenContact = new Entity.OftenContact
                        {
                            IndividualGroupId = 0,
                            ContactUserId = userId.ToString(),
                            UserId = int.Parse(request.ContactUserId),
                            ChatType = request.ChatType,
                            IsBlack = false,
                            IsDelete = false
                        };
                        oftenContactId = await _repository.InsertAsync(oftenContact);
                        //添加middle
                        middle = new Entity.Middle.Middle();
                        middle.IsAdmin = true;
                        middle.MiddleId = oftenContactId;
                        middle.FolderType = 10;
                        middle.IsDefault = false;
                        middle.IsDelete = false;
                        middle.UserId = int.Parse(request.ContactUserId);
                        middle.IsRead = false;
                        middle.IsTop = false;
                        middle.IsUpload = false;
                        middle.ParentId = request.IndividualGroupId;
                        await _middleRepository.InsertAsync(middle);
                    }
                    else
                    {
                        var oftenContact = new Entity.OftenContact
                        {
                            IndividualGroupId = request.IndividualGroupId,
                            ContactUserId = request.ContactUserId,
                            UserId = userId,
                            ChatType = request.ChatType,
                            IsBlack = false,
                            IsDelete = false
                        };
                        var oftenContactId = await _repository.InsertAsync(oftenContact);
                        //添加middle
                        var middle = new Entity.Middle.Middle();
                        middle.IsAdmin = true;
                        middle.MiddleId = oftenContactId;
                        middle.FolderType = 10;
                        middle.IsDefault = false;
                        middle.IsDelete = false;
                        middle.UserId = userId;
                        middle.IsRead = false;
                        middle.IsTop = false;
                        middle.IsUpload = false;
                        middle.ParentId = request.IndividualGroupId;
                        result.Id = await _middleRepository.InsertAsync(middle);
                    }
                    transactionScope.Complete();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return result;
        }

    }
}
