Source: plugins/outbound/alarmdepesche/modules/alarmfax.js

const Pdf = require('../../../../models/core/pdf');
const EventEmitter = require('events');

class Alarmfax extends EventEmitter {

    constructor() {
        super();
        this.pngPath = global.appRoot + '/temp/fax-map.png';  // DEFAULT Value for map png file
        this.headerLogo = global.appRoot + '/public/assets/img/logoPdf.jpg';    // Default Logo for pdf document
        this.markerImage = global.appRoot + '/plugins/outbound/alarmdepesche/marker.png';   // Default Marker to be used for map
        this.fullPdfOutputPath = global.appRoot + '/temp/alarmfax.pdf';
    }

    /**
     * Set / get operation object the fax shall be rendered for
     */
    set operation(operation) {
        this._operation = operation;
    }
    get operation() {
        return this._operation;
    }

    /**
     * Set / get if map shall be rendered to fax ot not (boolean)
     */
    set renderWithMap(renderWithMap) {
        this._renderWithMap = renderWithMap;
    }
    get renderWithMap() {
        return this._renderWithMap;
    }

    /**
     * Set / get the path to the pdf output file
     */
    set fullPdfOutputPath(fullPdfOutputPath) {
        this._fullPdfOutputPath = fullPdfOutputPath;
    }
    get fullPdfOutputPath() {
        return this._fullPdfOutputPath;
    }

    /**
     * Set / get the path to the png file of map
     */
    set fullMapPngPath(fullMapPngPath) {
        this._fullMapPngPath = fullMapPngPath;
    }
    get fullMapPngPath() {
        return this._fullMapPngPath;
    }

    /**
     * Renders a static image with a marker at lat / lon
     * @param {float} lat 
     * @param {float} lon 
     * @param {function} callback (success (true/false), pathToPng (string))
     */
    getMap(callback) {
        var StaticMaps = require('staticmaps');
        const options = {
            width: 600,
            height: 400,
        };
        const map = new StaticMaps(options);

        // calculate path for PNG File of Map
        if (this.fullMapPngPath != null) {
            this.pngPath = this.fullMapPngPath;
        }

        const marker = {
            img: this.markerImage, // can also be a URL,
            offsetX: 24,
            offsetY: 48,
            width: 48,
            height: 48,
            coord: [this.operation.lon.value, this.operation.lat.value],
        };
        map.addMarker(marker);
        map.render()
            .then(() => map.image.save(this.pngPath))
            .then(() => {
                callback(true, this.pngPath);
            })
            .catch((err) => {
                console.log('PLUGINS | ALARMDEPESCHE | Error generating map: ' + err.message);
                callback(false, null);
            });
    }

    /**
     * converts a png image to a jpeg image
     * @param {string} filename path to png file to convert to jpg 
     * @param {function} callback (success true/false), filename of jpeg file 
     */
    mapPngToJpg(filename, callback) {
        const fs = require("fs");
        const pngToJpeg = require('png-to-jpeg');

        var outputfilename = filename + '.jpeg';
        
        let buffer = fs.readFileSync(filename);
        pngToJpeg({ quality: 90 })(buffer)
            .then(output => {
                fs.writeFileSync(outputfilename, output);
                callback(true, outputfilename);
            })
            .catch((err) => {
                console.log('PLUGINS | ALARMDEPESCHE | error converting map from png to jpg: ' + err.message);
                callback(false, null);
            });
    }

