using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAOs
{
    public abstract class AbstractDAO<TEntity> : IDisposable,
       InterfaceDAO<TEntity> where TEntity : class
    {
        BancoContext db = new BancoContext();

        public void Incluir(TEntity objeto)
        {
            this.db.Set<TEntity>().Add(objeto);
        }

        public void Excluir(Func<TEntity, bool> predicate)
        {
            this.db.Set<TEntity>().Where(predicate).ToList()
               .ForEach(del => this.db.Set<TEntity>().Remove(del));
        }

        public void Atualizar(TEntity objeto)
        {
            this.db.Entry(objeto).State = EntityState.Modified;
        }

        public void DefinirSemAlteracoes(TEntity objeto)
        {
            this.db.Entry(objeto).State = EntityState.Unchanged;
        }

        public void Salvar()
        {
            this.db.SaveChanges();
        }

        public TEntity Carregar(params object[] key)
        {
            return this.db.Set<TEntity>().Find(key);
        }

        public IQueryable<TEntity> Listar(Func<TEntity, bool> predicate)
        {
            return ListarTodos().Where(predicate).AsQueryable();
        }

        public IQueryable<TEntity> ListarTodos()
        {
            return this.db.Set<TEntity>();
        }


        public void Dispose()
        {
            db.Dispose();
        }
    }
}