Source: models/feedbackstates.js

const EventEmitter = require('events');

/**
 * Class represents feedback states in ALARMiator Core
 * @class
 * @author Jens Dinstühler
 */
class Feedbackstates extends EventEmitter {

    /**
     * @constructor
     * @param {object} logger logger instance to be used by this class instance.
     */
    constructor(logger) {
        super();
        this.logger = logger;
    }

    /**
     * Returns all states from core database
     * @param {function} callback rows or null
     */
    getAllStates(callback) {
        let query = "SELECT * FROM feedbackStates";

        global.coreDb.all(query, [], (err, rows) => {
            if (err) {
                console.log(err);
                this.logger.error('MODELS-FEEDBACKSTATES | Error reading all states from core database: ' + err.message);
                return callback([]);
            } else {
                return callback(rows);
            }
        });
    }

    
    /**
     * Creates a new feedbackstate in coredatabase table
     * @param {string} label of new state
     * @param {function} callback success (true or false)
     */
    addState(stateLabel, callback) {
        let query = "INSERT INTO feedbackStates (stateLabel, state) VALUES (?, ?)";

        global.coreDb.run(query, [
            stateLabel,
            1
        ], function (err) {
            if (err) {
                console.log('MODELS-FEEDBACKSTATES | Error storing new state to database: ' + err.message);
                callback(false);
            } else {
                callback(true);
            }
        });
    }
    

    /**
     * 
     * @param {integer} deviceId id of device in core database table alertdevices
     * @param {function} callback success (true or false)
     */
    removeState(stateId, callback) {
        let query = "DELETE FROM feedbackStates WHERE id = ?";

        global.coreDb.run(query, [
            stateId
        ], function (err) {
            if (err) {
                callback(false);
            } else {
                callback(true);
            }
        });
    }

    
    /**
     * Update information of an existing state
     * @param {integer} stateId id of state to be updated
     * @param {string} stateLabel label of state
     * @param {integer} state state (1 == active, 0 == inactive)
     * @param {function} callback success (true/false)
     */
    updateState(stateId, stateLabel, state, callback) {
        let query = "UPDATE feedbackState SET " +
            "stateLabel = ?, " +
            "state = ? " +
            "WHERE id = ?";

            global.coreDb.run(query, [
                stateLabel,
                state, 
                stateId
            ], function (err) {
                if (err) {
                    callback(false);
                } else {
                    callback(true);
                }
            });
    }

    /**
     * Sets the state of a feedbackState to active
     * @param {integer} state id of state in coredatabase to set state for
     * @param {function} callback success (true or false)
     */
    setActive(stateId, callback) {
        let query = "UPDATE feedbackStates SET state = ? WHERE id = ?";
        global.coreDb.run(query, [
            1,
            stateId
        ], function (err) {
            if (err) {
                callback(false);
            } else {
                callback(true);
            }
        });
    }

    /**
     * Sets the state of a feedbackState to inactive
     * @param {integer} state id of state in coredatabase to set state for
     * @param {function} callback success (true or false)
     */
    setInactive(stateId, callback) {
        let query = "UPDATE feedbackStates SET state = ? WHERE id = ?";
        global.coreDb.run(query, [
            0,
            stateId
        ], function (err) {
            if (err) {
                callback(false);
            } else {
                callback(true);
            }
        });
    }

    /**
     * Checks, if a state with label exists in database
     * @param {string} stateLabel 
     * @param {function} callback null or row of existing state
     */
    doesStateExist(stateLabel, callback) {
        let query = "SELECT * FROM feedbackStates WHERE stateLabel = ?";

        global.coreDb.all(query, [
            stateLabel
        ], (err, rows) => {
            if (err) {
                return null;
            } else {
                return callback(rows);
            }
        });
    }

}

module.exports = Feedbackstates;