﻿//
// zifmia-controller.js - Copyright © 2011 - David Cornelson
//

//
// Initialize Page Variables
//
var NOT_LOGGED_IN = "User is not logged in.";
var LOGGED_IN = "User {0} is logged in.";

var currentTurn = 1;
var totalTurns = 1;
var firstTurn = 1;

var currentAuthKey = "";
var currentplayerKey = "";
var currentSessionKey = "";
var currentGameKey = "";

var sessionList = new Array();
var gameList = new Array();

var displayName = "";

var zifmiaCookie = 'zifmiaData';

var authKeyPanel = "#authKeyPanel";
var sessionKeyPanel = "#sessionKeyPanel";
var gameKeyPanel = "#gameKeyPanel";
var displayNamePanel = "#displayNamePanel";
var inputPanel = "#inputPanel";

var gameListPanel = "#gameListPanel";
var titlePanel = "#titlePanel";
var chapterPanel = "#chapterPanel";
var mainPanel = "#mainPanel";
var statusChannel = "#statusPanel";
var messagePanel = "#messagePanel";
var errorPanel = "#errorPanel";

//
// Channel Names (these are the only ones we care about for now)
//
var MAIN_CHANNEL = "MAIN";
var PROLOGUE_CHANNEL = "PLOG";
var TITLE_CHANNEL = "TITL";
var CHAPTER_CHANNEL = "CHAP";
var LOCATION_CHANNEL = "LOCN";
var TURN_CHANNEL = "TURN";
var DEATH_CHANNEL = "DEAD";
var CREDITS_CHANNEL = "CRED";
var SCORE_CHANNEL = "SCOR";
var HINT_CHANNEL = "HINT";

//
// Utility functions
//
function GetChannelText(channelData, channelName) {
    for (channelIndex = 0; channelIndex < channelData.length; channelIndex++) {
        if (channelData[channelIndex].Name == channelName)
            return channelData[channelIndex].Content;
    }
    return "";
}

function GameLink(authKey, gameKey, title) {
    return "<a href=\"javascript:UIStartGame('" + authKey + "','" + gameKey + "');\">" + title + "</a>";
}

function SessionLink(authKey, sessionKey, turn) {
    return "<a href=\"javascript:UIStartGame('" + authKey + "','" + gameKey + "');\">" + title + "</a>";
}


//
// AJAX error messages handled here...
//
function errorHandler(xhr, textStatus, errorThrown) {
    $(errorPanel).text(xhr.status);
}

//
// UI functions...transition from DOM to AJAX
//

function UIRegisterUser() {
    var ui_username = "regression";
    var ui_password = "99";
    var ui_displayName = "Reggie Q. User";
    var ui_emailAddress = "regression@textfyre.com";

    RegisterUser(ui_username, ui_password, ui_displayName, ui_emailAddress);
}

function UILogin() {
    var ui_username = "regression";
    var ui_password = "99";

    Login(ui_username, ui_password);
}

function UIListGames() {
    ListGames();
}

function UIStartGame(authKey, gameKey) {
    SessionStart(authKey, gameKey);

    currentAuthKey = authKey;
    currentGameKey = gameKey;
}

function UISendCommand() {
    command = "look at clock";
    SessionCommand(currentAuthKey, currentSessionKey, currentTurn, command);
}

function UIGetSession(sessionKey) {
    SessionSet(currentAuthKey, sessionKey);
}

function UIGetHistory(turn) {
    SessionHistory(currentAuthKey, currentSessionKey, turn);
}

function UIUserSessionList() {
    UserSessionList(currentAuthKey);
}

// zifmiaRegistrationViewModel.Message contains a text message of the results of the registration process.
// zifmiaRegistrationViewModel.Status contains 1 for success and 0 for failure.
function registerHandler(zifmiaRegistrationViewModel) {
    $(messagePanel).text(zifmiaRegistrationViewModel.Message);
    $(statusPanel).text(zifmiaRegistrationViewModel.Status);
}

