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

namespace GxPress.Repository.Implement.WorkProcess
{
    public class RuleConditionRepository : IRuleConditionRepository
    {
        private readonly Repository<RuleCondition> _repository;

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

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

        public async Task<int> InsertAsync(RuleCondition conditionRule)
        {
            return await _repository.InsertAsync(conditionRule);
        }

        public async Task<IEnumerable<RuleCondition>> GetListAsync(int nodeId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(RuleCondition.NodeId), nodeId)
                .OrderBy(nameof(RuleCondition.Id))
            );
        }

        public async Task DeleteByNodeIdListAsync(IEnumerable<int> nodeIdList)
        {
            await _repository.DeleteAsync(Q
                .WhereIn(nameof(RuleCondition.NodeId), nodeIdList)
            );
        }
    }
}