
/**
 * LoginRedirectModule
 * @author Jae Cho
 *
 * This module bridges the dojo login mechanism and the new jquery framework
 *
 * When a user logs in or logs out, two things can happen.
 *
 * 1. The page can be redirected (often to itself). One common usage is on a
 * page where change in login state requires a lot of changes in modules.
 * Instead of updating all modules, refreshing the page is easier.
 * You can also set path to redirect when login state changes.
 *
 * 2. The page may remain the same and the modules can refresh themselves.
 * This may be more involved, but it has better user experience
 *
 * @dependencies dojo stuffs
 */
function LoginRedirectModule()
{
    /** name of this module */
    this._name = LoginRedirectModule.NAME;

    this._logoutPath = window.location.pathname  + window.location.search;

    this._loginPath = window.location.pathname  + window.location.search;

    this._mode = LoginRedirectModule.MODE_REFRESH_MODULES;

    /** called when page initializes */
    this.initialize = function()
    {
        dojo.require("ag.event.EventBus");
        dojo.require("ag.user.LoggedInUser");

        this._status = ag.user.LoggedInUser.isUserLoggedIn();

        this._subscriptions = [
            ag.event.EventBus.APPLICATION.LOGIN.subscribe(this, "onUserLoggedIn"),
            ag.event.EventBus.APPLICATION.LOGOUT.subscribe(this, "onUserLoggedOut")
        ];
    };

    /** set the mode */
    this.setMode = function(mode) {
        this._mode = mode;
    };

    /** set path to redirect when loging out */
    this.setLogoutPath = function(path) {
        this._logoutPath = path;
    };

    /** set path to redirect when logging in */
    this.setLoginPath = function(path) {
        this._loginPath = path;
    };

    this.onUserLoggedIn = function()
    {
        if (this._status == ag.user.LoggedInUser.isUserLoggedIn()) return;
        else this._status = true;

        switch (this._mode) {
        case LoginRedirectModule.MODE_REFRESH_MODULES:
            this._page.updateForLoginChange();
            break;

        case LoginRedirectModule.MODE_REDIRECT:
            if (this._loginPath) {
                window.location = "http://" + window.location.hostname + this._loginPath;
            }
            break;
        }
    };

    this.onUserLoggedOut = function()
    {
        if (this._status == ag.user.LoggedInUser.isUserLoggedIn()) return;
        else this._status = false;

        switch (this._mode) {
        case LoginRedirectModule.MODE_REFRESH_MODULES:
            this._page.updateForLoginChange();
            break;

        case LoginRedirectModule.MODE_REDIRECT:
            if (this._logoutPath) {
                window.location = "http://" + window.location.hostname + this._logoutPath;
            }
            break;
        }
    };
}

LoginRedirectModule.NAME = "LoginRedirectModule";

LoginRedirectModule.prototype = new Module();

LoginRedirectModule.MODE_REDIRECT = "redirect";

LoginRedirectModule.MODE_REFRESH_MODULES = "refreshModules";

