/**
 * (c) by aheadware.com
 */

var app = app || {};
app.core = app.core || {};

app.core.StateManager = (function()
{
    var currentState = null,
        prevStateId = null,
        isPreviousState = false,
        StateBase;

    var fnSaveStateSettings,
        fnExitCurrent,
        fnEnterNew;

    // State model
    StateBase = function( stateId, stateSettings )
    {
        this._stateId = stateId || null;
        this._stateSettings = stateSettings || null;
    };
    StateBase.prototype.onEnter = function( p ) { return true; };
    StateBase.prototype.onExit = function( p ) { return true; };
    StateBase.prototype.getStateId = function() { return this._stateId; };
    StateBase.prototype.getStateSettings = function() { return this._stateSettings; };
    StateBase.prototype.getStateSetting = function( key )
    {
        var settings = this.getStateSettings();
        if ( settings && settings.hasOwnProperty( key ) )
        {
            return settings [ key ];
        }
        else
        {
            return null;
        }
    };
    StateBase.prototype.setStateSetting = function( key, value )
    {
        if ( !this._stateSettings )
        {
            this._stateSettings = {};
        }
        this._stateSettings[ key ] = value;
    };

    fnSaveStateSettings = function()
    {
        if ( currentState && currentState.hasOwnProperty( '_stateSettings' ) )
        {
            // store settings
            app.core.Cache.set( 'state-setting-' + currentState.getStateId(), currentState.getStateSettings() );
        }
    };

    /**
     *
     * @param params
     * @returns {boolean}
     */
    fnExitCurrent = function( params )
    {
        if ( currentState && currentState.onExit )
        {
            if ( false === currentState.onExit( params ) )
            {
                return false;
            }
            fnSaveStateSettings();
            prevStateId = currentState.getStateId();
            app.core.EventManager.trigger( 'STATEMANAGER_ON_STATE_EXIT', [ currentState.getStateId() ] );
        }

        currentState = null;

        return true;
    };

    /**
     *
     * @param state
     * @param params
     */
    fnEnterNew = function( state, params )
    {
        currentState = state;

        currentState && currentState.onEnter && currentState.onEnter( params );

        if ( currentState )
        {
            app.core.EventManager.trigger( 'STATEMANAGER_ON_STATE_ENTER', [ currentState.getStateId() ] );
        }
    };

    return {
        createState : function( stateId, defaultSettings )
        {
            var stateSettings = defaultSettings || null,
                savedSettings = null;

            if ( !stateId ) throw new Error( 'CreateState called without stateId.' );

            // If we have previously stored settings
            savedSettings = app.core.Cache.get( 'state-setting-' + stateId );
            if ( savedSettings )
            {
                // use them
                stateSettings = savedSettings;
            }

            return new StateBase( stateId, stateSettings );
        },
        getCurrentState : function()
        {
            return currentState;
        },
        getPreviousStateId : function()
        {
            return prevStateId;
        },
        saveStateSettings : function()
        {
            fnSaveStateSettings();
        },
        isPreviousState : function()
        {
            return isPreviousState;
        },
        switchTo : function( state, params )
        {
            isPreviousState = this.getPreviousStateId() === state.getStateId();

            if ( fnExitCurrent() )
            {
                fnEnterNew( state, params );
            }
        }
    };

})();