Zw.namespace("Zw.sprint.features.registration");
Zw.sprint.features.registration.TexterPlusSignupFeature = Ext.extend(Zw.Feature, {
    /**
     * @public
     * @field
     * @description The packages that this feature needs
     * @type {String/Array}
     */
    packages : ['wizard.all', 'webtexterplus.wizard'], //this needs to have packages for the new web texter plus signupprocess.

    callBackScope : this,

    onInit : function() {

        //if the context wants to register, let them.
        this.context.subscribe('/desktop/texterplus/show', this.onRegistrationRequest, this);
        this.context.subscribe('/desktop/texterplustos/show', this.showTOSWindow, this);
        this.context.subscribe('/desktop/texterplusvideo/show', this.showVideoWindow, this);
    },

    onRegistrationRequest : function(featureArgs) {
         if(this.context.Account.isGuest(true)){
            alert('Please log in.')
            return;
        }
        var onPackageReadyWrapper = function() {
            this.onPackagesReady(featureArgs);
        };
        Zw.Packages.get(this.packages, onPackageReadyWrapper, this);
    },

    onPackagesReady : function(featureArgs) {
        if (!this.w) {
            this.w = this.context.Desktop.registerWindow(this.createWindow(featureArgs));
            this.w.on('destroy', this.onWindowDestroy, this);
        }
        this.w.show();
    },

    onWindowDestroy : function() {
        delete this.w;
    },
    showTOSWindow : function() {
        var texterPlusTOSWindow = new Zw.sprint.features.registration.TexterPlusTermsOfServiceWindow({
            context: this.context,
            scope:this

        });
        texterPlusTOSWindow.show();
    },
    showVideoWindow : function() {
        var texterPlusVideoWindow = new Zw.sprint.features.registration.TexterPlusVideoWindow({
            context: this.context,
            scope:this

        });
        texterPlusVideoWindow.show();
    },


    createWindow : function(featureArgs) {
        var successFunc = this.registrationSuccess;
        var failureFunc = this.registrationFailure;
        var callBackScope = this;

        var wizardControllerPlugin = this.createWizardController();
        var texterPlusSignupWindow = new Zw.sprint.features.registration.TexterPlusSignupWindow({
            modal:true,
            context: this.context,
            scope:callBackScope,
            plugins : [
                wizardControllerPlugin
            ]
        }, this);

        var windowCloseFunctionWrapper = function() {
            this.onBeforeClose(featureArgs);
        }
        texterPlusSignupWindow.on('beforeclose', windowCloseFunctionWrapper, this);
        wizardControllerPlugin.finishActions.add({

            //fn: featureArgs.success.createInterceptor(function(result) {
            fn: function() {
                if (this.finished && featureArgs && featureArgs.success) {
                    featureArgs.success.call(featureArgs.scope, {
                        success:true
                    });
                }
            },
            scope: callBackScope
        });
        return texterPlusSignupWindow;
    },
    onBeforeClose : function(featureArgs) {
        //Not called.
        //Need to jump to the window closer event and call this function.
        if (!this.finished && featureArgs && featureArgs.failure) {
            var scope = this;
            if (featureArgs.scope) {
                scope = featureArgs.scope;
            }
            featureArgs.failure.call(scope, {
                success:false
            });
        }
    },

    registrationSuccess : function() {
        Zw.console("RegSuccess, the user finished the registration process and clicked finish, or the final next button.");
    },

    registrationFailure : function() {
        Zw.console("RegFailure, the window was closed before finishing registration.");
    },
    createWizardController : function() {
        //Zw.console("TexterPlusSignupWizardControllerPlugin start");
        return new Zw.sprint.features.registration.TexterPlusSignupWizardControllerPlugin({
            context: this.context
        });

    },

    subscribeSuccess : function(response) {
        Zw.console("subscribeSuccess, the server has been updated");
        Zw.Application.publish('/security/discover', {
            accessGranted:true
        });
    },

    subscribeFail : function(response) {
        Zw.console("subscribeFail, the server has not been updated");
        Zw.Application.publish('/security/discover', {
            accessGranted:false
        });
    },

    onAuthenticatedAsUser : function() {

    },
    onAuthenticatedAsGuest : function() {
        this.onDestroy();
    },


    onDestroy : function() {
        // stop listening to this event
        this.context.unsubscribe('/desktop/texterplus/show', this.onRegistrationRequest, this);
        this.context.unsubscribe('/desktop/texterplustos/show', this.showTOSWindow, this);
        this.context.unsubscribe('/desktop/textervideotos/show', this.showTOSWindow, this);

    }

});
Zw.features.Factory.register('Zw.sprint.features.registration.TexterPlusSignupFeature');