    /**
     * generates pdf document and returnes path to it
     * @param {boolean} map shall map be generated or not
     * @param {string} mapJpegPath full path to map jpge
     * @param {function} callback success (true or false), path to pdf document
     */
    generatePDF(map = false, mapJpegPath = null, callback) {
        let pdf = new Pdf(null);
        pdf.initialize();

        var operation = this.operation;

        // add Header
        pdf.headerLogo = this.headerLogo;
        pdf.headerTitle = "ALARMDEPECHE | " + operation.operationnumber.value;
        pdf.addHeader(operation.subject.value + ' | ' + operation.keywordName.value, 0, false);

        // add Organization
        pdf.addTable([250], 0);
        pdf.addTableRow(['Feuerwehr Reichenschwand'], [{ padding: 0, color: 0xff0000 }]);
        pdf.addText('----', { textAlign: 'center', color: 0xffffff });
        pdf.addText('----', { textAlign: 'center', color: 0xffffff });

        // add Keyword
        pdf.addText('EINSATZGRUND', { fontSize: 16, textAlign: 'center', color: 0xff0000 });
        pdf.addTable([540], 1, 7, '0xff0000', 0);
        pdf.addTableRow([operation.subject.value + ' - ' + operation.keywordName.value], [{ fontSize: 20, textAlign: 'center', color: 0xff0000 }]);
        pdf.addText('----', { textAlign: 'center', color: 0xffffff });

        // alarm date and time
        pdf.addText('ALARMIERUNG', { fontSize: 16, textAlign: 'left', color: 0x000000 });
        pdf.addTable([270, 270], 1, 3, '0xa4a4a7');
        pdf.addTableRow(['Zeitpunkt', operation.alarmtimeCalc.value], [{ backgroundColor: 0xe3e3e3 }, {}]);
        pdf.addTableRow(['Leitstelle', operation.ils.value], [{ backgroundColor: 0xe3e3e3 }, {}]);
        pdf.addText('----', { textAlign: 'center', color: 0xffffff });

        // Location
        pdf.addText('EINSATZORT', { fontSize: 16, textAlign: 'left', color: 0x000000 });
        pdf.addTable([270, 270], 1, 3, '0xa4a4a7');
        pdf.addTableRow(['Straße', operation.street.value], [{ backgroundColor: 0xe3e3e3 }, {}]);
        pdf.addTableRow(['Ort', operation.location.value], [{ backgroundColor: 0xe3e3e3 }, {}]);
        pdf.addTableRow(['Gemeinde', operation.community.value], [{ backgroundColor: 0xe3e3e3 }, {}]);
        pdf.addTableRow(['Objekt', operation.object.value], [{ backgroundColor: 0xe3e3e3 }, {}]);
        pdf.addTableRow(['Abschnitt', operation.section.value], [{ backgroundColor: 0xe3e3e3 }, {}]);

        pdf.addText('----', { textAlign: 'center', color: 0xffffff });

        // DISPATCH Message
        pdf.addText('MITTEILUNG', { fontSize: 16, textAlign: 'left', color: 0x000000 });
        pdf.addTable([540], 1, 7, '0x000000', 0);
        pdf.addTableRow([operation.message.value], [{ textAlign: 'left', color: 0x000000 }]);
        pdf.addText('----', { textAlign: 'center', color: 0xffffff });


        // Gear
        pdf.addText('Alarmierte Einsatzmittel', { fontSize: 16, textAlign: 'left', color: 0x000000 });
        pdf.addTable([540], 1, 2);
        operation.gearCalc.value.forEach(gear => {
            pdf.addTableRow([gear]);
        });

        // Karte
        if (this.renderWithMap === true) {
            pdf.doc.pageBreak();
            pdf.addText('KARTE EINSATZORT', { fontSize: 16, textAlign: 'left', color: 0x000000 });
            pdf.addText('----', { textAlign: 'center', color: 0xffffff });
            pdf.addImage(mapJpegPath, 10, 0, 360, 'left');
            pdf.addText('----', { textAlign: 'center', color: 0xffffff });
        }


        pdf.addFooter();
        pdf.writeDocToFile(this.fullPdfOutputPath, (successPdf) => {
            if (successPdf) {
                callback(true, this.fullPdfOutputPath);
            } else {
                callback(false, null);
            }
            
        })
    }

    

}

module.exports = Alarmfax;