/*****************************************************************************\
|                                               ( )_  _                       |
|    _ _    _ __   _ _    __    ___ ___     _ _ | ,_)(_)  ___   ___     _     |
|   ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| |  | |/',__)/' v `\ /'_`\   |
|   | (_) )| |  ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) )  |
|   | ,__/'(_)  `\__,_)`\__  |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/'  |
|   | |                ( )_) |                                                |
|   (_)                 \___/'                                                |
|                                                                             |
| General Bots Copyright (c) Pragmatismo.io. All rights reserved.             |
| Licensed under the AGPL-3.0.                                                |
|                                                                             |
| According to our dual licensing model, this program can be used either      |
| under the terms of the GNU Affero General Public License, version 3,        |
| or under a proprietary license.                                             |
|                                                                             |
| The texts of the GNU Affero General Public License with an additional       |
| permission and of our proprietary license can be found at and               |
| in the LICENSE file you have received along with this program.              |
|                                                                             |
| This program is distributed in the hope that it will be useful,             |
| but WITHOUT ANY WARRANTY, without even the implied warranty of              |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                |
| GNU Affero General Public License for more details.                         |
|                                                                             |
| "General Bots" is a registered trademark of Pragmatismo.io.                 |
| The licensing of the program under the AGPLv3 does not imply a              |
| trademark license. Therefore any rights, title and interest in              |
| our trademarks remain entirely with us.                                     |
|                                                                             |
\*****************************************************************************/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const botlib_1 = require("botlib");
const GBConfigService_1 = require("../../core.gbapp/services/GBConfigService");
const ChartServices_1 = require("./ChartServices");
const urlJoin = require('url-join');
const app_1 = require("../../../src/app");
const SecService_1 = require("../../security.gbapp/services/SecService");
const SystemKeywords_1 = require("./SystemKeywords");
const GBMinService_1 = require("../../core.gbapp/services/GBMinService");
const HubSpotServices_1 = require("../../hubspot.gblib/services/HubSpotServices");
const WhatsappDirectLine_1 = require("../../whatsapp.gblib/services/WhatsappDirectLine");
const GBAdminService_1 = require("../../admin.gbapp/services/GBAdminService");
const DateDiff = require('date-diff');
const puppeteer = require('puppeteer');
const Path = require('path');
const sgMail = require('@sendgrid/mail');
const PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
var mammoth = require("mammoth");
const qrcode = require('qrcode');
/**
 * Base services of conversation to be called by BASIC which
 * requries step variable to work.
 */
