using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FakeBank_DAL.Infrastructure;
using FakeBank_DAL.Reposity;
using System.Data.Entity;
namespace FakeBank_DAL.Implementations
{
    public class GenericRepesitory<T> : IGenericReposity<T> where T : class
    {
        DbSet<T> DbSetEntity;
        public bool Create(T c)
        {
            bool à_retourner = false;
            try
            {
            using (FakeBankDBContext contexte = new FakeBankDBContext())
            {
                // Create en utilisant Attach !
                //contexte.Set<T>;
                DbSetEntity = contexte.Set<T>();
                DbSetEntity.Attach(c);
                contexte.Entry(c).State = EntityState.Added;

                contexte.SaveChanges();
                à_retourner = true;
            }
            }
            catch (Exception ex)
            {
                Console.WriteLine("LOG : Attention, il y a eu l'exception : " + ex.Message);
            }
            return à_retourner;
        }



        public T FindById(int id)
        {
            T entity = null;
            using (FakeBankDBContext contexte = new FakeBankDBContext())
            {
                // La methode la plus simple
                DbSetEntity = contexte.Set<T>();
                entity = DbSetEntity.Find(id);
            }
            return entity;
        }


        public List<T> FindAll()
        {
            List<T> resultat = null;

            using (FakeBankDBContext ctxt = new FakeBankDBContext())
            {
                DbSetEntity = ctxt.Set<T>();
                ctxt.Configuration.AutoDetectChangesEnabled = false;
                resultat = DbSetEntity.AsNoTracking().ToList<T>();
            }
            return resultat;
        }
        public bool Update(T c)
        {
            bool à_retourner = false;
            try
            {
                using (FakeBankDBContext ctxt = new FakeBankDBContext())
                {
                    DbSetEntity = ctxt.Set<T>();
                    DbSetEntity.Attach(c);
                    ctxt.Entry(c).State = EntityState.Modified;
                    ctxt.SaveChanges();
                    à_retourner = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("LOG : Attention il y a eu l'exception " + ex.Message);
            }

            return à_retourner;

        }

        public bool Delete(T d)
        {
            bool à_retourner = false;
            try
            {
                using (FakeBankDBContext ctxt = new FakeBankDBContext())
                {
                    DbSetEntity = ctxt.Set<T>();
                    
                    DbSetEntity.Attach(d);
                    ctxt.Entry(d).State = EntityState.Deleted;
                    ctxt.SaveChanges();
                    à_retourner = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("LOG : Attention, il y a l'exception :" + ex.Message); ;
            }
            return à_retourner;
        }

    }
}
