/**
 * @class PlantreePanel
 * @extends Ext.tree.TreePanel
 *
 * @author ssmeman
 * @version 1.0
 */

/**
 * @constructor
 * @param {Object} config Configuration options
 */
Roo.gui.PlantreePanel = Ext.extend(Ext.tree.TreePanel, {
	//soft config (can be changed from outside)
	id: 'plantreePanel'

	,constructor: function(config) {
		// constructor pre-processing - configure listeners here
		config = config || {};
		config.listeners = config.listeners || {};

		Ext.applyIf(config.listeners, {
			click: function(node, e) {
				var pathElements = node.id.split('/'),
					plan = pathElements[0];

				Roo.parentVVPPath = pathElements.slice(0,-1).join('/');

				Ext.getCmp('largemapPanel').renderVVPath(node.id);

				if(node.attributes.cls !== 'kaart' && node.attributes.cls !== 'plan') {
					Ext.getCmp('infoPanel').fireEvent('receivedTreenodeDetails', {
						featureId: node.attributes.part
						,featureType: node.attributes.type
						,planId: plan
					});
				}
			}
			,beforeexpandnode: function(node) {
				for(var n=0; n <node.childNodes.length; n++) {
					var child = node.childNodes[n];
					var newId = child.getPath('part').split('/').slice(1).join('/');

					child.setId(newId);
				}
			}
			,beforedestroy: function(panel) {
				this.fireEvent('closedPlantreeWindow');
			}
		});
		Roo.gui.PlantreePanel.superclass.constructor.apply(this, arguments);
	}

	,initComponent: function() {
		this.tabChanged = false;

		this.addEvents(
            /**
             * @event checkedGebruiksmogelijkheid
             * Fires when a checkBox on this panel is checked.
             * @param {Object} with config of checkbox
             */
			'clickedTreenode'
			,'closedPlantreeWindow'
		);

		// hard coded (cannot be changed from outside)
		var config = {
		    rootVisible: true
		    ,autoScroll: true
		    ,singleExpand: true
		    ,lines: false
		    ,loader: new Ext.ux.DWRTreeLoader({
				dwrCall:SearchService.getPlanTree
				,createNode: function(attr) {
		      		attr.part = attr.id;
		      		return Ext.ux.DWRTreeLoader.superclass.createNode.call(this, attr);
		      	}
		      	,uiProviders: {
		      		'roo': Roo.gui.PlanTreeNodeUI
		      	}
		    })
		    ,root: new Ext.tree.AsyncTreeNode({
				text: this.planName
				,hasChildren:true
				,expanded: true
				,cls: 'plan'
				,id: this.planIdn
				,part: this.planIdn
		    })
		};

		// apply config
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);

		// call parent
		Roo.gui.PlantreePanel.superclass.initComponent.apply(this, arguments);

	} // e/o function initComponent
});	// e/o extend

Roo.gui.PlantreeWindow = Ext.extend(Ext.Window, {
	titleText: messages.plantreePanel.title()
	,id: 'plantreeWindow'

	,constructor: function(config) {
		this.plan = config.plan;

		// constructor pre-processing - configure listeners here
		config = config || {};
		config.listeners = config.listeners || {};

		Ext.applyIf(config.listeners, {
			beforedestroy: function(window) {
				eventHub.un('selectedPlan', this.updateTreeNode, this);
				eventHub.un('changedFilter', this.clearTree, this);
			}
		});

		// call parent constructor
		Roo.gui.PlantreeWindow.superclass.constructor.apply(this, arguments);
	}


	,initComponent: function() {
		this.addEvents(
			'closedPlantreeWindow'
		);

		var config = {
			title: this.titleText
			,height: 450
			,width: 300
			,x: 30
			,y: 220
			,layout: 'fit'
			,items: [{
				xtype: 'plantreePanel'
				,border: false
				,header: false
				,planIdn: this.plan.identificatie
				,planName: this.plan.naam
			}]
		};

		// apply config
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);

		// call parent
		Roo.gui.PlantreeWindow.superclass.initComponent.apply(this, arguments);

		this.treePanel = this.items.itemAt(0);

		eventHub.relayEvents(this.treePanel, ['closedPlantreeWindow']);

		eventHub.on('selectedPlan', this.updateTreeNode, this);
		eventHub.on('changedFilter', this.clearTree, this);
	}

	,updateTreeNode: function (plan) {
		console.log('plantreeWindow.onSelectedPlan');
		console.log(plan);
		this.treePanel.setRootNode( new Ext.tree.AsyncTreeNode({
		      text: plan.naam
		      ,hasChildren:true
		      ,expanded: true
		      ,id: plan.identificatie
		      ,part: plan.identificatie
		      ,cls: 'plan'
		    }
		));
	}
	,clearTree: function(parameters) {
		console.log('plantreeWindow.onChangedFilter');
		if( parameters.filter == "JURIDISCH" || parameters.filter == "ACTUEEL" ) {
			this.close();
		}
		else {
			this.treePanel.setRootNode( new Ext.tree.TreeNode({id:'emptyNode', leaf:true}) );
		}
	}
});

// register xtypes
Ext.reg('plantreePanel', Roo.gui.PlantreePanel);
Ext.reg('plantreeWindow', Roo.gui.PlantreeWindow);