/**
 * @class Zw.Feature
 * This class displays an infobar at the bottom of that app.
 * It will not show if the user is a subscriber or is signed up
 * to what the info bar is selling by listening to  /security/discover
 */
Zw.namespace('Zw.sprint.features.infobar');
Zw.sprint.features.infobar.TexterPlusInfoBarFeature = Ext.extend(Zw.Feature, {

    created : false,
    desktopInfoBar : null,
    onInit : function () {

        this.context.subscribe('/account/user/data', this.onAuthenticated, this);

    },

    onAuthenticated : function(args) {

        if (this.context.Account.isGuest()) {
            return;
        }

        this._showInfoBar();
    },

    adjustTaskbar : function() {
        var taskbar = Ext.fly('taskbar');

        if (!Ext.isEmpty(taskbar)) {

            taskbar.addClass('infobar-adjust');
        }
        var logo = Ext.fly('logo');
        if (!Ext.isEmpty(logo)) {

            logo.addClass('infobar-adjust');
        }
        var socket = Ext.fly('zw-socket-indicator');
        if (!Ext.isEmpty(socket)) {

            socket.addClass('infobar-adjust');
        }

    }   ,
    _showInfoBar : function() {

        Ext.fly('infobar').update('', false);
        this.adjustTaskbar.defer(500, this);

        if (!this.created)
            this.toggleDesktopInfoBar(this);
        //Zw.Application.subscribe('/security/discover', this.toggleDesktopInfoBar, this);
        this.setSettinglet({
            xtype: 'Zw.features.settings.SendFromMobileSettinglet',
            // if the xtype does not resolve, inspect this package
            pkg: 'webtexterplus.settings',
            // if we need packaging, trigger the download from this tab
            tab : 'Texter Plus'
        });
        this.setSettinglet({
            xtype: 'Zw.features.settings.TexterPlusAccountDetailsSettinglet',
            // if the xtype does not resolve, inspect this package
            pkg: 'webtexterplus.settings',
            // if we need packaging, trigger the download from this tab
            tab : 'Texter Plus'
        });
        this.setSettinglet({
            xtype: 'Zw.features.settings.TexterPlusAjaxSettinglet',
            // if the xtype does not resolve, inspect this package
            pkg: 'webtexterplus.settings',
            // if we need packaging, trigger the download from this tab
            tab : 'Texter Plus'
        });
    },


    toggleDesktopInfoBar : function (args) {
        /* if (args) {
         if (args.subsystem && args.subsystem == '/app/load') {
         if (!args.accessGranted) {
         if (!this.created) {*/
        this.createDesktopInfoBar();
        /* }
         }
         else {
         if (this.created) {
         this.desktopInfoBar.hide();
         }
         }
         }
         }*/
    },
    showTexterPlusRegWizard : function() {

        var context = this.context;
        if (context && !context.Account.isGuest()) {

            /**
             * We're redirecting to WebTexterPlus if you're already subscribed.
             * In order to give a snappy UI, let's show the upgrade window first (assuming not subscribed).
             */
            context.publish('/net/ajax/request', {
                session: true,
                url: '/sprint/texterplus/enrolled',
                success: function(sender, json) {
                    if (json && json.success === true && json.response === true){
                        window.location = '/sprint/texterplus/redirect';
                    }
                },

                failure: Ext.emptyFn

            });

            context.publish('/desktop/texterplus/show', {
                success: Ext.emptyFn,
                failure: Ext.emptyFn
            });

        }
        else {
            Ext.MessageBox.alert("Please log in.")
        }

    }
    ,
    createDesktopInfoBar : function () {


        if (!this.created) {

            this.desktopInfoBar = this.createInfoBar();

            this.desktopInfoBar.on('click', this.showTexterPlusRegWizard, this);
            this.created = true;
            var infoBarDiv = Ext.get('infobar');

            Zw.console('infoBarDiv', infoBarDiv);
            this.desktopInfoBar.render('infobar');
        }

        else {
            // this.advertBubble.el.alignTo(this.advertBar.el, 'bl-br', [13, 0]);

            this.desktopInfoBar.show();

        }


    },

    createInfoBar : function () {
        return new Zw.sprint.controls.TexterPlusInfoBar();
    },

    onDestroy : function () {
        Zw.Application.unsubscribe('/security/discover', this.blinkEntry, this);
        delete this.desktopInfoBar;
    }

});
Zw.features.Factory.register('Zw.sprint.features.infobar.TexterPlusInfoBarFeature');
