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

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

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

/**
 * Also you can specify setting just for global cassandra connection (base.cassandra()), without afecting base.cassandra.newInstance(),
 * using base.cassandra.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.cassandra.configurate() BEFORE any used base.cassandra()
 * If you use after base.cassandra() it will close previous connection and create a new one with the new configuration
 * 
 * Default setting are:
 */

base.cassandra.configurate({
    "hosts": ["localhost"],
    "port": 9042,
    "keyspace": "keyspace",
    "user": "user",
    "password": "password",
    "timeout": 3000,
    "autoConnect": false
});

/**
 * Simple use case
 */
base.cassandra().execute("query", {}, {}, function(error, response){});


/**
 * You alo can create how many connection you want asociating with an unique tag
 * To do that use base.cassandra.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.cassandra.newInstance({
    "port": 6380,
    "hosts": ["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.cassandra(tag).
 * 
 * To get an connection by tag use base.cassandra(tag);
 */
base.cassandra("unique_tag");

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

 /**
 * Simple use case
 */
base.cassandra("unique_tag").execute("query", {}, {}, function(error, response){});

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


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


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

 /**
  * !!! Keep in mind !!!
  * If you want to change "global" you dont need to delete "global" connection use base.cassandra.deleteInstance();
  * Then change config using base.cassandra.configurate(config) and then start using new connection
  *
  * Any time you call base.cassandra.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.cassandra.configurate(config)
  * Also if you want to change a specific conection is enought to overwrite it base.cassandra.newInstance(config, "old_tag")
  * using same tag. base.cassandra.newInstance(config, "old_tag"), will ensure will close old connection after this will create a new one
  */