'use strict';

var dbConfig = require('../DatabaseConfig');
var connections = {};

module.exports = function (connectionName) {

    if (!connections[connectionName]) {
        var connectionURL;
        var mongo = dbConfig.getDataServicesConfig(connectionName);

        if (mongo.url !== undefined && mongo.url !== ""){
            connectionURL = mongo.url;
        }
        else{
            connectionURL = 'mongodb://';

            if (mongo.userName !== undefined && mongo.userName !== "" &&
                mongo.password !== undefined && mongo.password !== ""){
                connectionURL +=  mongo.userName + ':' + mongo.password + '@';
            }
            connectionURL +=  mongo.host + ':' + mongo.port + '/' + mongo.db + '?autoReconnect=true';

            var maxPoolSize = mongo.maxPoolSize;

            if (!maxPoolSize) {
                maxPoolSize = 5;
            }

            connectionURL += '&maxPoolSize=' + maxPoolSize;
        }
        connections[connectionName] = connectionURL;
    }

    return connections[connectionName];
};