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

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

app.util.OneSignal = (function(){

    var OneSignal,
        cbNotificationOpened,
        cbGetIds;

    /**
     * Retrieve notification jsonData once user clicked on it
     * @param jsonData
     */
    cbNotificationOpened = function( jsonData )
    {
        console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
        if ( jsonData &&
            jsonData.notification &&
            jsonData.notification.payload &&
            jsonData.notification.payload.additionalData )
        {
            var addData = jsonData.notification.payload.additionalData;

            if ( addData && addData.hasOwnProperty( 'inAppUrl' ) )
            {
                app.core.Controller.redirect( addData.inAppUrl );
            }
        }
        app.core.NotificationManager.forceUpdate();
    };

    /**
     * Retrieve one signal player id
     * @param playerId
     */
    cbGetIds = function( ids )
    {

        app.core.Rpc.call(
            'Account',
            'registerDevice',
            {
                platform : 'ANDROID',
                reg_id : ids.userId
            },
            function()
            {
                if (window.navigator.userAgent !== "ProBuddy/2.0 (Android)") {
                    OneSignal.setSubscription( true );
                }
            }
            ,
            function( err )
            {
                //console.log( "OneSignal Error", err );
            }
        );
    };

    return {
        init : function()
        {
            if ( !window.plugins || !window.plugins.OneSignal ) return;

            OneSignal = window.plugins.OneSignal;

            var appId = app.core.App.getConfig( 'onesignal.appId' );

            if ( appId && app.util.Environment.isNativeMobile() && app.core.Session.isLoggedIn() )
            {
                try
                {
                    OneSignal.setLocationShared( false );
                }
                catch( e )
                {
                    // fail silently
                }

                if (window.navigator.userAgent === "ProBuddy/2.0 (Android)") {
                    // New cordova app (version 3)
                    // Uncomment to set OneSignal device logging to VERBOSE
                    //OneSignal.setLogLevel(6, 0);

                    // NOTE: Update the setAppId value below with your OneSignal AppId.
                    OneSignal.setAppId(appId);
                    OneSignal.setNotificationOpenedHandler(cbNotificationOpened);

                    //Prompts the user for notification permissions.
                    //    * Since this shows a generic native prompt, we recommend instead using an In-App Message to prompt for notification permission (See step 6) to better communicate to your users what notifications they will get.
                    // OneSignal.promptForPushNotificationsWithUserResponse(function(accepted) {
                    //     console.log("User accepted notifications: " + accepted);
                    // });

                } else {
                    // Old cordova app (version 2)
                    OneSignal
                        .startInit( appId )
                        .handleNotificationOpened( cbNotificationOpened )
                        .endInit();

                    OneSignal.getIds( cbGetIds );
                }
            }
        }
        ,
        destroy : function()
        {
            if ( !window.plugins || !window.plugins.OneSignal ) return;

            if (window.navigator.userAgent !== "ProBuddy/2.0 (Android)") {
                OneSignal.setSubscription( false );
            }

        }
    }

})();