using EasyLOB.Persistence;
using EasyLOB.Security;

namespace EasyLOB.AuditTrail.Persistence
{
    public class AuditTrailUnitOfWorkNH : UnitOfWorkNH, IAuditTrailUnitOfWork
    {
        #region Methods
        
        public AuditTrailUnitOfWorkNH(IAuthenticationManager authenticationManager)
            : base(AuditTrailFactory.Session, authenticationManager)
        {
            Domain = ""; // ???
            //Domain = "AuditTrail";

            //ISession session = base.Session;
        }

        public override IGenericRepository<TEntity> GetRepository<TEntity>()
        {
            if (!Repositories.Keys.Contains(typeof(TEntity)))
            {
                var repository = new AuditTrailGenericRepositoryNH<TEntity>(this);
                Repositories.Add(typeof(TEntity), repository);
            }

            return Repositories[typeof(TEntity)] as IGenericRepository<TEntity>;
        }

        #endregion Methods        

        #region Methods IDispose

        private bool disposed = false;

        protected override void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (Session != null)
                    {
                        Session.Dispose();
                        Session = null;
                    }
                }

                disposed = true;

                base.Dispose(disposing);
            }
        }

        #endregion Methods IDispose
    }
}

