﻿//
// Zifmia.js - Copyright © 2011 - David Cornelson
//
// This file should not need to be modified.
//

//
// Zifmia Service Layer - Each call is a routed REST call that returns JSON.
//
//var ZifmiaService           = "http://zifmia.textfyre.com/ZifmiaService/";
var ZifmiaService           = "http://localhost:61616/ZifmiaService/";
var ZifmiaRegister          = ZifmiaService + "Register/{0}/{1}/{2}/{3}";               // username, password, displayName, emailAddress
var ZifmiaLogin             = ZifmiaService + "Login/{0}/{1}";                          // username, password
var ZifmiaGames             = ZifmiaService + "Games";                                  // no arguments
var ZifmiaSessionStart      = ZifmiaService + "Session/Start/{0}/{1}";                  // authKey, gameKey
var ZifmiaSessionCommand    = ZifmiaService + "Session/Command/{0}/{1}/{2}/{3}/{4}";    // authKey, sessionKey, branchid, turn, command
var ZifmiaSessionGet        = ZifmiaService + "Session/Get/{0}/{1}";                    // authKey, sessionKey
var ZifmiaSessionGetHistory = ZifmiaService + "Session/History/{0}/{1}/{2}/{3}";        // authKey, sessionKey, branchId, turn
var ZifmiaUserSessionList   = ZifmiaService + "User/Session/List/{0}";                  // authKey
//
// End Initialization
//

//
// AJAX Functions
//
function RegisterUser(username, password, displayName, emailAddress) {
   
    $.ajax({
        type: "GET",
        url: ZifmiaRegister.format(username, password, displayName, emailAddress),
        dataType: "json",
        success: function (zifmiaRegistrationViewModel) {
            registerHandler(zifmiaRegistrationViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

function Login(username, password) {
    $.ajax({
        type: "GET",
        url: ZifmiaLogin.format(username, password),
        dataType: "json",
        success: function (zifmiaLoginViewModel) {
            loginHandler(zifmiaLoginViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

function ListGames() {
    $.ajax({
        type: "GET",
        url: ZifmiaGames,
        dataType: "json",
        success: function (zifmiaGamesViewModel) {
            listGamesHandler(zifmiaGamesViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

function SessionStart(authKey, gameKey) {
    $.ajax({
        type: "GET",
        url: ZifmiaSessionStart.format(authKey, gameKey),
        dataType: "json",
        success: function (zifmiaViewModel) {
            sessionStartHandler(zifmiaViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

// Note that if the client sends in a command from a history turn (less than the greatest turn number for this session)
// a new session will be created and returned with the response. This means the client should always be aware of the
// returned sessionKey.
function SessionCommand(authKey, sessionKey, branchid, turn, command) {
    $.ajax({
        type: "GET",
        url: ZifmiaSessionCommand.format(authKey, sessionKey, branchid, turn, command),
        dataType: "json",
        success: function (zifmiaViewModel) {
            sessionCommandHandler(zifmiaViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

//
// Get the last turn session and response data of a given session.
//
function SessionGet(authKey, sessionKey) {
    $.ajax({
        type: "GET",
        url: ZifmiaSessionGet.format(authKey, sessionKey),
        dataType: "json",
        success: function (zifmiaViewModel) {
            sessionGetHandler(zifmiaViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

//
// Get the history turn.
//
function SessionGetHistory(authKey, sessionKey, branchId, turn) {
    $.ajax({
        type: "GET",
        url: ZifmiaSessionGetHistory.format(authKey, sessionKey, branchId, turn),
        dataType: "json",
        success: function (zifmiaViewModel) {
            sessionGetHistoryHandler(zifmiaViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

function UserSessionList(authKey) {
    $.ajax({
        type: "GET",
        url: ZifmiaUserSessionList.format(authKey),
        dataType: "json",
        success: function (zifmiaSessionsViewModel) {
            userSessionListHandler(zifmiaSessionsViewModel);
        },
        error: function (xhr, textStatus, errorThrown) {
            errorHandler(xhr, textStatus, errorThrown);
        }
    });
}

