DNow.WizardViewer = Class.create(aw.ui.Panel, {

    _wizardTree: null,
    _wizardInterface: null,
    _wizardQueue: null,

    _onWizardsChanged_fx: null,
    _onActiveWizardChanged_fx: null,

    initialize: function (properties, events) {
        this._wizardTree = null;
        this._wizardInterface = null;
        this._wizardQueue = null;

        this._onWizardsChanged_fx = null;
        this._onActiveWizardChanged_fx = null;

        this.inheritCSS = true;

        aw.ui.Panel.prototype.initialize.apply(this, arguments);

        if (this.componentBus) {
            this.componentBus.getComponentReferences('aw.WizardManager', function (components) {
                if ((components) && (components.length > 0)) {
                    var wm = components[0];

                    this._onWizardsChanged_fx = this._onWizardsChanged.bind(this);
                    this._onActiveWizardChanged_fx = this._onActiveWizardChanged.bind(this);
                    this._onWizardJobUpdated_fx = this._onWizardJobUpdated.bind(this);
                    this._onWizardJobCreated_fx = this._onWizardJobCreated.bind(this);

                    wm.registerEvent('onwizardschanged', this._onWizardsChanged_fx);
                    wm.registerEvent('onactivewizardchanged', this._onActiveWizardChanged_fx);
                    wm.registerEvent('onwizardjobupdated', this._onWizardJobUpdated_fx);
                    wm.registerEvent('onwizardjobcreated', this._onWizardJobCreated_fx);

                    var wizards = wm.getAllWizards();
                    if ((wizards) && (wizards.length > 0))
                        this._onWizardsChanged(wizards);

                    if (wm.activeWizard) {
                        this._onActiveWizardChanged(wm.activeWizard);

                        if (wm.activeWizard['interface'])
                            this._onWizardInterfaceReceived(wm.activeWizard);
                    }
                }
            }.bind(this));
        }
    },

    draw: function () {
        aw.ui.Panel.prototype.draw.apply(this);

        this._wizardTree = new aw.ui.Tree({
            'mode': 'single'
        }, {
            'onselectionchanged': this._onWizardTreeSelectionChanged.bind(this)
        });

        this._wizardInterface = new aw.ui.WizardInterface({
            'componentBus': this.componentBus
        });

        this._wizardQueue = new aw.ui.WizardQueue({
            'componentBus': this.componentBus,
            'title': 'Queue',
            'width': 400,
            'minWidth': 350,
            'helpCallback': this._onHelpClick.bind(this, 'Wizards:Queue'),
            'maskedExportFormats': ['mif'],
            'renamedExportFormats': [
                { 'format': 'vhtml', 'label': 'VHTML (FireFox Only)' },
                { 'format': 'csv', 'label': 'CSV' },
                { 'format': 'dbf', 'label': 'DBF' }
            ]
        });

        //RALLY US856 - Add link to cluster description page for solocast wizards
        this.helpGroup = new aw.ui.Toolbar.Group({
            'label': 'Help',
            'cssClass': 'indexToolbarGroup',
            'componentBus': this.componentBus,
            'items': [
                new aw.ui.HTML({
                    html: "<a href='custom/clusters.htm' target=_blank>Cluster Descriptions</a>"
                })
            ]
        });
        //------------------------

        this.add(
            new aw.ui.Toolbar({
                'region': 'top',
                'height': 100,
                'cssClass': 'DNow_Toolbar',
                'items': [
                    new aw.ui.Toolbar.Group({
                        'label': 'Geography Selection',
                        'cssClass': 'geoselection',
                        'items': [
                            new DNow.GeographyBar({
                                'componentBus': this.componentBus
                            })
                        ],
                        'helpCallback': this._onHelpClick.bind(this, 'Geography Selection')
                    }),
                    this.helpGroup //US856
                ]
            }));

        this.add(
            new aw.ui.Panel.Splitter({
                'titleVisibility': false,
                'orientation': 'horizontal',
                'items': [
	                new aw.ui.Panel({
	                    'title': 'Available Wizards',
	                    'width': '200',
	                    'minWidth': '150',
	                    'items': [
	                        this._wizardTree
                        ],
	                    'helpCallback': this._onHelpClick.bind(this, 'Wizards:Available Wizards')
	                }),
                    this._wizardInterface,
                    this._wizardQueue
			    ]
            }));

        return this.canvas;
    },

    _onActiveWizardChanged: function (wizard) {
        if (!wizard)
            return;

        if (this._wizardTree) {
            this._wizardTree.selectNodeByKey(wizard.guid, false);
            //this._wizatdTree.expandNodeByKey( ((wizard.isPublic === true) ? 'public' : 'private' ) );
        }
    },

    _onHelpClick: function (topic) {
        if (this.componentBus) {
            var help = this.componentBus.getComponentReferences('DNow.Help');
            if ((help) && (help.length > 0)) {
                help[0].loadTopicByTitle(topic);
                return;
            }
        }

        new DNow.Help({
            'componentBus': this.componentBus,
            'activeTopicKey': topic
        });
    },

    _onWizardJobUpdated: function (job) {
        if (!job)
            return;

        if (job.disposition != 'complete')
            return;

        var wm = this.componentBus.getComponentReferences('aw.WizardManager');
        if ((wm) && (wm.length > 0))
            wm = wm[0];
        else
            return;

        //alert(wm.getJobOutputStreamURL( job, job.files[0], 'pdf' ) );
    },

    _onWizardJobCreated: function (job) {
        var wm = this.componentBus.getComponentReferences('aw.WizardManager');
        if ((wm) && (wm.length > 0)) {
            wm = wm[0];
            /*
            var jobs = wm.getAllJobs();
            var tmp = jobs[0];
            jobs[0] = jobs[jobs.length - 1];
            jobs[jobs.length - 1] = tmp;

            wm._jobRegistry = jobs;

            this._wizardQueue.drawItem(this._wizardQueue.items[0]);
            */
            //console.log(wm);
        }
        else
            return;
    },

    _onWizardsChanged: function (wizards) {
        this._wizardTree.clear();
        /*        this._wizardTree.addNodes([
        {
        'key': 'public',
        'label': 'Public',
        'selectable': false,
        'type': 'branch'
        },{
        'key': 'private',
        'label': 'Private',
        'selectable': false,
        'type': 'branch'
        }
        ]);*/

        var wizard = null;
        for (var i = 0; wizard = wizards[i]; i++) {
            this._wizardTree.addNode({
                'key': wizard.guid,
                /*'parentKey': (wizard.isPublic) ? 'public' : 'private',*/
                'label': wizard.name,
                'selectable': true
            });
        }
    },

    _onWizardTreeSelectionChanged: function (tree) {
        if ((!tree) || (!tree.getSelectedKeys))
            return;

        var key = tree.getSelectedKeys();
        if ((!key) || (key.length !== 1))
            return;

        var wm = this.componentBus.getComponentReferences('aw.WizardManager');
        if ((wm) && (wm.length > 0))
            wm[0].setActiveWizardByKey(key);
    },

    _onWizardInterfaceReceived: function (wizard) {
        if (!wizard)
            return;

        this._wizardInterface.loadInterface(wizard.guid, wizard['interface']);
    },

    CLASS_NAME: 'DNow.WizardViewer'
});
