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 RuleCarbonCopyRepository : IRuleCarbonCopyRepository
    {
        private readonly Repository<RuleCarbonCopy> _repository;

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

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

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

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

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