// zifmiaLoginViewModel.AuthKey is the authKey for this user's login.
// zifmiaLoginViewModel.DisplayName is the displayName for this user.
// zifmiaLoginViewModel.Message is the status message of the login.
// zifmiaLoginViewModel.Status is 1 for success or 0 for failure.
function loginHandler(zifmiaLoginViewModel) {
    $(messagePanel).text(zifmiaLoginViewModel.Message);
    $(statusPanel).text(zifmiaLoginViewModel.Status);

    currentAuthKey = zifmiaLoginViewModel.AuthKey;
    displayName = zifmiaLoginViewModel.DisplayName;

    $(authKeyPanel).text(currentAuthKey);
    $(displayNamePanel).text(displayName);

    $(inputPanel).hide();

}

// zifmiaGamesViewModel.Message is the status message of the login.
// zifmiaGamesViewModel.Status is 1 for success or 0 for failure.
// zifmiaGamesViewModel.Games[0..n].Key is the gameKey for the game.
function listGamesHandler(zifmiaGamesViewModel) {

    $(gameListPanel).text("");

    if (zifmiaGamesViewModel.Games.length == 0) {
        $(messagePanel).text("No games to list.");
        return;
    }

    for (x = 0; x < zifmiaGamesViewModel.Games.length; x++) {

        var game = zifmiaGamesViewModel.Games[x];

        if (currentAuthKey == "")
            $(gameListPanel).append(game.Title);
        else
            $(gameListPanel).append(GameLink(currentAuthKey, game.GameKey, game.Title));
        $(gameListPanel).append("<br/>");
    }

    if (currentAuthKey == "")
        $(messagePanel).text("Must be logged in to play a game.");

}

// zifmiaViewModel.AuthKey is the auth key
// zifmiaViewModel.BranchId is the current branch id
// zifmiaViewModel.BranchMap is the list of branches for this session
// zifmiaViewModel.Channels[0..n].Name is the name of a channel
// zifmiaViewModel.Channels[0..n].Content is the response text of a channel
// zifmiaViewModel.Command is the current command
// zifmiaViewModel.GameKey is the game key
// zifmiaViewModel.HasNextNode tells if there are more nodes after the current node on the current branch
// zifmiaViewModel.HasPreviousNode tells if there are previous nodes on the current branch
// zifmiaViewModel.Message is a message about the service request, important for errors
// zifmiaViewModel.PlayerKey is the player key
// zifmiaViewModel.SessionKey is the session key
// zifmiaViewModel.Status is the status of the service request, either success or failure
// zifmiaViewModel.Turn is the displayed turn
function sessionStartHandler(zifmiaViewModel) {
    // use GetChannelText(zifmiaResponse.Channels, MAIN_CHANNEL) (or other channel name)
    // to retrieve text for a specific channel.

    $(messagePanel).text(zifmiaViewModel.Message);
    $(statusPanel).text(zifmiaViewModel.Status);

    $(mainPanel).text(GetChannelText(zifmiaViewModel.Channels, PROLOGUE_CHANNEL));
    $(mainPanel).append(GetChannelText(zifmiaViewModel.Channels, MAIN_CHANNEL));

    $(titlePanel).text(GetChannelText(zifmiaViewModel.Channels, TITLE_CHANNEL));
    $(chapterPanel).text(GetChannelText(zifmiaViewModel.Channels, CHAPTER_CHANNEL));
    $(locationPanel).text(GetChannelText(zifmiaViewModel.Channels, LOCATION_CHANNEL));

    currentTurn = zifmiaViewModel.Turn;
    currentGameKey = zifmiaViewModel.GameKey;
    currentSessionKey = zifmiaViewModel.SessionKey;
}

