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

namespace GxPress.Repository.Implement.WorkProcess
{
    public class ProcessFieldRepository : IProcessFieldRepository
    {
        private readonly Repository<ProcessField> _repository;

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

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        public async Task<int> InsertAsync(ProcessField processField)
        {
            return await _repository.InsertAsync(processField);
        }

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

        public async Task DeleteByProcessIdAsync(int processId, List<FormField> formFields)
        {
            await _repository.DeleteAsync(Q.Where(nameof(ProcessField.ProcessId), processId).WhereNotIn(nameof(ProcessField.Id), formFields.Select(n => n.Id)));
        }
        public async Task<bool> UpdateAsync(ProcessField processField)
        {
            return await _repository.UpdateAsync(processField);
        }

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