﻿function HomeViewModel() {
    var self = this;
    var userId = 0;
    var sessionId = 0;

    self.options = ko.observableArray();
    self.sessions = ko.observableArray();

    // Submit a vote
    self.doVote = function (data, event) {
        $.ajax({
            type: 'PUT',
            url: '/api/user/'+userId+'/vote',
            contentType: 'application/json',
            data: JSON.stringify({
                OptionId: data.Id,
                SessionId: sessionId
            }),

            success: function (returnData) {
                var currentRow = event.currentTarget.parentElement.parentElement;
                $(currentRow).siblings().removeClass("success");
                $(currentRow).addClass("success");

                setTimeout(function () { window.location = "/Result?session=" + sessionId; }, 500);
            }
        });
    }

    self.keyIsEnter = function (key, callback) {
        if (key && key.keyCode == 13)
        {
            callback();
        }
    }

    // Do login
    self.submitLogin = function () {
        $.ajax({
            type: 'PUT',
            url: '/api/user',
            contentType: 'application/json',
            data: JSON.stringify({
                Name: $("#Name").val()
            }),

            success: function (data) {
                userId = data;
                self.highlightPreviousVote();
                $("#login-box").hide();
                $("#vote-table").show();
            }
        });
    }

    self.submitSession = function () {
        sessionId = $("#session-select").val();
        window.location = "?session=" + sessionId;
    }

    self.createSession = function () {
        $.ajax({
            type: 'POST',
            url: '/api/session',
            contentType: 'application/json',

            data: JSON.stringify({
                Name: $("#session-create").val()
            }),

            success: function (data) {
                sessionId = data;
                window.location = "?session=" + sessionId;
            }
        })
    }

    self.highlightPreviousVote = function () {
        $.ajax({
            type: 'GET',
            url: '/api/user/' + userId + '/session/' + sessionId + '/vote',
            contentType: 'application/json',

            success: function (data) {
                for (var index = 0; index < data.length; index++) {
                    var previousOptionId = data[index].OptionId;
                    var previousOption = self.options().filter(function (d) { return d.Id == previousOptionId }).pop();
                    var previousOptionRowIndex = self.options().indexOf(previousOption);
                    var matchingRow = $("#inner-vote-table > tbody > tr").eq(previousOptionRowIndex);
                    matchingRow.addClass("success");
                }
            }
        });
    }

    self.allSessions = function () {
        $.ajax({
            type: 'GET',
            url: '/api/session',

            success: function (data) {
                self.sessions(data);
            }
        })
    }

    self.allOptions = function () {
        // Get all options
        $.ajax({
            type: 'GET',
            url: "/api/option",

            success: function (data) {
                self.options(data);
            }
        });
    }

    self.addOption = function () {
        //Don't submit without an entry in the name field
        if ($("#newName").val() == "") {
            return;
        }

        var newName = $("#newName").val();
        var newDescription = $("#newDescription").val()
        var newInfo = $("#newInfo").val()

        //Reset before posting, to prevent double posts.
        $("#newName").val("")
        $("#newDescription").val("")
        $("#newInfo").val("")

        $.ajax({
            type: 'POST',
            url: '/api/option',
            contentType: 'application/json',

            data: JSON.stringify({
                Name: newName,
                Description: newDescription,
                Info: newInfo
            }),

            success: function () {
                self.allOptions();
            }
        })
    }

    $(document).ready(function () {
        sessionId = getSessionId();

        if (!sessionId) {
            self.allSessions();
            $("#login-box").hide();
            $("#sessions").show();
        }
        else {
            $("#sessions").hide();
            $("#login-box").show();
        }

        self.allOptions();

        $("#Name-submit").click(self.submitLogin);
        //Submit on pressing return key
        $("#Name").keypress(function (event) { self.keyIsEnter(event, self.submitLogin); });

        //Add option on pressing return key
        $("#newOptionRow").keypress(function(event) { self.keyIsEnter(event, self.addOption); });
    });
}

ko.applyBindings(new HomeViewModel());

@@ -4786,16 +4786,17 @@
 function
+ 
 (event) 
@@ -4836,24 +4836,178 @@
 Option); %7D);
+%0A%0A        //Create session on pressing return key%0A        $(%22#session-create%22).keypress(function (event) %7B self.keyIsEnter(event, self.createSession); %7D);
 %0A    %7D);%0A%7D%0A%0A

Add return-key hook for new session