const mailjet_public = "c9cbd50d7f4afe487e56949f95cb28a0";
const mailjet_private = "f8b77ee8e7b1181d94f07905d90e18c6";
const mailjet_from_email = "info@loquedeverdadimporta.org";
const mailjet_from_name = "Fundación Lo Que De Verdad Importa";

/**
 * Enviar un email
 * @param {number} data.to - Email destino
 * @param {number} data.name - Nombre del contacto destino
 * @param {number} data.subject - Asunto
 * @param {number} data.text - Cuerpo del mensaje en texto plano
 * @param {number} data.html - Cuerpo del mensaje en HTML
 * @return {Promise} 
 * 
 */

function send(header, body) {

    const params = Object.assign(
        {
        "From": {
            "Email": mailjet_from_email,
            "Name": mailjet_from_name
            },
        "To": [{
            "Email": header.to,
            "Name": header.name
            }],
        "Subject": header.subject,
        },
        body
        );

    const mailjet = require('node-mailjet')
        .connect(mailjet_public, mailjet_private)
    const request = mailjet
        .post("send", { 'version': 'v3.1' })
        .request({
            "Messages": [params]
        })
    request
        .then((result) => {
            console.log('EEEEEEEEEEEEEEEEEEEENNNNNNNNNNNNNNNNNNVIO correo>>>>');
            console.log(result.body);
            return result;
        })
        .catch((error) => {
            console.log(error.statusCode);
            console.log(error);            
            return error;
        })
};

/**
 * Enviar un email con entrada
 * @header
 *   @param {number} data.to - Email destino
 *   @param {number} data.name - Nombre del contacto destino
 *   @param {number} data.subject - Asunto
 * @values
 *   @param {number} data.tipoEntrada  
 *   @param {number} data.qrCode 
 * @return {Promise}
 *
 */
function sendTicket(header, values) {

    const body = {
        "TemplateID": 980158,
        "TemplateLanguage": true,
        "TemplateErrorDeliver": true,
        "TemplateErrorReporting": {
            "Email": "info@rodax-software.com",
            "Name": "Air traffic control"
        },
        "Variables": {
            "tipoEntrada": values.tipoEntrada,
            "descriptionEntrada": values.descriptionEntrada,
            "qrCode": values.qrCode,
            "codeTicket": values.codeTicket,
            "eventName": values.eventName,
            "dateEvent": values.dateEvent,            
            "dateInscription": values.dateInscription,
            "color": (values.color)? values.color : 'gray',
            "nameInscription": header.name,
        }
    };

    return send(header, body);
};

function sendListaEspera(header, values) {

    const body = {
        "TemplateID": 980163,
        "TemplateLanguage": true,
        "TemplateErrorDeliver": true,
        "TemplateErrorReporting": {
            "Email": "info@rodax-software.com",
            "Name": "Air traffic control"
        },
        "Variables": {
            "tipoEntrada": values.tipoEntrada,
            "eventName": values.eventName,
            "dateEvent": values.dateEvent,                        
            "dateInscription": values.dateInscription,
            "color": (values.color) ? values.color : 'gray',
            "nameInscription": header.name,            
        }
    };

    return send(header, body);
};

function sendCancelacion(header, values) {

    const body = {
        "TemplateID": 978886,
        "TemplateLanguage": true,
        "TemplateErrorDeliver": true,
        "TemplateErrorReporting": {
            "Email": "info@rodax-software.com",
            "Name": "Air traffic control"
        },
        "Variables": {
            "tipoEntrada": values.tipoEntrada,
            "eventName": values.eventName,
            "dateEvent": values.dateEvent,                        
            "dateInscription": values.dateInscription,
            "color": (values.color) ? values.color : 'gray',
            "nameInscription": header.name,
        }
    };

    return send(header, body);
};



/**
 * Enviar un email
 * @header
 *   @param {number} data.to - Email destino
 *   @param {number} data.name - Nombre del contacto destino
 *   @param {number} data.subject - Asunto
 * @subjectAnBody
 *   @param {number} data.text - Cuerpo del mensaje en texto plano
 *   @param {number} data.html - Cuerpo del mensaje en HTML
 * @return {Promise}
 *
 */
function sendMail(header, values) {

    const body = {
        "TextPart": values.text,
        "HTMLPart": values.html,
    };

    return send(header, body);
};


module.exports = {
    sendMail,
    sendTicket,
    sendListaEspera,
    sendCancelacion,

};