"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebServer = void 0;
var express = require("express");
var socket_io_1 = require("socket.io");
var http_1 = require("http");
var DashboardRouter_1 = require("./DashboardRouter");
var path_1 = require("path");
var CalibrationRouter_1 = require("./CalibrationRouter");
var PREFIX = '[WebServer]';
var WebServer = /** @class */ (function () {
    function WebServer(Main) {
        this._Main = Main;
        this.Dashboard = new DashboardRouter_1.DashboardRouter(this._Main);
        this.Calibration = new CalibrationRouter_1.CalibrationRouter(this._Main);
        this.prepare();
    }
    WebServer.prototype.prepare = function () {
        var _this = this;
        this.app = express();
        this.httpServer = (0, http_1.createServer)(this.app);
        this.socket = new socket_io_1.Server(this.httpServer);
        this.app.use(express.static((0, path_1.join)(__filename, '..', '..', '..', 'frontend', 'static')));
        this.app.use(this.Dashboard.Router);
        this.app.use(this.Calibration.Router);
        this.socket.on('connection', function (socket) {
            socket.emit('cameraRunnerState', _this._Main.CameraRunner.getState());
            socket.emit('unityRunnerState', _this._Main.UnityRunner.getStatus());
            socket.emit('unityWebSocketState', _this._Main.UnityWebSocket.getState());
            socket.on('cameraRunner', function (command) {
                var _a;
                var args = [];
                for (var _i = 1; _i < arguments.length; _i++) {
                    args[_i - 1] = arguments[_i];
                }
                return (_a = _this._Main.CameraRunner).handle.apply(_a, __spreadArray([command], args, false));
            });
            socket.on('unityRunner', function (command) {
                var _a;
                var args = [];
                for (var _i = 1; _i < arguments.length; _i++) {
                    args[_i - 1] = arguments[_i];
                }
                return (_a = _this._Main.UnityRunner).handle.apply(_a, __spreadArray([command], args, false));
            });
            socket.on('unityWebSocket', function (command) {
                var _a;
                var args = [];
                for (var _i = 1; _i < arguments.length; _i++) {
                    args[_i - 1] = arguments[_i];
                }
                return (_a = _this._Main.UnityWebSocket).handle.apply(_a, __spreadArray([command], args, false));
            });
        });
    };
    WebServer.prototype.listen = function () {
        var _this = this;
        return new Promise(function (resolve) {
            var port = _this._Main.Config.webServer.port;
            _this.httpServer.listen(port, function () {
                console.log(PREFIX, "Listening on port http://127.0.0.1:".concat(port));
                resolve();
            });
        });
    };
    return WebServer;
}());
exports.WebServer = WebServer;
//# sourceMappingURL=WebServer.js.map