// zifmiaViewModel.AuthKey is the auth key
// zifmiaViewModel.BranchId is the current branch id
// zifmiaViewModel.BranchMap is the list of branches for this session
// zifmiaViewModel.Channels[0..n].Name is the name of a channel
// zifmiaViewModel.Channels[0..n].Content is the response text of a channel
// zifmiaViewModel.Command is the current command
// zifmiaViewModel.GameKey is the game key
// zifmiaViewModel.HasNextNode tells if there are more nodes after the current node on the current branch
// zifmiaViewModel.HasPreviousNode tells if there are previous nodes on the current branch
// zifmiaViewModel.Message is a message about the service request, important for errors
// zifmiaViewModel.PlayerKey is the player key
// zifmiaViewModel.SessionKey is the session key
// zifmiaViewModel.Status is the status of the service request, either success or failure
// zifmiaViewModel.Turn is the displayed turn
function sessionCommandHandler(zifmiaViewModel) {
    // use GetChannelText(zifmiaResponse.Channels, MAIN_CHANNEL) (or other channel name)
    // to retrieve text for a specific channel.

    $(messagePanel).text(zifmiaViewModel.Message);
    $(statusPanel).text(zifmiaViewModel.Status);

    $(mainPanel).text(GetChannelText(zifmiaViewModel.Channels, PROLOGUE_CHANNEL));
    $(mainPanel).append(GetChannelText(zifmiaViewModel.Channels, MAIN_CHANNEL));

    $(titlePanel).text(GetChannelText(zifmiaViewModel.Channels, TITLE_CHANNEL));
    $(chapterPanel).text(GetChannelText(zifmiaViewModel.Channels, CHAPTER_CHANNEL));
    $(locationPanel).text(GetChannelText(zifmiaViewModel.Channels, LOCATION_CHANNEL));

    currentTurn = zifmiaViewModel.Turn;
    currentGameKey = zifmiaViewModel.GameKey;
    currentSessionKey = zifmiaViewModel.SessionKey;
}

// zifmiaViewModel.AuthKey is the auth key
// zifmiaViewModel.BranchId is the current branch id
// zifmiaViewModel.BranchMap is the list of branches for this session
// zifmiaViewModel.Channels[0..n].Name is the name of a channel
// zifmiaViewModel.Channels[0..n].Content is the response text of a channel
// zifmiaViewModel.Command is the current command
// zifmiaViewModel.GameKey is the game key
// zifmiaViewModel.HasNextNode tells if there are more nodes after the current node on the current branch
// zifmiaViewModel.HasPreviousNode tells if there are previous nodes on the current branch
// zifmiaViewModel.Message is a message about the service request, important for errors
// zifmiaViewModel.PlayerKey is the player key
// zifmiaViewModel.SessionKey is the session key
// zifmiaViewModel.Status is the status of the service request, either success or failure
// zifmiaViewModel.Turn is the displayed turn
function sessionGetHandler(zifmiaViewModel) {

    $(messagePanel).text(zifmiaViewModel.Message);
    $(statusPanel).text(zifmiaViewModel.Status);

    $(mainPanel).text(GetChannelText(zifmiaViewModel.Channels, PROLOGUE_CHANNEL));
    $(mainPanel).append(GetChannelText(zifmiaViewModel.Channels, MAIN_CHANNEL));

    $(titlePanel).text(GetChannelText(zifmiaViewModel.Channels, TITLE_CHANNEL));
    $(chapterPanel).text(GetChannelText(zifmiaViewModel.Channels, CHAPTER_CHANNEL));
    $(locationPanel).text(GetChannelText(zifmiaViewModel.Channels, LOCATION_CHANNEL));

    currentTurn = zifmiaViewModel.Turn;
    currentGameKey = zifmiaViewModel.GameKey;
    currentSessionKey = zifmiaViewModel.SessionKey;
}

// zifmiaSessionsViewModel.Sessions[0..n].BranchMap is the list of branches
// zifmiaSessionsViewModel.Sessions[0..n].GameKey is the game key
// zifmiaSessionsViewModel.Sessions[0..n].GameTitle is the title of the game
// zifmiaSessionsViewModel.Sessions[0..n].PlayerKey is the player key
// zifmiaSessionsViewModel.Sessions[0..n].PlayerName is the player name
// zifmiaSessionsViewModel.Sessions[0..n].SessionKey is the session key
// zifmiaSessionsViewModel.Sessions[0..n].StartDate is when the session was started
// zifmiaSessionsViewModel.Status contains the status of the service request
// zifmiaSessionsViewModel.Message contains any message from the service request
function userSessionListHandler(zifmiaSessionsViewModel) {

    if (zifmiaSessionsViewModel.Sessions.length == 0) {
        $(messagePanel).text("No sessions to list.");
        return;
    }

    $(mainPanel).text("");

}

