using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.BlacklistUser;
using Microsoft.Extensions.Options;
using Datory;

namespace GxPress.Repository.Implement
{
    public class BlacklistUserRepository : IBlacklistUserRepository
    {
        private readonly Repository<BlacklistUser> _repository;
        private readonly Repository<User> _userRepository;
        private readonly IMapper _mapper;

        public BlacklistUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<BlacklistUser>(database);
            _userRepository = new Repository<User>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        /// <summary>
        /// 获取黑名单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<BlacklistUser>> GetBlacklistUserAsync(BlacklistUserSearchRequest request)
        {
            var result= await _repository.GetAllAsync(Q.Where(nameof(BlacklistUser.UserId), request.UserId));
            foreach (var item in result)
                item.AvatarUrl=StringUtils.AddDomainMin(item.AvatarUrl);
            return result;
        }

        /// <summary>
        /// 根据ID获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BlacklistUser> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }
        /// <summary>
        /// 根据用户ID查询黑名单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="blackUserId"></param>
        /// <returns></returns>
        public async Task<BlacklistUser> GetBlackUserAsync(int userId, int blackUserId)
        {
            return await _repository.GetAsync(Q.Where(nameof(BlacklistUser.UserId), userId).Where(nameof(BlacklistUser.BlackUserId), blackUserId));
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(int userId, BlacklistUserInRequest request)
        {
            var user = await _userRepository.GetAsync(request.UserId);
            var blacklistUser = new BlacklistUser
            {
                UserId = userId,
                AvatarUrl = user.AvatarUrl,
                BlackUserName = user.Name,
                ImId = user.ImId,
                BlackUserId = request.UserId
            };
            //修改聊天界面黑名单

            return await _repository.InsertAsync(blacklistUser) > 0;
        }

        public async Task<bool> InsertAsync(BlacklistUser request)
        {

            return await _repository.InsertAsync(request) > 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }

        /// <summary>
        /// 查询该用户是否是黑名单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="blackUserId"></param>
        /// <returns></returns>
        public async Task<bool> FindUserIsBlack(int userId, int blackUserId)
        {
            var count = await _repository.CountAsync(Q.Where(nameof(BlacklistUser.UserId), userId)
                .Where(nameof(BlacklistUser.BlackUserId), blackUserId));
            return count > 0;
        }
    }
}
