using DocuMed.Repository.Abstracts;

namespace DocuMed.Repository.Repositories.Base
{
    public class BaseRepository<T> : Repository<T>, IBaseRepository<T> where T : class, IApiEntity
    {
        protected readonly ICurrentUserService CurrentUserService;

        public BaseRepository(ApplicationContext dbContext,ICurrentUserService currentUserService) : base(dbContext)
        {
            CurrentUserService = currentUserService;
        }

        public virtual async ValueTask<T> GetByIdAsync(CancellationToken cancellationToken, params object[] ids)
        {
            return await Entities.FindAsync(ids, cancellationToken);
        }

        #region Sync Methods

        public virtual T GetById(params object[] ids)
        {
            var ent = Entities.Find(ids);
            Detach(ent);
            return ent;
        }

        public virtual void Add(T entity)
        {
            AssertExtensions.NotNull(entity, nameof(entity));
            Entities.Add(entity);
        }

        public virtual void AddRange(IEnumerable<T> entities)
        {
            AssertExtensions.NotNull(entities, nameof(entities));
            Entities.AddRange(entities);
        }

        public virtual void Update(T entity)
        {
            AssertExtensions.NotNull(entity, nameof(entity));
            Detach(entity);
            Entities.Update(entity);
        }

        public virtual void UpdateRange(IEnumerable<T> entities)
        {
            AssertExtensions.NotNull(entities, nameof(entities));
            Entities.UpdateRange(entities);
        }

        public void HardDelete(T entity)
        {
            AssertExtensions.NotNull(entity, nameof(entity));
            Entities.Remove(entity);
        }

        public virtual void Delete(T entity)
        {
            AssertExtensions.NotNull(entity, nameof(entity));

            Entities.Entry(entity).Property(e => e.RemovedAt)
                .CurrentValue = DateTime.Now;
            Entities.Entry(entity).Property(e => e.IsRemoved)
                .CurrentValue = true;
            if (CurrentUserService.UserName != null)
                Entities.Entry(entity).Property(e => e.RemovedBy)
                    .CurrentValue = CurrentUserService.UserName;
            Entities.Update(entity);
        }

        public virtual void DeleteRange(IEnumerable<T> entities)
        {
            var apiEntities = entities.ToList();
            AssertExtensions.NotNull(apiEntities, nameof(entities));
            foreach (var entity in apiEntities)
            {
                Entities.Entry(entity).Property(e => e.RemovedAt)
                    .CurrentValue = DateTime.Now;
                Entities.Entry(entity).Property(e => e.IsRemoved)
                    .CurrentValue = true;
                if (CurrentUserService.UserName != null)
                    Entities.Entry(entity).Property(e => e.RemovedBy)
                        .CurrentValue = CurrentUserService.UserName;
                Entities.Update(entity);
            }
        }

        #endregion

        #region Attach & Detach

        public virtual void Detach(T entity)
        {
            AssertExtensions.NotNull(entity, nameof(entity));
            var entry = DbContext.Entry(entity);
            if (entry != null)
                entry.State = EntityState.Detached;
        }

        public virtual void Attach(T entity)
        {
            AssertExtensions.NotNull(entity, nameof(entity));
            if (DbContext.Entry(entity).State == EntityState.Detached)
                Entities.Attach(entity);
        }

        #endregion
    }
}