﻿using System;
using MongoDB.Driver;

namespace MongoDbRepository.DBManager
{
    public static class InstanceHelper<IHT>
    {
        public static IMongoCollection<T> GetCollectionFromConnectionString<T>(string connectionString, string collectionName)
            where T : IEntity<IHT>
        {
            return GetDatabaseFromUrl(new MongoUrl(connectionString)).GetCollection<T>(collectionName);
        }

      

        public static IMongoCollection<T> GetCollectionFromUrl<T>(MongoUrl url, string collectionName)
            where T : IEntity<IHT>
        {
            return GetDatabaseFromUrl(url).GetCollection<T>(collectionName);
        }        

        /// <summary>
        /// Creates and returns a MongoDatabase from the specified url.
        /// </summary>
        /// <param name="url">The url to use to get the database from.</param>
        /// <returns>Returns a MongoDatabase from the specified url.</returns>
        private static IMongoDatabase GetDatabaseFromUrl(MongoUrl url)
        {
            var client = new MongoClient(url);
            return client.GetDatabase(url.DatabaseName); // WriteConcern defaulted to Acknowledged
        }
    }
}