﻿namespace BugLogger.Data.Repositories
{
    using System.Data.Entity;
    using System.Linq;

    using BugLogger.Data;

    public class Repository<T> : IRepository<T> where T : class
    {
        private DbContext db;
        private IDbSet<T> set;

        public Repository()
            : this(new BugLoggerDbContext())
        {
        }

        public Repository(DbContext context)
        {
            this.db = context;
            this.set = context.Set<T>();
        }

        public IQueryable<T> All()
        {
            return this.set;
        }

        public T Find(object id)
        {
            return this.set.Find(id);
        }

        public void Add(T entity)
        {
            this.ChangeEntityState(entity, EntityState.Added);
        }

        public void Update(T entity)
        {
            this.ChangeEntityState(entity, EntityState.Modified);
        }

        public T Delete(T entity)
        {
            this.ChangeEntityState(entity, EntityState.Deleted);
            return entity;
        }

        public T Delete(object id)
        {
            var entity = this.Find(id);
            this.Delete(entity);
            return entity;
        }

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

        private void ChangeEntityState(T entity, EntityState state)
        {
            var entry = this.db.Entry(entity);

            if (entry.State == EntityState.Detached)
            {
                this.set.Attach(entity);
            }

            entry.State = state;
        }
    }
}
