using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.EntityFrameworkCore;
using Quadarax.Foundation.Core.Data.Domain;
using Quadarax.Foundation.Core.Data.Entity;
using Quadarax.Foundation.Core.Data.Extensions;
using Quadarax.Foundation.Core.Data.Interface.Domain;
using Quadarax.Foundation.Core.Data.Interface.Repository;

namespace Quadarax.Foundation.Core.Data.Repository
{
    public abstract class EntityRepository<TKey, TEntity> : IDaoRepository<TKey, TEntity> 
        where TEntity : Entity<TKey>, new() 
        where TKey : IEquatable<TKey>
    {
        private IUnitOfWork<DataDomain> _unitOfWork;
        private DbSet<TEntity> DbSetInstance => _unitOfWork.Context.GetDbSet<DbSet<TEntity>>();
        protected DataDomain Context => _unitOfWork.Context;
        
        protected IList<string> DefaultIncludes { get; }

        protected EntityRepository(IUnitOfWork<DataDomain> unitOfWork)
        {
            _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
            DefaultIncludes = new List<string>();
            //_dbSet = domain.GetDbSet<DbSet<TEntity>>();
        }

        protected virtual IQueryable<TEntity> OnQuery(IPaging paging,bool usingUncommited=false)
        {
            var local = DbSetInstance.Local.Page(paging.IsDisabled ? (int?) null : paging.Page, paging.PageSize).AsQueryable();
            if (local.Any() && usingUncommited)
            {
                return local.MultiInclude(DefaultIncludes.ToArray());
            }

            return DbSetInstance.Page(paging.IsDisabled ? (int?) null : paging.Page, paging.PageSize).MultiInclude(DefaultIncludes.ToArray());
        }

        public IQueryable<TEntity> Query(IPaging paging, bool usingUncomited = false)
        {
            return OnQuery(paging, usingUncomited);
        }

        public virtual TEntity Get(TKey id)
        {
            return Query(Paging.NoPaging).MultiInclude(DefaultIncludes.ToArray()).FirstOrDefault(x => Equals(x.Id, id));
        }

        public virtual IQueryable<TEntity> Get(IEnumerable<TKey> ids)
        {
            return Query(Paging.NoPaging).Where(x => ids.Contains(x.Id));
        }

        public virtual TEntity Set(TEntity entity)
        {
            //DbSetInstance.Update(entity);
            return entity;
        }

        public virtual IEnumerable<TEntity> Set(IEnumerable<TEntity> entities)
        {
            return entities.Select(Set).ToList();
        }

        public virtual TEntity New()
        {
            return DbSetInstance.Add(new TEntity()).Entity;
        }

        public virtual bool Remove(TKey id)
        {
            DbSetInstance.Remove(Get(id));
            return true;
        }

        public virtual IEnumerable<bool> Remove(IEnumerable<TKey> ids)
        {
            return ids.Select(Remove).ToList();
        }

        public virtual bool Remove(TEntity entity)
        {
            DbSetInstance.Remove(entity);
            return true;
        }

        public virtual IEnumerable<bool> Remove(IEnumerable<TEntity> entities)
        {
            return entities.Select(Remove).ToList();
        }

        public void Commit()
        {
            _unitOfWork.Commit();
        }
    }
}
