﻿/* ********************************************************** */
/* KeanePopupManagerPrototype declaration                     */        
/* ********************************************************** */
var KeanePopupManagerPrototype = function(spec) {
    // Private properties/methods.
    var     
        that = {},
        constants = spec.constants,
        cacheManager = spec.cacheManager;
        
    // Renders the popup prior to display.
    var display = function(popup, popupLabel, message, title) {
        // Assign header.
        var header = GuiController.clientName;
        if (title) {
            header += ' - ';
            header += title;
        }
        popup.SetHeaderText(header);
        
        // Assign message.
        if (message) {
            popupLabel.SetText(message);
        }
        
        // Show popup.
        popup.Show();
    };        

    // Displays an information message to the user.
    var displayInformationMessage = function(message, title) {
        display(_informationPopup, _informationPopupLabel, message, title);
    };
    
    // Displays the confirmation popup to the user.
    var displayConfirmationMessage = function(message, title) {
        display(_confirmationPopup, _confirmationPopupLabel, message, title);
    };
        
    // Displays a progress message to the user.
    var displayProgressMessage = function(message, title) {
        var suffix;
        if (!message) {
            message = GuiController.defaultMessageInfo.progress;
        }
        suffix = ' ';
        suffix += GuiController.defaultMessageInfo.progressSuffix;
        display(_progressPopup, _progressPopupLabel, message + suffix, title);        
    };
    
    // Displays a redirect message to the user.
    var displayRedirectMessage = function(message, title) {
        var suffix;
        if (!message) {
            message = GuiController.defaultMessageInfo.redirect;
        }
        suffix = ' ';
        suffix += GuiController.defaultMessageInfo.redirectSuffix;
        display(_progressPopup, _progressPopupLabel, message + suffix, title);        
    };

    // Displays a validation message to the user.
    var displayValidationMessage = function(message, title) {
        display(_errorPopup, _errorPopupLabel, message, title);
    };
    
    // Displays a failure message to the user.
    var displayFailureMessage = function(message, title) {
        display(_errorPopup, _errorPopupLabel, message, title);
    };

    // Displays an error message to the user.
    var displayErrorMessage = function(message, title) {
        var suffix;
        if (!message) {
            message = GuiController.defaultMessageInfo.error;
        }
        suffix = ' ';
        suffix += GuiController.defaultMessageInfo.errorSuffix;
        display(_errorPopup, _errorPopupLabel, message + suffix, title);
    };
    
    // Gets the default message to be displayed by type.
    var getDefaultMessage = function(type) {
        if (type === constants.MESSAGE_TYPE_PROGRESS) {
            return cacheManager.defaultMessageInfo.progress;
        }
        else if (type === constants.MESSAGE_TYPE_VALIDATION) {

        }
        else if (type === constants.MESSAGE_TYPE_ERROR) {
            return cacheManager.defaultMessageInfo.error;
        }
    }
    
    // Displays a message to the user.
    var displayMessage = function(type, message, title) {
        if (!message) {
            message = getDefaultMessage(type);
        }    
        if (message) {
            if (type === constants.MESSAGE_TYPE_INFORMATION) {
                displayInformationMessage(message, title);
            }
            else if (type === constants.MESSAGE_TYPE_CONFIRMATION) {
                displayConfirmationMessage(message, title);
            }
            else if (type === constants.MESSAGE_TYPE_PROGRESS) {
                displayProgressMessage(message, title);
            }
            else if (type === constants.MESSAGE_TYPE_REDIRECT) {
                displayRedirectMessage(message, title);
            }
            else if (type === constants.MESSAGE_TYPE_VALIDATION) {
                displayValidationMessage(message, title);
            }
            else if (type === constants.MESSAGE_TYPE_FAILURE) {
                displayFailureMessage(message, title);
            }
            else if (type === constants.MESSAGE_TYPE_ERROR) {
                displayErrorMessage(message, title);
            }
        }
    };

    // Displays an error message collection to the user.
    var displayMessageCollection = function(type, messageCollection, title) {
        if (messageCollection) {
            if (messageCollection.length === 1) {
                displayMessage(type, messageCollection[0].text, messageCollection[0].title);
            }
            else
            {
                alert('Implement displaying a message collection.');
            }
        }
    };
    
    // Hides a message from the user.
    var hideMessage = function(type) {
        if (type === constants.MESSAGE_TYPE_INFORMATION) {
            _informationPopup.Hide();
        }
        else if (type === constants.MESSAGE_TYPE_CONFIRMATION) {
            _confirmationPopup.Hide();
        }
        else if (type === constants.MESSAGE_TYPE_PROGRESS) {
            _progressPopup.Hide();
        }
        else if (type === constants.MESSAGE_TYPE_REDIRECT) {
            _redirectPopup.Hide();
        }
        else if (type === constants.MESSAGE_TYPE_VALIDATION) {
            _errorPopup.Hide();
        }
        else if (type === constants.MESSAGE_TYPE_ERROR) {
            _errorPopup.Hide();
        }
    };
    
    // Performs page load tasks.
    var onPageLoad = function() {
        if (cacheManager && 
            cacheManager.pageLoadMessage) {
            messageInfo = cacheManager.pageLoadMessage;
            displayMessage(messageInfo.type, messageInfo.text, messageInfo.title);            
        }
    };   
    
    // Public properties/methods.
    that.onPageLoad = onPageLoad;
    that.displayMessage = displayMessage;
    that.displayMessageCollection = displayMessageCollection;
    that.hideMessage = hideMessage;
    
    // Return constructed object.
    return that;
}