﻿/* ********************************************************** */
/* KeaneCommandPrototype declaration                          */        
/* ********************************************************** */
var KeaneCommandPrototype = function(spec) {
    // Private properties/methods.
    var 
        that = {},
        constants = spec.constants,
        cacheManager = spec.cacheManager,
        callbackManager = spec.callbackManager,
        postbackManager = spec.postbackManager,
        popupManager = spec.popupManager,
        info = spec.info,
        state = true;

    // Sets the command data.
    var setData = function(commandData) {
        that.data = commandData;
    };

    // Refreshes the state of the UI.
    var refreshUI = function() {
        // This is command specific until we develop a generic means of toggling ui state.
    };

    // Sets the command state.
    var setState = function(newState) {
        state = newState;
        that.state = newState;
        that.refreshUI();
    };

    // Gets a validation message from the cache.
    var getValidationMessage = function(validationCode) {
        var result;
        if (info.messages.validation &&
            info.messages.validation.length > 0) {
            if (!validationCode) {
                validationCode = 0;
            }
            result = info.messages.validation[validationCode];
        }
        return result;
    }

    // Displays a success message to the user.
    var displaySuccessMessage = function() {
        popupManager.displayMessage(constants.MESSAGE_TYPE_INFORMATION, info.messages.success, info.messages.title);
    }

    // Displays a progress message to the user.
    var displayProgressMessage = function() {
        popupManager.displayMessage(constants.MESSAGE_TYPE_PROGRESS, info.messages.progress, info.messages.title);
    }

    // Displays a validation message to the user.
    var displayValidationMessage = function(validationCode) {
        var validationMessage = getValidationMessage(validationCode);
        popupManager.displayMessage(constants.MESSAGE_TYPE_VALIDATION, validationMessage, info.messages.title);
    }

    // Displays an error message to the user.
    var displayErrorMessage = function() {
        popupManager.displayMessage(constants.MESSAGE_TYPE_ERROR, info.messages.error, info.messages.title);
    }

    // Displays a failure message to the user.
    var displayFailureMessage = function() {
        popupManager.displayMessage(constants.MESSAGE_TYPE_FAILURE, info.messages.error, info.messages.title);
    }

    // Displays a confirmation message to the user.
    var displayConfirmationMessage = function() {
        popupManager.displayMessage(constants.MESSAGE_TYPE_CONFIRMATION, info.messages.confirmation, info.messages.title);
    }

    // Performs the relevant redirect.
    var processRedirect = function() {
        if (info.redirectInfo && info.redirectInfo.url) {
            if (info.redirectInfo.toNewWindow === true) {
                KeaneUtils.openUrl(info.redirectInfo.url);
            }
            else {
                KeaneUtils.redirectToUrl(info.redirectInfo.url);
            }
        }
    }

    // Default validate invoke handler.
    var validateInvoke = function() {
        return -1;
    };

    // Default validate invoke handler.
    var onValidationError = function(validationErrorCode) {
        displayValidationMessage(validationErrorCode);
    };

    // Determines whether a confirmation message must be displayed to the user or not.
    var requiresConfirmation = function() {
        if (info.messages && info.messages.confirmation)
            return true;
        else
            return false;
    };

    // Default pre-invocation handler.
    var onInvoking = function() {
    };

    // Default invocation handler.
    var onInvoke = function() {
        if (info.invokeMode === constants.INVOKE_MODE_SERVER_CALLBACK) {
            if (!info.messages.silentMode) {
                displayProgressMessage();
            }
            callbackManager.executeCommandCallback(that);
        }
        else if (info.invokeMode === constants.INVOKE_MODE_SERVER_POSTBACK) {
            postbackManager.executeCommandPostback(that);
        }
        else if (info.invokeMode === constants.INVOKE_MODE_CLIENT_JSHANDLER) {
            processRedirect();
        }
    };

    // Default post-invocation handler.
    var onInvoked = function() {
        displaySuccessMessage();
    };

    // Default failure post-invocation handler.
    var onInvokedFailure = function() {
        displayFailureMessage();
    };

    // Public properties/methods.
    that.info = info;
    that.name = info.name;
    that.state = state;
    that.invokeMode = info.invokeMode;
    that.requiresConfirmation = requiresConfirmation;
    that.setData = setData;
    that.setState = setState;
    that.refreshUI = refreshUI;
    that.validateInvoke = validateInvoke;
    that.onValidationError = onValidationError;
    that.onInvoking = onInvoking;
    that.onInvoke = onInvoke;
    that.onInvoked = onInvoked;
    that.onInvokedFailure = onInvokedFailure;
    that.displaySuccessMessage = displaySuccessMessage;
    that.displayFailureMessage = displayFailureMessage;
    that.displayErrorMessage = displayErrorMessage;
    that.displayValidationMessage = displayValidationMessage;
    that.displayProgressMessage = displayProgressMessage;
    that.displayConfirmationMessage = displayConfirmationMessage;
    that.processRedirect = processRedirect;

    // Return constructed object.
    return that;
};