Source: models/alarm.js

const EventEmitter = require('events');

class Alarm extends EventEmitter {

    /**
     * constructor
     */
    constructor() {
        super();
    }

    /**
     * 
     * @param {integer} basedataId basedata id of basedata entry the alarming shall be persisted for
     * @param {string} namespace namespace of plugin, the alarming shall be registeres for
     * @param {function} callback success (true or false)
     */
    setAlarmForBasedata(basedataId, namespace, callback) {
        this.doesAlarmingForBasedataExist(basedataId, namespace, (exists) => {
            if (exists) {
                console.log('Alarming exists in database and is not stored again.');
                return callback(false);
            } else {
                let query = "INSERT INTO xt_basedata_alarmings (basedataId, pluginNamespace) VALUES (?, ?)";

                global.coreDb.run(query, [
                    basedataId,
                    namespace
                ], function (err) {
                    if (err) {
                        console.log(`ERROR Adding new alarming to core database: ${err.message}`);
                        callback(false);
                    } else {
                        callback(true);
                    }
                });
            }
        })
    }

    /**
     * returns list of all alarmings as rows
     * @param {function} callback rows or null
     */
    getAllAlarmings(callback) {
        let query = "SELECT * FROM xt_basedata_alarmings";
        global.coreDb.all(query, [], (err, rows) => {
            if (err) {
                return null;
            } else {
                return callback(rows);
            }
        });
    }

    /**
     * Returns an array of basedata UUIDs for the selected plugin namespace
     * @param {string} namespace plugin namespace for which UUIDs of basedata shall be returned
     * @param {function} callback one dimensional array of basedata uuids
     */
    getAllAlarmingUUIDsForNamespace(namespace, callback) {
        let query = "SELECT " +
                        "basedata.uuid " +
                    "FROM basedata " +
                    "INNER JOIN xt_basedata_alarmings ON xt_basedata_alarmings.basedataId = basedata.id " +
                    "WHERE xt_basedata_alarmings.pluginNamespace = ?";
        global.coreDb.all(query, [
            namespace
        ], (err, rows) => {
            if (err) {
                return [];
            } else {
                var uuidArr = [];
                if (rows.length > 0) {
                    var i;
                    for (i = 0; i < rows.length; i++) {
                        uuidArr.push(rows[i].uuid);
                    }
                }
                return callback(uuidArr);
            }
        });
    }

    /**
     * Checks if there exists an entry in database for the combination of basedataId and namespace
     * @param {integer} basedataId id of basedata entry
     * @param {string} namespace namespace of plugin
     * @param {function} callback existing (true or false)
     */
    doesAlarmingForBasedataExist(basedataId, namespace, callback) {
        let query = "SELECT * FROM xt_basedata_alarmings WHERE basedataId = ? AND pluginNamespace = ?";
        global.coreDb.all(query, [
            basedataId,
            namespace
        ], (err, rows) => {
            if (err) {
                return null;
            } else {
                if (rows.length > 0) {
                    return callback(true);
                } else {
                    return callback(false);
                }
            }
        });
    }

    /**
     * Removes an alarming from table
     * @param {integer} basedataId basedata id 
     * @param {string} namespace namespace of plugin
     * @param {function} callback success (true or false)
     */
    removeAlarmingForBasedata(basedataId, namespace, callback) {
        let query = "DELETE FROM xt_basedata_alarmings WHERE basedataId = ? AND pluginNamespace = ?";
        global.coreDb.run(query, [
            basedataId,
            namespace
        ], function (err) {
            if (err) {
                console.log(`ERROR Removing alarming from core database: ${err.message}`);
                callback(false);
            } else {
                callback(true);
            }
        });

    }

    /**
     * Send out a test alarm for only one user via one plugin
     * @param {integer} basedataId basedataId of user for whom this test alarm shall be sent out
     * @param {*} namespace namespace of plugin the alarm shall be processed by
     * @param {*} alarmInfo operations-object containing the alarm information
     * @param {*} callback success(true/false)
     */
    sendTestAlarmForBasedataAndNamespace(basedataId, namespace, alarmInfo, callback) {
        global.plgManager.event_new_test_alarm(basedataId, namespace, alarmInfo);
        return callback(true);
    }


    
}

module.exports = Alarm;