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

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

app.state.AuthLogout = function()
{
    var state = app.core.StateManager.createState( 'auth-logout' );

    state.onEnter = function( p )
    {
        app.gui.PageLoader.show();

        var redirect = app.core.Controller.getGetParam( "redirect" );
        if ( redirect )
        {
            redirect = '?redirect=' + redirect;
        }
        else
        {
            redirect = '';
        }

        // Notify server about logout
        // Note
        // This needs to be done before destroying locally the session because we need the session
        // token on client side
        app.core.Rpc.call(
            'Auth',
            'logout',
            null,
            function( res )
            {
            },
            function()
            {
            }
        );

        // Stop notification
        app.core.NotificationManager.destroy();

        // Destroy session
        app.core.Session.destroy();

        // Do not receive any push notifications once logged out
        app.util.OneSignal.destroy();

        // Hide loading screen
        app.gui.PageLoader.hide();

        // Redirect to start
        app.core.Controller.redirect( '#/auth/login' + redirect );
    };

    return state;
};