"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideohubReceiver = void 0;
const Command_1 = require("./Command");
class VideohubReceiver {
    constructor(state) {
        this.state = state;
    }
    processData(key, data) {
        const cmd = new Command_1.VideohubCommand(key);
        cmd.parseParameters(data);
        if (cmd.header.match(/(INPUT|OUTPUT|MONITORING OUTPUT|SERIAL PORT) LABELS/)) {
            this.updateLabels(cmd);
        }
        else if (cmd.header.match(/(VIDEO OUTPUT|VIDEO MONITORING OUTPUT|SERIAL PORT) ROUTING/)) {
            this.updateRouting(cmd);
        }
        else if (cmd.header.match(/(VIDEO OUTPUT|MONITORING OUTPUT|SERIAL PORT) LOCKS/)) {
            this.updateLocks(cmd);
        }
        else if (cmd.header.match(/(VIDEO INPUT|VIDEO OUTPUT|SERIAL PORT) STATUS/)) {
            this.updateStatus(cmd);
        }
        else if (cmd.header === 'SERIAL PORT DIRECTIONS') {
            this.updateSerialDirections(cmd);
        }
        else if (cmd.header === 'VIDEOHUB DEVICE') {
            this.updateDevice(cmd);
        }
        else {
            // TODO: find out more about the video hub from stuff that comes in here
        }
    }
    /**
     * INTERNAL: Updates device data from the Videohub
     *
     * @param {string} labeltype - the command/data type being passed
     * @param {Object} object - the collected data
     * @access protected
     * @since 1.1.0
     */
    updateDevice(cmd) {
        for (const item of cmd.parameters) {
            let value;
            switch (item.keyString()) {
                case 'Model name':
                    this.state.setDeviceName('' + item.valueString());
                    break;
                case 'Friendly name':
                    this.state.setName('' + item.valueString());
                    break;
                case 'Video inputs':
                    value = item.valueNumber();
                    if (value !== undefined && this.state.config.inputCount !== value) {
                        this.state.config.inputCount = value;
                        this.state.emit('change', 'config', { type: 'inputCount', value: this.state.config.inputCount });
                    }
                    break;
                case 'Video outputs':
                    value = item.valueNumber();
                    if (value !== undefined && this.state.config.outputCount !== value) {
                        this.state.config.outputCount = value;
                        this.state.emit('change', 'config', { type: 'outputCount', value: this.state.config.outputCount });
                    }
                    break;
                case 'Video monitoring outputs':
                    value = item.valueNumber();
                    if (value !== undefined && this.state.config.monitoringCount !== value) {
                        this.state.config.monitoringCount = value;
                        this.state.emit('change', 'config', { type: 'monitoringCount', value: this.state.config.monitoringCount });
                    }
                    break;
                case 'Serial ports':
                    value = item.valueNumber();
                    if (value !== undefined && this.state.config.serialCount !== value) {
                        this.state.config.serialCount = value;
                        this.state.emit('change', 'config', { type: 'serialCount', value: this.state.config.serialCount });
                    }
                    break;
            }
        }
    }
    /**
     * INTERNAL: Updates variables based on data from the Videohub
     *
     * @param {string} labeltype - the command/data type being passed
     * @param {Object} object - the collected data
     * @access protected
     * @since 1.0.0
     */
    updateLabels(cmd) {
        for (const item of cmd.parameters) {
            let obj;
            const id = item.keyNumber();
            if (id === undefined) {
                continue;
            }
            switch (cmd.header) {
                case 'INPUT LABELS':
                    obj = this.state.getInput(id);
                    if (obj.name !== item.value) {
                        obj.name = item.valueString();
                        this.state.emit('change', 'input', { type: 'label', id, value: item.valueString() });
                    }
                    break;
                case 'MONITORING OUTPUT LABELS':
                    obj = this.state.getMonitoringOutput(id);
                    if (obj.name !== item.valueString()) {
                        obj.name = item.valueString();
                        this.state.emit('change', 'monitoringOutput', { type: 'label', id, value: item.valueString() });
                    }
                    break;
                case 'OUTPUT LABELS':
                    obj = this.state.getOutput(id);
                    if (obj.name !== item.valueString()) {
                        obj.name = item.valueString();
                        this.state.emit('change', 'output', { type: 'label', id, value: item.valueString() });
                    }
                    break;
                case 'SERIAL PORT LABELS':
                    obj = this.state.getSerial(id);
                    if (obj.name !== item.valueString()) {
                        obj.name = item.valueString();
                        this.state.emit('change', 'serial', { type: 'label', id, value: item.valueString() });
                    }
                    break;
            }
        }
    }
    /**
     * INTERNAL: Updates lock states based on data from the Videohub
     *
     * @param {string} labeltype - the command/data type being passed
     * @param {Object} object - the collected data
     * @access protected
     * @since 1.1.0
     */
    updateLocks(cmd) {
        for (const item of cmd.parameters) {
            const id = item.keyNumber();
            if (id === undefined) {
                continue;
            }
            let obj;
            switch (cmd.header) {
                case 'MONITORING OUTPUT LOCKS':
                    obj = this.state.getSerial(id);
                    if (obj.lock !== item.valueString()) {
                        obj.lock = item.valueString();
                        this.state.emit('change', 'monitoringOutput', { type: 'lock', id, value: item.valueString() });
                    }
                    break;
                case 'VIDEO OUTPUT LOCKS':
                    obj = this.state.getOutput(id);
                    if (obj.lock !== item.valueString()) {
                        obj.lock = item.valueString();
                        this.state.emit('change', 'output', { type: 'lock', id, value: item.valueString() });
                    }
                    break;
                case 'SERIAL PORT LOCKS':
                    obj = this.state.getSerial(id);
                    if (obj.lock !== item.valueString()) {
                        obj.lock = item.valueString();
                        this.state.emit('change', 'serial', { type: 'lock', id, value: item.valueString() });
                    }
                    break;
            }
        }
    }
    /**
     * INTERNAL: Updates routing table based on data from the Videohub
     *
     * @param {string} labeltype - the command/data type being passed
     * @param {Object} object - the collected data
     * @access protected
     * @since 1.0.0
     */
    updateRouting(cmd) {
        for (const item of cmd.parameters) {
            const dest = item.keyNumber();
            const src = item.valueNumber();
            if (dest === undefined || src === undefined) {
                continue;
            }
            let obj;
            switch (cmd.header) {
                case 'VIDEO MONITORING OUTPUT ROUTING':
                    obj = this.state.getMonitoringOutput(dest);
                    if (obj.route !== src) {
                        obj.route = src;
                        this.state.emit('change', 'monitoringOutput', { type: 'route', id: dest, value: src });
                    }
                    break;
                case 'VIDEO OUTPUT ROUTING':
                    obj = this.state.getOutput(dest);
                    if (obj.route !== src) {
                        obj.route = src;
                        this.state.emit('change', 'output', { type: 'route', id: dest, value: src });
                    }
                    break;
                case 'SERIAL PORT ROUTING':
                    obj = this.state.getSerial(dest);
                    if (obj.route !== src) {
                        obj.route = src;
                        this.state.emit('change', 'serial', { type: 'route', id: dest, value: src });
                    }
                    break;
            }
        }
    }
    /**
     * INTERNAL: Updates serial port directions based on data from the Videohub
     *
     * @param {string} labeltype - the command/data type being passed
     * @param {Object} object - the collected data
     * @access protected
     * @since 1.1.0
     */
    updateSerialDirections(cmd) {
        for (const item of cmd.parameters) {
            const num = item.keyNumber();
            const dir = item.valueString();
            if (num === undefined) {
                continue;
            }
            switch (cmd.header) {
                case 'SERIAL PORT DIRECTIONS':
                    const obj = this.state.getSerial(num);
                    if (obj.direction !== dir) {
                        obj.direction = dir;
                        this.state.emit('change', 'serial', { type: 'direction', id: num, value: dir });
                    }
                    break;
            }
        }
    }
    /**
     * INTERNAL: Updates variables based on data from the Videohub
     *
     * @param {string} labeltype - the command/data type being passed
     * @param {Object} object - the collected data
     * @access protected
     * @since 1.1.0
     */
    updateStatus(cmd) {
        for (const item of cmd.parameters) {
            const num = item.keyNumber();
            const label = item.valueString();
            if (num === undefined) {
                continue;
            }
            let obj;
            switch (cmd.header) {
                case 'VIDEO INPUT STATUS':
                    obj = this.state.getInput(num);
                    if (obj.status !== label) {
                        obj.status = label;
                        this.state.emit('change', 'input', { type: 'status', id: num, value: label });
                    }
                    break;
                case 'VIDEO OUTPUT STATUS':
                    obj = this.state.getOutput(num);
                    if (obj.status !== label) {
                        obj.status = label;
                        this.state.emit('change', 'output', { type: 'status', id: num, value: label });
                    }
                    break;
                case 'SERIAL PORT STATUS':
                    obj = this.state.getSerial(num);
                    if (obj.status !== label) {
                        obj.status = label;
                        this.state.emit('change', 'serial', { type: 'status', id: num, value: label });
                    }
                    break;
            }
        }
    }
}
exports.VideohubReceiver = VideohubReceiver;
//# sourceMappingURL=VideohubReceiver.js.map