var wsocket = null;
var client = null;
var reconnectionInterval = null;
var connectionRetries = 0;
var disconnectCount = 0;

function connectWebSocket(socketId, clientId, onConnectFunction, onCloseFunction) {
    if (client == null) {
        connectAndReconnect(socketId, clientId, onConnectFunction, onCloseFunction);
    }
}

function connectAndReconnect(socketId, clientId, onConnectFunction, onCloseFunction) {
    if (reconnectionInterval != null) {
        connectionRetries++;
        clearInterval(reconnectionInterval);
        reconnectionInterval = null;

        // Fallback - reload page (1.5m)
        if (connectionRetries > 45) {

            connectionRetries = 0;
            // Verify connection before reloading
            $.get('/check', function () {
                location.reload();
            });
        }
    }

    if (client != null) {
        try {
            client.disconnect();
            wsocket.close();
        } catch (e) {
            if (console) {
                console.log("Could not disconnect socket");
            }
        }
        $('#connection-spinner').hide();
        client = null;
        wsocket = null;
    }

    try {
        wsocket = new SockJS('/server');
        client = Stomp.over(wsocket);

        var onConnect = function (frame) {
            if (disconnectCount >= 5) {
                $('#connection-spinner').addClass("connection-warning");
            }
            $('#connection-spinner').removeClass("connection-error");
            $('#connection-spinner').removeClass("connection-starting");
            connectionRetries = 0;
            onConnectFunction(client);
        };

        var onClose = function (frame) {
            disconnectCount++
            $('#connection-spinner').addClass("connection-error");
            $('#connection-spinner').removeClass("connection-starting");
            if (onCloseFunction) {
                onCloseFunction(client);
            }
            initReconnectionInterval(socketId, clientId, onConnectFunction, onCloseFunction);
        };

        client.connect({"client-id": socketId}, onConnect, onClose);

    } catch (e) {
        if (console) {
            console.log('Disregarding websocket context')
        }
        initReconnectionInterval(socketId, clientId, onConnectFunction, onCloseFunction);
    }

}

function initReconnectionInterval(socketId, clientId, onConnectFunction, onCloseFunction) {
    if (reconnectionInterval == null) {
        reconnectionInterval = window.setInterval(function () {
            connectAndReconnect(socketId, clientId, onConnectFunction, onCloseFunction);
        }, 11000);
    }
}

function closeAndFinishWebSocket() {
    try {
        clearInterval(reconnectionInterval);
        reconnectionInterval = null;
        disconnectWebSocket();
    } catch (e) {
        if (console) {
            console.log("Could not finalize socket");
        }
    }
}

function disconnectWebSocket() {
    try {
        if (client && client.connected) {
            wsocket.close();
            client.disconnect();
        }
    } catch (e) {
        if (console) {
            console.log("Could not disconnect socket");
        }
    }
}

