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

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

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

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

base.mailer.configurate({
    "mailTransportConfig": {
        "host": "localhost",
        "port": 25,
        "auth":{
            "user": "user_email",
            "pass": "user_pass"
        }
    },
    "mailConfig": {
        "subject": "Mail from node-base-module",
        "from": "node_base_module@domain.com",
        "to": "target@domain.com",
        "text": ""
    } 
});

/**
 * Simple use case
 */
let mailConfig = {
    "subject": "Mail from node-base-module",
    "from": "node_base_module@domain.com",
    "to": "target@domain.com",
    "text": ""
}
base.mailer().sendMail(mailConfig, function(error, response){});


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

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

 /**
 * Simple use case
 */
base.mailer("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.mailer.deleteInstance("tag");
 * This function will close and delete connection, if you try tu use it after deletion using base.mailer("tag"),
 * function will return "null"
 */
base.mailer.deleteInstance("unique_tag");


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


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

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