let redisClient;
const createRedisClient = (config) => {
    let client = null;

    const redisConfig = config || require('./RedisConfig').getConfig();

    if (redisConfig && redisConfig.port && redisConfig.host) {

        const options = {
            host: redisConfig.host,
            port: redisConfig.port
        };

        if (redisConfig.databaseNumber) {
            options.db = redisConfig.databaseNumber;
        }

        if (redisConfig.auth) {
            options.password = redisConfig.auth.password;
        }

        client = require('redis').createClient(options);
    }

    if (!client) {
        logger.error("Redis is misconfigured.");
    }

    return client;
};

const getRedisClient = (config) => {
  if (!redisClient || !redisClient.connected) {
    redisClient = createRedisClient(config);
  }

  return redisClient;
};

module.exports = {
    createRedisClient,
    getRedisClient
};
