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

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

app.core.App = (function(){

    var fnInit = null,
        configData = null;

    fnInitAfterSession = function()
    {
        // Belows modules can rely on Session variables
        app.util.Locale.init( app.core.Dict );
        app.util.Locale.expose( '_lc' );
        moment.locale( 'de' );
        app.core.View.init();
        app.core.Controller.init();

        if ( true === window.isNativeMobile )
        {
            // Open external link in native browser
            $(document).on( "click", 'a[target="_blank"]', function(e) {
                var url = $(this).attr('href');
                window.open(url, '_system');
                e.preventDefault();
            });
        }

    };

    fnInit = function( cfg )
    {
        // Establish server communication and base
        app.core.Cache.init();
        app.core.Session.init();
        app.core.Rpc.init( cfg.rpc );
        app.core.EventManager.init();

        // Retrieve session
        app.core.Rpc.call(
            'Auth',
            'getSession',
            {
                includeConfig : true
            },
            function( res )
            {
                configData = res.config;
                app.core.Session.init( res );
                fnInitAfterSession();
            },
            function( res, errorCode )
            {
                fnInitAfterSession();
            }
        );
    };

    return {
        getConfig : function( key )
        {
            if ( key )
            {
                if ( configData && configData.hasOwnProperty( key ) )
                {
                    return configData[ key ];
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return configData;
            }
        }
        ,
        setConfig : function( key, value )
        {
            if ( key )
            {
                if (configData && configData.hasOwnProperty(key))
                {
                    configData[key] = value;
                }
            }
        },
        getCountries : function()
        {
            return [
                { key : 'de', name : 'Deutschland' },
                { key : 'at', name : 'Österreich' },
                { key : 'ch', name : 'Schweiz' },
                { key : 'it', name : 'Italien' },
                { key : 'dk', name : 'Dänemark' },
                { key : 'be', name : 'Belgien' },
                { key : 'fr', name : 'Frankreich' },
                { key : 'lu', name : 'Luxemburg' },
                { key : 'nl', name : 'Niederlande' },
                { key : 'pl', name : 'Polen' }
            ];
        },
        isLiveVersion : function()
        {
            var isLive = false;

            if ( "LIVE" === this.getConfig( 'environment' ) )
            {
                isLive = true;
            }

            return isLive;
        }
        ,
        getVersion : function()
        {
            var v = this.getConfig( 'version' );

            if ( !v )
            {
                v = 'n/a'
            }
            else
            {
                v = parseInt( v );
                v = v / 100;
            }

            return '' + v;
        }
        ,
        run : function( cfg )
        {
            fnInit( cfg );

            console.log( "App is up and running..." );
        }
    };
})();