//Include module
var base = require("../lib/index.js");

/**
 * NOTE:
 * All resource: Logger, Redis, Cassandra, Elastic, Mailer, Metrics has same logic
 */

/**
 * To use redis in your project is pretty simple just call base.redis()
 * Module will ensure a stable connection with configuration established
 * in default config "./options.json (redis)" or specified by you using base.setDefaultOptions()
 * in any point before this (see: templates/express)
 */
base.redis();

/**
 * Also you can specify setting just for global redis connection (base.redis()), without afecting base.redis.newInstance(),
 * using base.redis.configurate().
 * 
 * !!! Atention !!!
 * It is recomended to specify all configuration at begin of project using base.setDefaultOptions().
 * If you have need to specify different setting for general(global) connection use base.redis.configurate() BEFORE any used base.redis()
 * If you use after base.redis() it will close previous connection and create a new one with the new configuration
 * 
 * Default setting are:
 */

base.redis.configurate({
    "isCluster": false,
    "sentinels":[
        {"host":"localhost", "port":6380}
    ],
    "sentinel_cluster_name":"mymaster",
    "password":"mypass",
    "keys":{
        "rediskey_connections":"node-base-module:connections:"
    },
    "statusCheckInterval": 5000,
    "port": 6380,
    "host": "localhost",
    "family": 4
});

/**
 * Simple use case
 */
base.redis().set("key", "value", function(isSucces, response){});


/**
 * You alo can create how many connection you want asociating with an unique tag
 * To do dat use base.redis.newInstance(config, tag);
 * Also in this function you can specify any diferent config from the default one
 * 
 * For exemple: if new conection is identical with configuration from default (setted by you with base.setDefaultOptions())
 * but with different port and host can do like this:
 */
base.redis.newInstance({
    "port": 6380,
    "host": "localhost",
}, "unique_tag");


/**
 * If the tag is not unique the old one associated with that tag will be closed and replaced with new one
 * Tags are very important for identify a specific connection, if tag is invalid, do not exist anymore any connection associated
 * with that tag, "null" will be returned by function base.redis(tag).
 * 
 * To get an connection by tag use base.redis(tag);
 */
base.redis("unique_tag");

/**
 * !!! ATENTION !!!
 * Tag "global" is reserved for the global connection
 * So base.redis("global") are equivalent with  base.redis(), both use same connection
 */

 /**
 * Simple use case
 */
base.redis("unique_tag").set("key", "value", function(isSucces, response){});

/**
 * If you do not have any more need by a connection, you can delete it using base.redis.deleteInstance("tag");
 * This function will close and delete connection, if you try tu use it after deletion using base.redis("tag"),
 * function will return "null"
 */
base.redis.deleteInstance("unique_tag");


/**
 * You can also delete global connection using base.redis.deleteInstance("global") or better base.redis.deleteInstance()
 * Deleting the global connection is allowed
 */
base.redis.deleteInstance();

class RedisUsage{
    async test(){
        console.log(await base.redis().promisefy(base.redis().set,"redis_exemple", "test"));
        console.log(await base.redis().promisefy(base.redis().get, "redis_exemple"));
        console.log(await base.redis().promisefy(base.redis().hset, "redis_exemple2", "test2", "test2"));
        console.log(await base.redis().promisefy(base.redis().hget, "redis_exemple2", "test2"));
        console.log(await base.redis().promisefy(base.redis().hgetall, "redis_exemple2"));
        console.log(await base.redis().promisefy(base.redis().keys, "*"));
        console.log(await base.redis().promisefy(base.redis().delete, "redis_exemple"));
        console.log(await base.redis().promisefy(base.redis().countValues, "redis_exemple2"));
        console.log(await base.redis().promisefy(base.redis().hdel, "redis_exemple2", "test2"));

        console.log(await base.redis().promisefy(base.redis().add_to_set, "redis_set", "test3"));
        console.log(await base.redis().promisefy(base.redis().add_to_set, "redis_set", "test4"));

        console.log(await base.redis().promisefy(base.redis().remove_from_set, "redis_set", "test3"));
        console.log(await base.redis().promisefy(base.redis().pop_from_set, "redis_set"));
    }
}


/**
 * !!! ATENTION !!!
 * Deleting the global connection is temporar, until you start using again
 * So if you use base.redis() after deletion will be created a new connection based on your configuration
 * Behavior is same as your first call of base.redis()
 * 
 * It will keep remaind also base.redis.configurate(config);
 * So you dont need to recall base.redis.configurate(config) if you want an identical connection
 * Otherwise you will need to call base.redis.configurate(config) to change "global" connection configuration
 * or base.setDefaultOptions() if you want to affect all future connection not just the "global" one alse 
 * any future base.redis.newInstance(config, "tag")
 */

 /**
  * !!! Keep in mind !!!
  * If you want to change "global" you dont need to delete "global" connection use base.redis.deleteInstance();
  * Then change config using base.redis.configurate(config) and then start using new connection
  *
  * Any time you call base.redis.configurate(config) will delete old connection and when start using it will create a new one
  * using the new configuration.
  *
  * Changing "global" connection is enough calling base.redis.configurate(config)
  * Also if you want to change a specific conection is enought to overwrite it base.redis.newInstance(config, "old_tag")
  * using same tag. base.redis.newInstance(config, "old_tag"), will ensure will close old connection after this will create a new one
  */