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

namespace GreenTree.Nachtragsmanagement.Services.DbContext
{
    public class DbContextService : IDbContextService
    {
        #region Fields

        /// <summary>
        /// Current database context
        /// </summary>
        private readonly IDbContext _dbContext;

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the DbContextService class
        /// </summary>
        public DbContextService(IDbContext iDbContext)
        {
            _dbContext = iDbContext;
        }

        #endregion

        #region Services

        /// <summary>
        /// Checks if a DbSet exists in the curent DbContext
        /// </summary>
        /// <typeparam name="TEntity">The entity type of the DbSet.</typeparam>
        public bool DbSetExists<TEntity>() where TEntity : BaseEntity
        {
            var dbSet = _dbContext.Get<TEntity>();

            return dbSet != null;
        }

        /// <summary>
        /// Get the DbSet of the corresponding Entity type
        /// </summary>
        /// <typeparam name="TEntity">The entity type of the DbSet.</typeparam>
        public IDbSet<TEntity> GetDbSet<TEntity>() where TEntity : BaseEntity
        {
            var dbSet = _dbContext.Get<TEntity>();

            return dbSet;
        }

        /// <summary>
        /// Gets the current DbContext
        /// </summary>
        public AppendixObjectContext GetDbContext()
        {
            return (AppendixObjectContext)_dbContext;
        }

        #endregion
    }
}