class DialogKeywords {
    /**
     * When creating this keyword facade, a bot instance is
     * specified among the deployer service.
     */
    constructor(min, deployer, step, user) {
        this.min = min;
        this.user = user;
        this.internalSys = new SystemKeywords_1.SystemKeywords(min, deployer, this);
        this.step = step;
    }
    async getDeployer() {
        return this.min.deployService;
    }
    async getMin() {
        return this.min;
    }
    async getStep() {
        return this.step;
    }
    /**
     * Base reference of system keyword facade, called directly
     * by the script.
     */
    sys() {
        return this.internalSys;
    }
    /**
     * Returns the page object.
     *
     * @example x = GET PAGE
     */
    async getPage(step, url) {
        if (!this.browser) {
            this.browser = await puppeteer.launch({
                args: [
                    '--ignore-certificate-errors',
                    '--no-sandbox',
                    '--disable-setuid-sandbox',
                    '--window-size=1920,1080',
                    "--disable-accelerated-2d-canvas",
                    "--disable-gpu"
                ],
                ignoreHTTPSErrors: true,
                headless: false,
            });
        }
        const page = await this.browser.newPage();
        await page.goto(url);
        return page;
    }
    /**
     *
     *
     * Data = [10, 20, 30]
     * Legends = "Steve;Yui;Carlos"
     * img = CHART "pie", data, legends
     *
     * https://c3js.org/examples.html
     *
     * @param data
     * @param legends
     * @see https://www.npmjs.com/package/plot
     */
    async chart(step, type, data, legends, transpose) {
        let table = [[]];
        if (legends) {
            const legends_ = legends.split(';');
            // Columns and data are merged like:
            //     columns: [
            //       ['data1', 30, 200, 100, 400, 150, 250],
            //       ['data2', 50, 20, 10, 40, 15, 25]
            //     ]
            for (let i = 0; i < legends_.length; i++) {
                table[i] = [legends_[i]];
                table[i] = table[i].concat(data);
            }
        }
        else {
            table = SystemKeywords_1.SystemKeywords.JSONAsGBTable(data, false);
            table.shift();
        }
        if (transpose) {
            const transpose = (array) => {
                return array.reduce((prev, next) => next.map((item, i) => (prev[i] || []).concat(next[i])), []);
            };
            table = transpose(table);
        }
        let definition = {
            size: {
                "height": 420,
                "width": 680
            },
            data: {
                columns: table,
                type: type
            },
            bar: {
                ratio: 0.5
            }
        };
        // TODO: https://c3js.org/samples/timeseries.html
        if (type === 'timeseries') {
            definition['axis'][table[0]] = {
                type: 'timeseries',
                tick: {
                    format: '%Y-%m-%d'
                }
            };
        }
        const gbaiName = `${this.min.botId}.gbai`;
        const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService_1.GBAdminService.getRndReadableIdentifier()}.jpg`);
        await ChartServices_1.ChartServices.screenshot(definition, localName);
        const url = urlJoin(app_1.GBServer.globals.publicAddress, this.min.botId, 'cache', Path.basename(localName));
        botlib_1.GBLog.info(`BASIC: Visualization: Chart generated at ${url}.`);
        return url;
    }
    /**
     * Find element on page DOM.
     *
     * @example GET page, "elementName", "text"
     */
    async getBySelector(page, elementName) {
        await page.waitForSelector(elementName);
        let element = await page.$(elementName);
        return element;
    }
    /**
     * Find element on page DOM.
     *
     * @example GET page, "frameSelector, "elementSelector"
     */
    async getByFrame(page, frame, selector) {
        await page.waitForSelector(frame);
        let frameHandle = await page.$(frame);
        const f = await frameHandle.contentFrame();
        await f.waitForSelector(selector);
        const element = await f.$(selector);
        element['originalSelector'] = selector;
        element['frame'] = f;
        return element;
    }
    /**
     * Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
     *
     * @example x = TODAY
     */
    async click(step, page, idOrName) {
        const e = await this.getBySelector(page, idOrName);
        await Promise.all([
            page.waitForNavigation(),
            page.click(e.name)
        ]);
    }
    /**
     * Returns the screenshot of page or element
     *
     * @example file = SCREENSHOT page
     */
    async screenshot(step, page, idOrName, localName) {
        const e = await this.getBySelector(page, idOrName);
        await e.screenshot({ path: localName });
    }
    /**
     * Types the text into the text field.
     *
     * @example TYPE page, "elementName", "text"
     */
    async type(step, page, idOrName, text) {
        const e = await this.getBySelector(page, idOrName);
        await e.type(text);
    }
    /**
     * Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
     *
     * @example x = TODAY
     */
    async getOCR(step, localFile) {
        const tesseract = require("node-tesseract-ocr");
        const config = {
            lang: "eng",
            oem: 1,
            psm: 3,
        };
        return await tesseract.recognize(localFile, config);
    }
    /**
     * Returns the today data filled in dd/mm/yyyy or mm/dd/yyyy.
     *
     * @example x = TODAY
     */
    async getToday(step) {
        let d = new Date(), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d.getFullYear();
        if (month.length < 2) {
            month = '0' + month;
        }
        if (day.length < 2) {
            day = '0' + day;
        }
        const contentLocale = this.min.core.getParam(this.min.instance, 'Default Content Language', GBConfigService_1.GBConfigService.get('DEFAULT_CONTENT_LANGUAGE'));
        switch (contentLocale) {
            case 'pt':
                return [day, month, year].join('/');
            case 'en':
                return [month, day, year].join('/');
            default:
                return [year, month, day].join('/');
        }
    }
    /**
     * Quits the dialog, currently required to get out of VM context.
     *
     * @example EXIT
     */
    async exit(step) {
        if (this.browser) {
            await this.browser.close();
        }
        await step.endDialog();
    }
    /**
     * Get active tasks.
     *
     * @example list = ACTIVE TASKS
     */
    async getActiveTasks() {
        let s = new HubSpotServices_1.HubSpotServices(null, null, process.env.HUBSPOT_KEY);
        return await s.getActiveTasks();
    }
    /**
     * Creates a new deal.
     *
     * @example CREATE DEAL dealname, contato, empresa, amount
     */
    async createDeal(dealName, contact, company, amount) {
        let s = new HubSpotServices_1.HubSpotServices(null, null, process.env.HUBSPOT_KEY);
        let deal = await s.createDeal(dealName, contact, company, amount);
        return deal;
    }
    /**
     * Finds contacts in XRM.
     *
     * @example list = FIND CONTACT "Sandra"
     */
    async fndContact(name) {
        let s = new HubSpotServices_1.HubSpotServices(null, null, process.env.HUBSPOT_KEY);
        return await s.searchContact(name);
    }
    getContentLocaleWithCulture(contentLocale) {
        switch (contentLocale) {
            case 'pt':
                return 'pt-BR';
            case 'en':
                return 'en-US';
            default:
                return 'en-us';
        }
    }
    getCoded(value) {
        // Checks if it is a GB FILE object.
        if (value.data && value.filename) {
            value = value.data;
        }
        return Buffer.from(value).toString("base64");
    }
    /**
     * Returns specified date week day in format 'Mon'.
     *
     * @example day = WEEKDAY (date)
     *
     */
    getWeekFromDate(date) {
        const contentLocale = this.min.core.getParam(this.min.instance, 'Default Content Language', GBConfigService_1.GBConfigService.get('DEFAULT_CONTENT_LANGUAGE'));
        let dt = SystemKeywords_1.SystemKeywords.getDateFromLocaleString(date, contentLocale);
        botlib_1.GBLog.info(`BASIC WEEKDAY contentLocale: ${this.getContentLocaleWithCulture(contentLocale)}`);
        botlib_1.GBLog.info(`BASIC WEEKDAY date: ${dt}`);
        botlib_1.GBLog.info(dt.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' }));
        if (dt) {
            if (!(dt instanceof Date)) {
                dt = new Date(dt);
            }
            let week = dt.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { weekday: 'short' });
            return week.substr(0, 3);
        }
        return 'NULL';
    }
    /**
     * Returns an object ready to get information about difference in several ways
     * like years, months or days.
     *
     * @example days = DATEDIFF date1, date2, mode
     *
     */
    dateDiff(date1, date2, mode) {
        let dt1 = date1;
        let dt2 = date2;
        if (!(dt1 instanceof Date)) {
            dt1 = new Date(dt1);
        }
        if (!(dt2 instanceof Date)) {
            dt2 = new Date(dt2);
        }
        const diff = new DateDiff(date1, date2);
        switch (mode) {
            case 'year': return diff.years();
            case 'month': return diff.months();
            case 'week': return diff.weeks();
            case 'day': return diff.days();
            case 'hour': return diff.hours();
            case 'minute': return diff.minutes();
        }
    }
    /**
     * Returns specified date week day in format 'Mon'.
     *
     * @example DATEADD date, "minute", 60
     *
     * https://stackoverflow.com/a/1214753/18511
     */
    dateAdd(date, mode, units) {
        let dateCopy = date;
        if (!(dateCopy instanceof Date)) {
            dateCopy = new Date(dateCopy);
        }
        var ret = new Date(dateCopy); //don't change original date
        var checkRollover = function () { if (ret.getDate() != date.getDate())
            ret.setDate(0); };
        switch (String(mode).toLowerCase()) {
            case 'year':
                ret.setFullYear(ret.getFullYear() + units);
                checkRollover();
                break;
            case 'quarter':
                ret.setMonth(ret.getMonth() + 3 * units);
                checkRollover();
                break;
            case 'month':
                ret.setMonth(ret.getMonth() + units);
                checkRollover();
                break;
            case 'week':
                ret.setDate(ret.getDate() + 7 * units);
                break;
            case 'day':
                ret.setDate(ret.getDate() + units);
                break;
            case 'hour':
                ret.setTime(ret.getTime() + units * 3600000);
                break;
            case 'minute':
                ret.setTime(ret.getTime() + units * 60000);
                break;
            case 'second':
                ret.setTime(ret.getTime() + units * 1000);
                break;
            default:
                ret = undefined;
                break;
        }
        return ret;
    }
    /**
     * Returns specified list member separated by comma.
     *
     * @example TALK TOLIST (array, member)
     *
     */
    getToLst(array, member) {
        if (!array) {
            return "<Empty>";
        }
        if (array[0] && array[0]['gbarray']) {
            array = array.slice(1);
        }
        array = array.filter((v, i, a) => a.findIndex(t => (t[member] === v[member])) === i);
        array = array.filter(function (item, pos) { return item != undefined; });
        array = array.map((item) => { return item[member]; });
        array = array.join(", ");
        return array;
    }
    /**
     * Returns the specified time in format hh:dd.
     *
     * @example hour = HOUR (date)
     *
     */
    getHourFromDate(date) {
        function addZero(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
        const contentLocale = this.min.core.getParam(this.min.instance, 'Default Content Language', GBConfigService_1.GBConfigService.get('DEFAULT_CONTENT_LANGUAGE'));
        let dt = SystemKeywords_1.SystemKeywords.getDateFromLocaleString(date, contentLocale);
        if (dt) {
            if (!(dt instanceof Date)) {
                dt = new Date(dt);
            }
            return addZero(dt.getHours()) + ':' + addZero(dt.getMinutes());
        }
        return 'NULL';
    }
    /**
     * Returns current time in format hh:dd.
     *
     * @example SAVE "file.xlsx", name, email, NOW
     *
     */
    async getNow() {
        const contentLocale = this.min.core.getParam(this.min.instance, 'Default Content Language', GBConfigService_1.GBConfigService.get('DEFAULT_CONTENT_LANGUAGE'));
        const nowUTC = new Date();
        const now = typeof nowUTC === 'string' ?
            new Date(nowUTC) :
            nowUTC;
        const nowText = now.toLocaleString(this.getContentLocaleWithCulture(contentLocale), { timeZone: process.env.DEFAULT_TIMEZONE });
        return /\b([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]/.exec(nowText)[0];
    }
    /**
     * Sends an e-mail.
     *
     * @example
     *
     * SEND MAIL "email@domain.com", "Subject",  "Message text."
     *
     */
    async sendEmail(to, subject, body) {
        // tslint:disable-next-line:no-console
        botlib_1.GBLog.info(`[E-mail]: to:${to}, subject: ${subject}, body: ${body}.`);
        const emailToken = process.env.EMAIL_API_KEY;
        // Inline word document used as e-mail body.
        if (typeof (body) === "object") {
            const result = await mammoth.convertToHtml({ buffer: body });
            body = result.value;
        }
        return new Promise((resolve, reject) => {
            sgMail.setApiKey(emailToken);
            const msg = {
                to: to,
                from: process.env.EMAIL_FROM,
                subject: subject,
                text: body,
                html: body
            };
            sgMail.send(msg, false, (err, res) => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(res);
                }
            });
        });
    }
    /**
     * Sends a file to a given mobile.
     *
     * @example SEND FILE TO "+199988887777", "image.jpg", caption
     *
     */
    async sendFileTo(step, mobile, filename, caption) {
        botlib_1.GBLog.info(`BASIC: SEND FILE TO '${mobile}', filename '${filename}'.`);
        return await this.internalSendFile(null, mobile, filename, caption);
    }
    /**
     * Sends a file to the current user.
     *
     * @example SEND FILE "image.jpg"
     *
     */
    async sendFile(step, filename, caption) {
        const mobile = await this.userMobile(step);
        botlib_1.GBLog.info(`BASIC: SEND FILE (current: ${mobile}, filename '${filename}'.`);
        return await this.internalSendFile(step, mobile, filename, caption);
    }
    /**
     * Defines the current language of the bot conversation.
     *
     * @example SET LANGUAGE "pt"
     *
     */
    async setLanguage(step, language) {
        const user = await this.min.userProfile.get(step.context, {});
        const sec = new SecService_1.SecService();
        user.systemUser = await sec.updateUserLocale(user.systemUser.userId, language);
        await this.min.userProfile.set(step.context, user);
        this.user = user;
    }
    /**
     * Defines the id generation policy.
     *
     * @example SET ID NUMBER
     *
     */
    async setIdGeneration(mode) {
        this['idGeneration'] = mode;
        this['id'] = await this.sys().getRandomId();
    }
    /**
     * Defines the maximum lines to scan in spreedsheets.
     *
     * @example SET MAX LINES 5000
     *
     */
    async setMaxLines(step, count) {
        const user = await this.min.userProfile.get(step.context, {});
        user.basicOptions.maxLines = count;
        await this.min.userProfile.set(step.context, user);
        this.user = user;
    }
    /**
     * Defines the FIND behaviour to consider whole words while searching.
     *
     * @example SET WHOLE WORD ON
     *
     */
    async setWholeWord(step, on) {
        const user = await this.min.userProfile.get(step.context, {});
        user.basicOptions.wholeWord = (on.trim() === "on");
        await this.min.userProfile.set(step.context, user);
        this.user = user;
    }
    /**
   * Defines the theme for assets generation.
   *
   * @example SET THEME "themename"
   *
   */
    async setTheme(step, theme) {
        const user = await this.min.userProfile.get(step.context, {});
        user.basicOptions.theme = theme.trim();
        await this.min.userProfile.set(step.context, user);
        this.user = user;
    }
    /**
     * Defines translator behaviour.
     *
     * @example SET TRANSLATOR ON | OFF
     *
     */
    async setTranslatorOn(step, on) {
        const user = await this.min.userProfile.get(step.context, {});
        user.basicOptions.translatorOn = (on.trim() === "on");
        await this.min.userProfile.set(step.context, user);
        this.user = user;
    }
    /**
     * Returns the name of the user acquired by WhatsApp API.
     */
    async userName(step) {
        return step ? WhatsappDirectLine_1.WhatsappDirectLine.usernames[await this.userMobile(step)] : 'N/A';
    }
    /**
     * OBSOLETE.
     */
    async getFrom(step) {
        return step ? await this.userMobile(step) : 'N/A';
    }
    /**
     * Returns current mobile number from user in conversation.
     *
     * @example SAVE "file.xlsx", name, email, MOBILE
     *
     */
    async userMobile(step) {
        return GBMinService_1.GBMinService.userMobile(step);
    }
    /**
     * Shows the subject menu to the user
     *
     * @example MENU
     *
     */
    async showMenu(step) {
        return await step.beginDialog('/menu');
    }
    /**
     * Performs the transfer of the conversation to a human agent.
     *
     * @example TRANSFER
     *
     */
    async transferTo(step, to = null) {
        return await step.beginDialog('/t', { to: to });
    }
    /**
     * Hears something from user and put it in a variable
     *
     * @example HEAR name
     *
     */
    async hear(step, promise, previousResolve, kind, ...args) {
        function random(low, high) {
            return Math.random() * (high - low) + low;
        }
        const idPromise = random(0, 120000000);
        this.min.cbMap[idPromise] = {};
        this.min.cbMap[idPromise].promise = promise;
        let opts = { id: idPromise, previousResolve: previousResolve, kind: kind, args };
        if (this.hrOn) {
            // Waits for next message in HEAR delegated context.
            const botId = this.min.botId;
            WhatsappDirectLine_1.WhatsappDirectLine.state[botId + this.hrOn] = {
                promise: promise, previousResolve: previousResolve
            };
        }
        else {
            if (previousResolve !== undefined) {
                previousResolve(opts);
            }
            else {
                await step.beginDialog('/hear', opts);
            }
        }
    }
    /**
     * Prepares the next dialog to be shown to the specified user.
     */
    async gotoDialog(step, fromOrDialogName, dialogName) {
        if (dialogName) {
            if (dialogName.charAt(0) === '/') {
                await step.beginDialog(fromOrDialogName);
            }
            else {
                let sec = new SecService_1.SecService();
                let user = await sec.getUserFromSystemId(fromOrDialogName);
                if (!user) {
                    user = await sec.ensureUser(this.min.instance.instanceId, fromOrDialogName, fromOrDialogName, null, 'whatsapp', 'from', null);
                }
                await sec.updateUserHearOnDialog(user.userId, dialogName);
            }
        }
        else {
            await step.beginDialog(fromOrDialogName);
        }
    }
    /**
     * Talks to the user by using the specified text.
     */
    async talk(step, text) {
        const translate = this.user ? this.user.basicOptions.translatorOn : false;
        await this.min.conversationalService['sendTextWithOptions'](this.min, step, text, translate, null);
    }
    static getChannel(step) {
        if (!step)
            return 'whatsapp';
        if (!isNaN(step.context.activity['mobile'])) {
            return 'webchat';
        }
        else {
            if (step.context.activity.from && !isNaN(step.context.activity.from.id)) {
                return 'whatsapp';
            }
            return 'webchat';
        }
    }
    /**
     * Processes the sending of the file.
     */
    async internalSendFile(step, mobile, filename, caption) {
        // Handles SEND FILE TO mobile, element in Web Automation.
        const page = filename._page;
        if (page) {
            const gbaiName = `${this.min.botId}.gbai`;
            const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService_1.GBAdminService.getRndReadableIdentifier()}.jpg`);
            await filename.screenshot({ path: localName });
            const url = urlJoin(app_1.GBServer.globals.publicAddress, this.min.botId, 'cache', Path.basename(localName));
            botlib_1.GBLog.info(`BASIC: WebAutomation: Sending the file ${url} to mobile ${mobile}.`);
            await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption);
        }
        // Handles Markdown.
        else if (filename.indexOf('.md') > -1) {
            botlib_1.GBLog.info(`BASIC: Sending the contents of ${filename} markdown to mobile ${mobile}.`);
            const md = await this.min.kbService.getAnswerTextByMediaName(this.min.instance.instanceId, filename);
            if (!md) {
                botlib_1.GBLog.info(`BASIC: Markdown file ${filename} not found on database for ${this.min.instance.botId}.`);
            }
            await this.min.conversationalService['playMarkdown'](this.min, md, DialogKeywords.getChannel(step), step, mobile);
        }
        else {
            botlib_1.GBLog.info(`BASIC: Sending the file ${filename} to mobile ${mobile}.`);
            let url;
            if (!filename.startsWith("https://")) {
                url = urlJoin(app_1.GBServer.globals.publicAddress, 'kb', `${this.min.botId}.gbai`, `${this.min.botId}.gbkb`, 'assets', filename);
            }
            else {
                url = filename;
            }
            await this.min.conversationalService.sendFile(this.min, step, mobile, url, caption);
        }
    }
    async getQRCode(text) {
        const img = await qrcode.toDataURL(text);
        const data = img.replace(/^data:image\/\w+;base64,/, "");
        const buf = Buffer.from(data, "base64");
        return buf;
    }
}
exports.DialogKeywords = DialogKeywords;
//# sourceMappingURL=DialogKeywords.js.map