﻿/* ********************************************************** */
/* KeaneGuiControllerPrototype declaration                    */        
/* ********************************************************** */
var KeaneGuiControllerPrototype = function() {
    // Private properties/methods.
    var 
        version = "1.0.0",
        constants = KeaneConstantsPrototype(),
        formManager = KeaneFormManagerPrototype( { constants : constants } ),
        cacheManager = KeaneCacheManagerPrototype( { constants : constants } ),
        postbackManager = KeanePostbackManagerPrototype( { constants : constants } ),
        popupManager = KeanePopupManagerPrototype( { constants : constants, cacheManager : cacheManager } ),
        callbackManager = KeaneCallbackManagerPrototype( { constants : constants , popupManager : popupManager, cacheManager : cacheManager } ),
        commandManager = KeaneCommandManagerPrototype( { constants : constants , popupManager : popupManager, cacheManager : cacheManager, callbackManager : callbackManager, postbackManager : postbackManager } ),
        that = {};

    // Standard initialisation routine.
    var initialise = function () {    
        cacheManager.initialise();
        that.clientName = cacheManager.standardInfo.clientName;
        that.defaultMessageInfo = cacheManager.defaultMessageInfo;
        commandManager.initialise();
    };

    // Gets the edit mode.
    // 0 = Unspecified (the default).
    // 1 = View (The user is viewing data)
    // 2 = Insert (The user is inserting data)
    // 3 = Update (The user is updating data)
    var getEditMode = function() {
        return cacheManager.getEditMode();
    };
    
    // Gets a value from the cache based upon the passed name.
    var getCachedValue = function(name) {
        return cacheManager.getNamedValue(name);
    };

    // Adds a named value to the cache.
    var setCachedValue = function(name, value) {
        
    };

    // Invokes a command.
    var invokeCommand = function(name, data) {
        commandManager.invoke(name, data);
    };

    // Updates a command state.
    var setCommandState = function(commandStateInfo) {
        commandManager.setCommandState(commandStateInfo);
    };

    // Processes a command failure.
    var processCommandFailure = function(commandName) {
        commandManager.onFailure(commandName);
    };

    // Processes a command success.
    var processCommandSuccess = function(commandName) {
        commandManager.onSuccess(commandName);
    };

    // Processes a server response.
    var processServerResponse = function(serverResponse) {
        hideProgressMessage();
        if (serverResponse) {
            callbackManager.processResponse(serverResponse);        
        }
        else {
            commandManager.onSuccess();
        }
    };

    // Processes a server error.
    var processServerError = function(serverResponse) {
        hideProgressMessage();
        if (serverResponse) {
            callbackManager.processResponse(serverResponse);        
        }
        else {
            commandManager.onFailure();
        }
    };
    
    // Returns a cached command.
    var getCommand = function(name) {
        return commandManager.getCommand(name);
    };

    // Displays an information message to the user.
    var displayInformationMessage = function(message, title) {        
        popupManager.displayMessage(constants.MESSAGE_TYPE_INFORMATION, message, title);
    };

    // Displays a progress message to the user.
    var displayProgressMessage = function(message, title) {
        popupManager.displayMessage(constants.MESSAGE_TYPE_PROGRESS, message, title);
    };

    // Hides the progress message from the user.
    var hideProgressMessage = function() {
        popupManager.hideMessage(constants.MESSAGE_TYPE_PROGRESS);
    };

    // Displays a validation message to the user.
    var displayValidationMessage = function(message, title) {
        popupManager.displayMessage(constants.MESSAGE_TYPE_VALIDATION, message, title);
    };

    // Displays an error message to the user.
    var displayErrorMessage = function(message, title) {
        popupManager.displayMessage(constants.MESSAGE_TYPE_ERROR, message, title);
    };

    // Refreshes the state of the UI .
    var refreshUI = function() {
        commandManager.refreshUI();
    };   

    // Refreshes the state of the UI .
    var registerCallbackHandler = function(handler) {
        callbackManager.registerHandler(handler);
    };   

    // Gets the named value stored within the callback.
    var getCallbackValue = function(name) {
        return callbackManager.getNamedValue(name);
    };   

    // Performs page load tasks.
    var onPageLoad = function() {
        commandManager.onPageLoad();
        popupManager.onPageLoad();
    };   

    // Public properties/methods.
    that.initialise = initialise;
    that.onPageLoad = onPageLoad;
    that.constants = constants;
    // ... caching helper routines;
    that.getCachedValue = getCachedValue;
    that.setCachedValue = setCachedValue;
    // ... command helper routines;
    that.getCommand = getCommand;
    that.setCommandState = setCommandState;
    that.invokeCommand = invokeCommand;
    that.processCommandFailure = processCommandFailure;
    that.processCommandSuccess = processCommandSuccess;
    // ... server callback helper routines.
    that.getCallbackValue = getCallbackValue;
    that.processServerResponse = processServerResponse;
    that.processServerError = processServerError;
    that.registerCallbackHandler = registerCallbackHandler;
    // ... UI helper routines;
    that.refreshUI = refreshUI;
    that.getEditMode = getEditMode;
    that.displayInformationMessage = displayInformationMessage;
    that.displayErrorMessage = displayErrorMessage;
    that.displayValidationMessage = displayValidationMessage;
    that.displayProgressMessage = displayProgressMessage;
    that.hideProgressMessage = hideProgressMessage;
    
    // Return constructed object.
    return that;
}

/* ********************************************************** */
/* GuiController instantiation                                */        
/* ********************************************************** */
if (!this.GuiController) {
    GuiController = KeaneGuiControllerPrototype();
}

/* ********************************************************** */
/* GuiController initialisation                               */        
/* ********************************************************** */
(function () {
    $(document).ready(function() {
        GuiController.initialise();
    });
})();