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

namespace GxPress.Repository.Implement.WorkProcess
{
    public class ProcessGroupRepository : IProcessGroupRepository
    {
        private readonly Repository<ProcessGroup> _repository;

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

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<bool> ExistsAsync(int id)
        {
            return await _repository.ExistsAsync(id);
        }

        public async Task<bool> IsNameExistsAsync(string name)
        {
            return await _repository.ExistsAsync(Q.Where(nameof(ProcessGroup.Name), name));
        }

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

        public async Task<ProcessGroup> GetAsync(int id)
        {
             return await _repository.GetAsync(id);
        }

        public async Task<bool> UpdateAsync(ProcessGroup processGroup)
        {
             return await _repository.UpdateAsync(processGroup);
        }

        public async Task<IEnumerable<ProcessGroup>> GetListAsync()
        {
            return await _repository.GetAllAsync();
        }

        /// <summary>
        /// ����
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ProcessGroup> AddAsync(ArticleGroupAddRequest request)
        {
            var exist = await IsNameExistsAsync(request.Name);
            if (exist) throw new BusinessException("�����������ظ�");
            var group = new ProcessGroup
            {
                Name = request.Name,
                Sort = request.Sort
            };

            group.Id = await InsertAsync(group);
            return group;
        }

        /// <summary>
        /// �޸�
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(int id, ArticleGroupAddRequest request)
        {
            var group = await GetAsync(id);
            if (group == null) throw new BusinessException("�÷��鲻����");

            if (group.Name != request.Name)
            {
                var exist = await IsNameExistsAsync(request.Name);
                if (exist) throw new BusinessException("�����������ظ�");
            }

            group.Name = request.Name;
            group.Sort = request.Sort;

            return await UpdateAsync(group);
        }

        /// <summary>
        /// ɾ��
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var group = await GetAsync(id);
            if (group == null) throw new BusinessException("�������鲻����");

            return await _repository.DeleteAsync(id);
        }
    }
}