
using Microsoft.EntityFrameworkCore;

namespace Netina.Repository.Repositories.Base
{
    public class WriteRepository<T> : Repository<T>, IDisposable, IWriteRepository<T> where T : class, IApiEntity
    {
        private readonly ICurrentUserService _currentUserService;

        public WriteRepository(ApplicationContext dbContext,ICurrentUserService currentUserService) : base(dbContext)
        {
            _currentUserService = currentUserService;
        }

        public void Dispose()
        {
            DbContext?.Dispose();
        }

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

        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 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);
            }
        }

        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);
        }
    }
}