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

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

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

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

base.elastic.configurate({
    "host": "localhost:9200",
    "log": "trace",
    "statusCheckInterval": 5000,
    "autoConnect": true
});

/**
 * Simple use case
 */
base.elastic().index({}, function (error, response) {});


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

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

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

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

/**
 * example async function call
 */
(async() => {

    try{
        let result1 = await new Promise((resolve, reject) => {
            base.elastic().index({
                index: 'index',
                type: 'index',
                id: 1,
                body: {
                    body: 'dummy'
                }
            }, function(err:any, response:any){
                if(err) reject(err);
                resolve(response);
            });
        });
    }catch(e){
        //handle error
        console.log(e);
    }

    try{
        let result2 = await new Promise((resolve, reject) => {
            base.elastic().search({
                index: 'index',
                q: 'index'
            }, function(err: any, response: any){
                if(err) reject(err);
                resolve(response);
            });
        });
    }catch(e){
        //handle error
        console.log(e);
    }
})();

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


/**
 * !!! ATENTION !!!
 * Deleting the global connection is temporar, until you start using again
 * So if you use base.elastic() after deletion will be created a new connection based on your configuration
 * Behavior is same as your first call of base.elastic()
 * 
 * It will keep remaind also base.elastic.configurate(config);
 * So you dont need to recall base.elastic.configurate(config) if you want an identical connection
 * Otherwise you will need to call base.elastic.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.elastic.newInstance(config, "tag")
 */

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