function addLog(message) {
    console = $('#Log');
    console.append(message + '\n\n');
    console.scrollTop(console[0].scrollHeight - console.height());
}

function successHandler(data) {
    addLog(JSON.stringify(data));
}

function errorHandler(data) {
    addLog('Request failed (' + data.status + '): ' + data.responseText);
}

function generalAjax(data) {
    $.ajax(data).done(successHandler).fail(errorHandler);
}

$(document).ready(function(event) {
    var apiRoot = '/api/1/';
    $('#EmulatorUrl').text(window.location);
    $('#EmulatorUrl').attr('href', window.location);

    $('#GetScenarios').on('click', function(event) {
        generalAjax({
            dataType: 'json',
            url: apiRoot + 'scenarios',
            cache: false
        });
    });
    
    $('#GetScenario').on('click', function(event) {
        sessionId = $('#ScenarioId').val();
        generalAjax({
            dataType: 'json',
            url: apiRoot + 'scenarios/' + sessionId,
            cache: false
        });
    });
    
    $('#GetSessions').on('click', function(event) {
        generalAjax({
            dataType: 'json',
            url: apiRoot + 'sessions',
            cache: false
        });
    });
    
    $('#OpenSession').on('click', function(event) {
        scenarioId = $('#ScenarioId').val();
        $.ajax({
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify({scenarioId: scenarioId}),
            url: apiRoot + 'sessions',
            type: 'POST'
        })
        .done(function(data) {
            successHandler(data);
            $('#SessionId').val(data.sessionId);
        })
        .fail(errorHandler);
    });

    $('#GetSession').on('click', function(event) {     
        sessionId = $('#SessionId').val();
        generalAjax({
            dataType: 'json',
            url: apiRoot + 'sessions/' + sessionId,
            cache: false
        });
    });
    
    $('#StopSession').on('click', function(event) {     
        sessionId = $('#SessionId').val();
        generalAjax({
            dataType: 'json',
            contentType: 'application/json',
            url: apiRoot + 'sessions/' + sessionId + '/command',
            data: JSON.stringify({command: 'stop'}),
            type: 'POST'
        });
    });
    
    $('#RestartSession').on('click', function(event) {     
        sessionId = $('#SessionId').val();
        generalAjax({
            dataType: 'json',
            contentType: 'application/json',
            url: apiRoot + 'sessions/' + sessionId + '/command',
            data: JSON.stringify({command: 'restart'}),
            type: 'POST'
        });
    });

    $('#PingSession').on('click', function(event) {     
        sessionId = $('#SessionId').val();
        generalAjax({
            dataType: 'json',
            contentType: 'application/json',
            url: apiRoot + 'sessions/' + sessionId + '/command',
            data: JSON.stringify({command: 'ping'}),
            type: 'POST'
        });
    });
    
    $('#DeleteSession').on('click', function(event) {     
        sessionId = $('#SessionId').val();
        generalAjax({
            dataType: 'json',
            url: apiRoot + 'sessions/' + sessionId,
            cache: false,
            type: 'DELETE'
        });
    });
    
    $('#LaunchSession').on('click', function(event) {
        var url = '/emulation?session=' + $('#SessionId').val();
        window.open(url);
    });

    $('#GetSessionConfig').on('click', function(event) {
        sessionId = $('#SessionId').val();
        generalAjax({
            dataType: 'json',
            url: apiRoot + 'sessions/' + sessionId + '/config',
            cache: false
        });
    });
    
    $('#SetSessionConfig').on('click', function(event) {        
        sessionId = $('#SessionId').val();
        replayProtectionEnabled = $('#ReplayProtectionEnabled').val() == '1' ? true : false;
        generalAjax({
            dataType: 'json',
            contentType: 'application/json',
            url: apiRoot + 'sessions/' + sessionId + '/config',
            type: 'PUT',
            cache: false,
            data: JSON.stringify({replayProtectionEnabled: replayProtectionEnabled})
        });
    });
});
