"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompanionDevice = exports.CompanionDeviceFactory = void 0;
const DeviceTypes_1 = require("./DeviceTypes");
const DeviceTypes_2 = require("../../common/DeviceTypes");
const events_1 = require("events");
const node_osc_1 = require("node-osc");
const LogTypes_1 = require("../../common/LogTypes");
const shortid = require("shortid");
const COMPANION_TYPE = 'COMPANION';
class CompanionDeviceFactory {
    constructor(logger) {
        this.logger = logger;
    }
    getType() { return COMPANION_TYPE; }
    create(config) {
        if (!config)
            return;
        if (!config.id)
            config.id = shortid.generate();
        const device = new CompanionDevice(config, this.logger);
        return device;
    }
}
exports.CompanionDeviceFactory = CompanionDeviceFactory;
class CompanionDevice extends events_1.EventEmitter {
    constructor(config, logger) {
        super();
        this.config = config;
        this.logger = logger;
        this.status = DeviceTypes_2.DeviceStatus.Unknown;
        this.initConnection();
    }
    initConnection() {
        if (this.client) {
            this.client.close();
            delete this.client;
        }
        if (this.config.enabled) {
            const port = this.config.params.port || 12321;
            this.client = new node_osc_1.Client(this.config.address, port);
            this.setStatus(DeviceTypes_2.DeviceStatus.Ok);
            this.logger.log('Connected to Companion', LogTypes_1.LogLevel.Info, { type: 'DEVICE', id: this.config.id });
        }
    }
    setStatus(status) {
        if (this.status != status) {
            this.status = status;
            this.emit(DeviceTypes_1.DeviceInterfaceEvents.StatusChanged, this.status);
        }
    }
    getConfig() { return this.config; }
    updateConfig(config) {
        this.config = config;
        this.initConnection();
    }
    connect() {
        if (!this.client) {
            const port = this.config.params.port || 12321;
            this.client = new node_osc_1.Client(this.config.address, port);
            this.setStatus(DeviceTypes_2.DeviceStatus.Ok);
            this.logger.log('Connected to Companion', LogTypes_1.LogLevel.Info, { type: 'DEVICE', id: this.config.id });
        }
    }
    disconnect() {
        if (this.client) {
            this.client.close();
            delete this.client;
        }
    }
    getStatus() { return this.status; }
    getConnected() { return true; }
    getActions() {
        return {
            in: ['press'],
            out: []
        };
    }
    getParams() {
        return {};
    }
    trigger(actionId, edge, params) {
        if (this.client) {
            if (actionId === 'press') {
                const direction = edge !== 0;
                try {
                    this.client.send(`/location/${params.page}/${params.row}/${params.column}/${direction ? 'down' : 'up'}`, 1, (err) => {
                        if (err) {
                            console.log(err);
                            this.logger.log(`Companion trigger error: ${err}`, LogTypes_1.LogLevel.Error, { type: 'DEVICE', id: this.config.id });
                            this.client?.close();
                            delete this.client;
                            const port = this.config.params.port || 12321;
                            this.client = new node_osc_1.Client(this.config.address, port);
                        }
                        else {
                            if (direction)
                                this.logger.log(`Companion button down: ${params.page}/${params.row}/${params.column}`, LogTypes_1.LogLevel.Info, { type: 'DEVICE', id: this.config.id });
                            else
                                this.logger.log(`Companion button up: ${params.page}/${params.row}/${params.column}`, LogTypes_1.LogLevel.Info, { type: 'DEVICE', id: this.config.id });
                        }
                    });
                }
                catch (e) {
                    this.logger.log(`Companion trigger exception: ${e}`, LogTypes_1.LogLevel.Error, { type: 'DEVICE', id: this.config.id });
                }
            }
            else {
                this.logger.log(`Unknown companion action: ${actionId}`, LogTypes_1.LogLevel.Error, { type: 'DEVICE', id: this.config.id });
            }
        }
        else {
            this.logger.log(`Companion not connected.`, LogTypes_1.LogLevel.Error, { type: 'DEVICE', id: this.config.id });
        }
    }
    addHandler(_handlerId, _action, _params, _handler) {
        // nop
    }
    removeHandler(_handlerId) {
        // nop
    }
    destroy() {
        if (this.client) {
            this.client.close();
            delete this.client;
        }
        this.removeAllListeners();
    }
}
exports.CompanionDevice = CompanionDevice;
//# sourceMappingURL=CompanionDevice.js.map