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 ProcessNodeRepository : IProcessNodeRepository
    {
        private readonly Repository<ProcessNode> _repository;
        private readonly IRuleCarbonCopyRepository _ruleCarbonCopyRepository;
        private readonly IRuleApproverCheckRepository _ruleApproverCheckRepository;
        private readonly IRuleConditionRepository _ruleConditionRepository;

        public ProcessNodeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IRuleCarbonCopyRepository ruleCarbonCopyRepository, IRuleApproverCheckRepository ruleApproverCheckRepository, IRuleConditionRepository ruleConditionRepository)
        {
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<ProcessNode>(database);
            _ruleCarbonCopyRepository = ruleCarbonCopyRepository;
            _ruleApproverCheckRepository = ruleApproverCheckRepository;
            _ruleConditionRepository = ruleConditionRepository;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<IEnumerable<ProcessNode>> GetListAsync(int processId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(ProcessNode.ProcessId), processId)
                .OrderBy(nameof(ProcessNode.Id))
            );
        }

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

        public async Task DeleteByProcessIdAsync(int processId)
        {
            var nodeIdList = await _repository.GetAllAsync<int>(Q
                .Select(nameof(ProcessNode.Id))
                .Where(nameof(ProcessNode.ProcessId), processId)
            );
            await _ruleCarbonCopyRepository.DeleteByNodeIdListAsync(nodeIdList);
            await _ruleApproverCheckRepository.DeleteByNodeIdListAsync(nodeIdList);
            await _ruleConditionRepository.DeleteByNodeIdListAsync(nodeIdList);
            await _repository.DeleteAsync(Q.Where(nameof(ProcessNode.ProcessId), processId));
        }

        public async Task<IEnumerable<ProcessNode>> GetListAsync(int processId, int parentId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(ProcessNode.ProcessId), processId)
                .Where(nameof(ProcessNode.ParentId), parentId)
                .OrderBy(nameof(ProcessNode.Id))
            );
        }

        public async Task<IEnumerable<ProcessNode>> GetAllAsync(int processId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(ProcessNode.ProcessId), processId)
                .OrderBy(nameof(ProcessNode.Id))
            );
        }
    }
}