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

namespace GxPress.Repository.Implement.VerificationCode
{
    public class VerificationCodeRepository : IVerificationCodeRepository
    {
        private readonly Repository<Entity.VerificationCode.VerificationCode> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public VerificationCodeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
            _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<Entity.VerificationCode.VerificationCode>(database);
            _mapper = mapper;
        }

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


        public async Task<Entity.VerificationCode.VerificationCode> GetAsync(SqlKata.Query query)
        {
            return await _repository.GetAsync(query);
        }

        public async Task<int> InsertAsync(Entity.VerificationCode.VerificationCode verificationCode)
        {
            return await _repository.InsertAsync(verificationCode);
        }

        public async Task<bool> UpdateAsync(Entity.VerificationCode.VerificationCode verificationCode)
        {
            return await _repository.UpdateAsync(verificationCode);
        }

        public async Task<bool> UpdateAsync(SqlKata.Query query)
        {
            return await _repository.UpdateAsync(query) > 0;
        }
    }
}