using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Datory;
using GxPress.Common.Tools;
using GxPress.Request.App.OftenContact;
using GxPress.Request.GroupChatUser;
using GxPress.Result.App.OftenContact;

namespace GxPress.Service.Implement.OftenContact
{
    public partial class OftenContactService
    {
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="individualGroupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<OftenContactInfo>> GetOftenContacts(OftenContactSearchRequest request)
        {

            //获取文件夹数据
            var result = new List<OftenContactInfo>();
            var data = await _repository.GetContactInfosAsync(request);
            foreach (var oftenContact in data)
            {
                if (oftenContact.ChatType == 1)
                {
                    var user = await _userRepository.GetAsync(int.Parse(oftenContact.ContactUserId));
                    if (user == null)
                        continue;
                    oftenContact.Name = user.Name;
                    oftenContact.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
                    oftenContact.ImId = user.ImId;
                    oftenContact.ToUserId = user.Id;
                }

                if (oftenContact.ChatType == 2)
                {
                    var groupChat = await _groupChatRepository.GetAsync(Q.Where(nameof(Entity.GroupChat.GroupChatImId),
                        oftenContact.ContactUserId));
                    if (groupChat == null)
                        continue;
                    oftenContact.Name = groupChat.Name;
                    oftenContact.AvatarUrl = StringUtils.AddDomainMin(groupChat.AvatarUrl);
                    oftenContact.ImId = groupChat.GroupChatImId;
                    var groupChatUserIsDisturbRequest = new GroupChatUserIsDisturbRequest();
                    groupChatUserIsDisturbRequest.GroupChatId = groupChat.Id;
                    groupChatUserIsDisturbRequest.UserId = oftenContact.UserId;
                    oftenContact.IsDisturb = await groupChatUserRepository.IsDisturbAsync(groupChatUserIsDisturbRequest);
                }
            }
            result = data.ToList();
            return result;
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="individualGroupId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<OftenContactInfo>> GetOftenContactsAll(OftenContactSearchRequest request)
        {

            //获取文件夹数据
            var result = new List<OftenContactInfo>();
            var data = await _repository.GetContactInfoAllAsync(request);
            foreach (var oftenContact in data)
            {
                if (oftenContact.ChatType == 1)
                {
                    var user = await _userRepository.GetAsync(int.Parse(oftenContact.ContactUserId));
                    if (user == null)
                        continue;
                    oftenContact.Name = user.Name;
                    oftenContact.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
                    oftenContact.ImId = user.ImId;
                    oftenContact.ToUserId = user.Id;
                }

                if (oftenContact.ChatType == 2)
                {
                    var groupChat = await _groupChatRepository.GetAsync(Q.Where(nameof(Entity.GroupChat.GroupChatImId),
                        oftenContact.ContactUserId));
                    if (groupChat == null)
                        continue;
                    oftenContact.Name = groupChat.Name;
                    oftenContact.AvatarUrl = StringUtils.AddDomainMin(groupChat.AvatarUrl);
                    oftenContact.ImId = groupChat.GroupChatImId;
                    var groupChatUserIsDisturbRequest = new GroupChatUserIsDisturbRequest();
                    groupChatUserIsDisturbRequest.GroupChatId = groupChat.Id;
                    groupChatUserIsDisturbRequest.UserId = oftenContact.UserId;
                    oftenContact.IsDisturb = await groupChatUserRepository.IsDisturbAsync(groupChatUserIsDisturbRequest);
                }
            }
            result = data.ToList();
            return result;
        }

    }
}
