"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatusType = exports.State = exports.Status = void 0;
var UnityRunner_1 = require("./Unity/UnityRunner");
var Status = /** @class */ (function () {
    function Status(Main) {
        this.states = new Map();
        this._Main = Main;
        this.ensure();
        this.startClock();
    }
    Status.prototype.update = function (type, state, message, buttons) {
        this.states.set(type, { state: state, message: message, buttons: buttons });
        this._Main.WebServer.socket.emit('status', this.getState());
    };
    Status.prototype.reboot = function (type) {
        switch (type) {
            case StatusType.CameraPC: {
                this._Main.CameraRunner.sendCommand('reboot', function (response) {
                    console.log('CameraRunner reboot response:', response);
                });
            }
            case StatusType.CameraProcess: {
                this._Main.CameraRunner.sendCommand('restart', function (response) {
                    console.log('CameraRunner restart response:', response);
                });
            }
            case StatusType.CameraUnityStream: {
                this._Main.CameraRunner.sendCommand('restart', function (response) {
                    console.log('CameraRunner restart response:', response);
                });
                this._Main.UnityRunner.restart(UnityRunner_1.StartOrigin.Manual);
            }
            case StatusType.UnityBuild: {
                this._Main.UnityRunner.restart(UnityRunner_1.StartOrigin.Manual);
            }
            case StatusType.ReplayFunction: {
                console.log('ReplayFunction reboot requested. Not implemented.');
            }
        }
    };
    Status.prototype.start = function (type) {
        switch (type) {
            case StatusType.UnityBuild: {
                this._Main.UnityRunner.requestStart(UnityRunner_1.StartOrigin.Manual);
            }
        }
    };
    Status.prototype.stop = function (type) {
        switch (type) {
            case StatusType.UnityBuild: {
                this._Main.UnityRunner.requestStop();
            }
        }
    };
    Status.prototype.getState = function () {
        var state = {};
        this.states.forEach(function (value, key) {
            state[key] = value;
        });
        return state;
    };
    Status.prototype.startClock = function () {
        var _this = this;
        var hadInvalidLastRound = false;
        setInterval(function () {
            var hasInvalid = false;
            _this.states.forEach(function (state, key) {
                if (state.state === State.Red)
                    hasInvalid = true;
            });
            if (hasInvalid && !hadInvalidLastRound) {
                hadInvalidLastRound = true;
                console.warn('One ore more status entries are invalid, waiting to see if they recover...');
            }
            else if (hasInvalid && hadInvalidLastRound) {
                console.error('One ore more status entries are still invalid after 5 seconds, restarting system.');
                _this._Main.Twilio.sendError('Status', 'One ore more status entries are still invalid after 5 seconds, restarting system.');
                _this._Main.restart();
            }
            else {
                _this._Main.Twilio.sendError('Status', null);
                hadInvalidLastRound = false;
            }
        }, 5000);
    };
    Status.prototype.ensure = function () {
        for (var type in StatusType) {
            if (!this.states.has(StatusType[type]))
                this.states.set(StatusType[type], {
                    state: State.Gray,
                    message: 'Loading...',
                });
        }
    };
    Status.prototype.doLog = function () {
        console.clear();
        this.states.forEach(function (state, key) {
            console.log("[".concat(key, "] ").concat(state.state, " - ").concat(state.message));
        });
    };
    return Status;
}());
exports.Status = Status;
var State;
(function (State) {
    State["Green"] = "GREEN";
    State["Yellow"] = "YELLOW";
    State["Red"] = "RED";
    State["Gray"] = "GRAY";
})(State || (exports.State = State = {}));
var StatusType;
(function (StatusType) {
    StatusType["CameraPC"] = "CAMERAPC";
    StatusType["CameraProcess"] = "CAMERAPROCESS";
    StatusType["CameraUnityStream"] = "CAMERAUNITYSTREAM";
    StatusType["UnityBuild"] = "UNITYBUILD";
    StatusType["ReplayFunction"] = "REPLAYFUNCTION";
})(StatusType || (exports.StatusType = StatusType = {}));
//# sourceMappingURL=Status.js.map