using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GxPress.Common.AppOptions;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.RoleGroup;
using GxPress.Result.RoleGroup;
using Microsoft.Extensions.Options;
using Datory;

namespace GxPress.Repository.Implement
{
    public class RoleGroupRepository : IRoleGroupRepository
    {
        private readonly Repository<RoleGroup> _repository;

        public RoleGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
        {
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<RoleGroup>(database);
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<int> InsertAsync(RoleGroup roleGroup)
        {
            return await _repository.InsertAsync(roleGroup);
        }

        public async Task<bool> IsNameExistsAsync(string name)
        {
            return await _repository.ExistsAsync(Q.Where(nameof(RoleGroup.Name), name));
        }

        public async Task<RoleGroup> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }

        public async Task<bool> UpdateAsync(RoleGroup roleGroup)
        {
            return await _repository.UpdateAsync(roleGroup);
        }

        public async Task<IEnumerable<RoleGroup>> GetListAsync()
        {
            return await _repository.GetAllAsync();
        }

        /// <summary>
        /// ����
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<RoleGroup> AddAsync(RoleGroupAddRequest request)
        {
            var exist = await IsNameExistsAsync(request.Name);
            if (exist) throw new BusinessException("��ɫ�������Ѵ���");
            var group = new RoleGroup
            {
                Name = request.Name,
                Sort = request.Sort
            };
            group.Id = await InsertAsync(group);
            return group;
        }

        /// <summary>
        /// �޸�
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(int id, RoleGroupAddRequest request)
        {
            var exist = await IsNameExistsAsync(request.Name);
            if (exist) throw new BusinessException("��ɫ�������Ѵ���");
            var group = await GetAsync(id);
            if (group == null) throw new BusinessException("��ɫ�鲻����");
            group.Name = request.Name;
            group.Sort = request.Sort;
            return await UpdateAsync(group);
        }

        /// <summary>
        /// ɾ��
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var group = await GetAsync(id);
            if (group == null) throw new BusinessException("��ɫ�鲻����");

            return await _repository.DeleteAsync(id);
        }

        /// <summary>
        /// ��ȡ�б�
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleGroupDetailResult>> GetDetailListAsync()
        {
            var list = await GetListAsync();

            var result = list.Select(item => new RoleGroupDetailResult
            {
                Id = item.Id,
                Name = item.Name,
                Sort = item.Sort
            }).ToList();

            return result;
        }
    }
}