const App = {
    create : function()
    {
        this.user = this.createInstance( 'User' );
        this.rpc = this.createInstance( 'Rpc', { url : 'api.php' } );
        this.UI = this.createInstance( 'UI' );

        this.rpc.call(
            'Auth',
            'getSessionManager',
            {
                sessionId : this.user.sessionId
            },
            function( res )
            {
                let data = ( res && res.hasOwnProperty( 'data' ) ) ? res.data : {};

                if ( data.sessionToken && data.userProfile )
                {
                    this.user.init( data.sessionToken, data.userProfile, data.teams, data.zContracts );
                    this.UI.onUserUpdate();
                    if ( window.location.hash.length === 0 )
                    {
                        this.redirect( 'dashboard/' + this.user.activeGroupId );
                    }
                }
                else
                {
                    this.user.deinit();
                    this.UI.onUserUpdate();
                    this.redirect( 'auth/login' );
                }

                this.controller.enable();

            }.bind( this )
        );

        return false;
    },

    openInApp : function( url )
    {
        window.open( location.origin.replace( /manager/gi, 'app' ) + url, 'probuddy' );
    }
};

export { App };