"use strict";
var packet_stream_1 = require("./packet-stream");
var net = require("net");
var ws = require("ws");
var temp = require("temp");
var helpers = require("../../common/helpers");
var SocketProxyFactory = (function () {
    function SocketProxyFactory($logger, $config, $projectData, $projectDataService, $options) {
        this.$logger = $logger;
        this.$config = $config;
        this.$projectData = $projectData;
        this.$projectDataService = $projectDataService;
        this.$options = $options;
    }
    SocketProxyFactory.prototype.createTCPSocketProxy = function (factory) {
        var _this = this;
        var socketFactory = function (callback) { return helpers.connectEventually(factory, callback); };
        this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
        var server = net.createServer({
            allowHalfOpen: true
        });
        server.on("connection", function (frontendSocket) {
            _this.$logger.info("Frontend client connected.");
            frontendSocket.on("end", function () {
                _this.$logger.info('Frontend socket closed!');
                if (!(_this.$config.debugLivesync && _this.$options.watch)) {
                    process.exit(0);
                }
            });
            socketFactory(function (backendSocket) {
                _this.$logger.info("Backend socket created.");
                backendSocket.on("end", function () {
                    _this.$logger.info("Backend socket closed!");
                    if (!(_this.$config.debugLivesync && _this.$options.watch)) {
                        process.exit(0);
                    }
                });
                frontendSocket.on("close", function () {
                    console.log("frontend socket closed");
                    if (!backendSocket.destroyed) {
                        backendSocket.destroy();
                    }
                });
                backendSocket.on("close", function () {
                    console.log("backend socket closed");
                    if (!frontendSocket.destroyed) {
                        frontendSocket.destroy();
                    }
                });
                backendSocket.pipe(frontendSocket);
                frontendSocket.pipe(backendSocket);
                frontendSocket.resume();
            });
        });
        var socketFileLocation = temp.path({ suffix: ".sock" });
        server.listen(socketFileLocation);
        if (!this.$options.client) {
            this.$logger.info("socket-file-location: " + socketFileLocation);
        }
        return server;
    };
    SocketProxyFactory.prototype.createWebSocketProxy = function (factory) {
        var _this = this;
        var socketFactory = function (callback) { return helpers.connectEventually(factory, callback); };
        var localPort = 8080;
        this.$logger.info("\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
        var server = ws.createServer({
            port: localPort,
            verifyClient: function (info, callback) {
                _this.$logger.info("Frontend client connected.");
                socketFactory(function (_socket) {
                    _this.$logger.info("Backend socket created.");
                    info.req["__deviceSocket"] = _socket;
                    callback(true);
                });
            }
        });
        server.on("connection", function (webSocket) {
            var encoding = "utf16le";
            var deviceSocket = webSocket.upgradeReq["__deviceSocket"];
            var packets = new packet_stream_1.PacketStream();
            deviceSocket.pipe(packets);
            packets.on("data", function (buffer) {
                webSocket.send(buffer.toString(encoding));
            });
            webSocket.on("message", function (message, flags) {
                var length = Buffer.byteLength(message, encoding);
                var payload = new Buffer(length + 4);
                payload.writeInt32BE(length, 0);
                payload.write(message, 4, length, encoding);
                deviceSocket.write(payload);
            });
            deviceSocket.on("end", function () {
                _this.$logger.info("Backend socket closed!");
                process.exit(0);
            });
            webSocket.on("close", function () {
                _this.$logger.info('Frontend socket closed!');
                process.exit(0);
            });
        });
        this.$logger.info("Opened localhost " + localPort);
        return server;
    };
    return SocketProxyFactory;
}());
exports.SocketProxyFactory = SocketProxyFactory;
$injector.register("socketProxyFactory", SocketProxyFactory);
