using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Repository.Interface.SystemRole;
using Microsoft.Extensions.Options;

namespace GxPress.Repository.Implement.SystemRole
{
    public class SystemRoleRepository : ISystemRoleRepository
    {
        private readonly Repository<Entity.SystemRole.SystemRole> _repository;
        private readonly IMapper _mapper;

        public SystemRoleRepository(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<Entity.SystemRole.SystemRole>(database);
            _mapper = mapper;
        }

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

        public async Task<int> InsertAsync(Entity.SystemRole.SystemRole model)
        {
            return await _repository.InsertAsync(model);

        }

        public async Task<Entity.SystemRole.SystemRole> GetRoleAsync(int id)
        {
            return await _repository.GetAsync(id);
        }
        public async Task<bool> UpdateAsync(Entity.SystemRole.SystemRole model)
        {
            return await _repository.UpdateAsync(model);
        }

        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }
        public async Task<IEnumerable<Entity.SystemRole.SystemRole>> GetSystemRolesAllAsync()
        {
            return await _repository.GetAllAsync(Q.OrderByDesc(nameof(Entity.SystemRole.SystemRole.CreatedDate)));
        }
    }
}