using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using MTWorkHR.Application.Services.Interfaces;
using MTWorkHR.Core.UnitOfWork;
using MTWorkHR.Core.IRepositories.Base;
using MTWorkHR.Application.Models;
using MTWorkHR.Core.Entities;

namespace MTWorkHR.Application.Services
{
    public class LogService<TEntity> : ILogService<TEntity> where TEntity : class 
    {
        private readonly IUnitOfWorkLog unitOfWork;

        private readonly IRepositoryLog<TEntity> repository;

        public LogService(IUnitOfWorkLog _unitOfWork)
        {
            unitOfWork = _unitOfWork;
            //this.repository = repository;
            repository = (IRepositoryLog<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
        }



        public virtual async Task Create(TEntity input)
        {
            IRepositoryLog<TEntity> repository = (IRepositoryLog<TEntity>)unitOfWork.GetRepositoryByName(input.GetType().Name);
            //var repository = (IRepository<TEntity>)value;
            await repository.AddAsync(input);

            await unitOfWork.CompleteAsync();
        }
         public virtual async Task<PagingResultDto<TEntity>> GetAll(PagingInputDto pagingInputDto)
        {
            var result = await repository.GetAllAsync(pagingInputDto);

            var list = Mapper.MapperObject.Mapper.Map<IList<TEntity>>(result.Item1);

            var response = new PagingResultDto<TEntity>
            {
                Result = list,
                Total = result.Item2
            };

            return response;
        }
    }
}