/**
 * @class Roo.gui.LargemapPanel
 * @extends Ext.ux.FlashPanel
 *
 * The LargeMapPanel is the center of the application, showing a map in the middle
 *
 * @author ssmeman
 * @version 1.0
 */

/**
 * @constructor
 * @param {Object} config Configuration options
 */
Roo.gui.LargemapPanel = Ext.extend(Ext.ux.FlashPanel, {

	// soft config (can be changed from outside)
	region: 'center'
	,border: false
	,id: 'largemapPanel'

	,initComponent: function() {
		this.flamingo = new Roo.data.FlamingoWrapper();
		var gebruiksmogelijkhedenDelay;

		// hard coded
		var config = {
			hideMode: 'visibility'
			,autoScroll:false
			,mediaCfg: {
				id: 'mainFlamingoEmbed'
				,url: 'flamingo/flamingo/flamingo.swf?config=' + Roo.applicationBaseDir + '/config/main'
			}
		};

		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);

		// call parent
		Roo.gui.LargemapPanel.superclass.initComponent.apply(this, arguments);

		// Flamingo (mapPanel) related event handling
		eventHub.on({
			scope: this
			,'foundLocation': function(location) {
				this.clearMapFilters();

				bb = location.boundingBox;
				this.flamingo.moveToExtent(bb.minX, bb.minY, bb.maxX, bb.maxY);

				if(bb.minX == bb.maxX && bb.minY==bb.maxY) {
					this.flamingo.setLocation(bb.minX, bb.minY, location.name);
				}
				else {
					this.flamingo.removeLocation();
				}
			}
			,'selectedPlan': function(plan) {
				Roo.currentPlan = plan;
				Roo.parentVVPPath = '';

				this.setMapFilters(plan);

				bb = plan.boundingBox;
				this.flamingo.moveToExtent(bb.minX, bb.minY, bb.maxX, bb.maxY);
				this.flamingo.removeLocation();
			}
			,'selectedPlanAuthentic': function(plan) {
				this.setMapFilters(plan);
				this.flamingo.removeLocation();
			}
			,'foundPlans': function(plans) {
				Roo.currentplan = {};
				Roo.parentVVPPath = '';

				this.clearMapFilters();
				this.flamingo.removeLocation();
				this.moveExtendToSearchResult();
			}
			,'collapsedGebruiksmogelijkhedenPanel': function(layers) {
				// Show all main layers

				// Show layer in overzichtskaart
				this.flamingo.setLayersVisible('overviewFlamingoEmbed', 'overview', 'gaOverzichtLayer', true);

				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map', 'bpHoofdgroepenLayer,bpDubbelbestemmingLayer', false);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map', 'boLayer', true);

				this.setLayers();

				this.flamingo.releaseFilterButton();
			}
			,'expandedGebruiksmogelijkhedenPanel': function(layers) {
				// Show the bgLayer containing the plancontouren
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','bgLayer', true);

				// Hide all main layers
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','boLayer,vgLayer,voLayer,igLayer,ioLayer,vvvpLayer', false);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map', 'bpHoofdgroepenLayer,bpDubbelbestemmingLayer', true);

				// Hide layer in overzichtskaart
				this.flamingo.setLayersVisible('overviewFlamingoEmbed', 'overview', 'gaOverzichtLayer', false);

				this.setBestemmingsHoofdgroepen(layers);
			}
			,'checkedGebruiksmogelijkheid': function(layer) {
				if(!this.gebruiksmogelijkhedenDelay) {
					this.gebruiksmogelijkhedenDelay = new Ext.util.DelayedTask(function() {
						var layers = Ext.getCmp('gebruiksmogelijkhedenPanel').getLayers();
						this.setBestemmingsHoofdgroepen(layers);
					}, this);
				}
				this.gebruiksmogelijkhedenDelay.delay(1000);
			}
			,'receivedUrlParameters': function(parameters) {
				var bb = parameters.boundingBox;
				if (bb!=null) {
					this.flamingo.moveToExtent(bb.minX, bb.minY, bb.maxX, bb.maxY);
				}
			}
			,'highlightFeature': function(parameters) {
				Ext.applyIf(parameters, {alpha:50});

				console.log('%o', parameters);

				if( Roo.parentVVPPath !== '') {
					console.log('highlight VVP');
					this.flamingo.highlightLayerVVP(Roo.vvvpUrl+'?SERVICE=WMS', Roo.parentVVPPath + '/' + parameters.value, 80);
				} else if( parameters.fid ){
					console.log('highlight Feature');
					this.flamingo.highlightFeature(Roo.wmsUrl, Roo.filterBaseDir, 'app:' + parameters.type,'app:' + parameters.property, parameters.fid, parameters.alpha);
				} else {
					console.log('highlight Feature');
					this.flamingo.highlightFeature(Roo.wmsUrl, Roo.filterBaseDir, 'app:' + parameters.type,'app:' + parameters.property, parameters.value, parameters.alpha);
				}
			}
			,'resetHighlight': function(parameters) {
				this.flamingo.resetFeature();
			}
			, 'changedFilter': function(parameters) {
				this.setFlamingoButtons();
				this.clearMapFilters();
				this.setLayers();
				this.enableMapTips(false);
				this.flamingo.removeLocation();
			}
			,'closedPlantreeWindow': function() {
				console.log('in LargemapPanel.on(closedPlantreeWindow)');

				Roo.parentVVPPath = '';
				this.flamingo.releasePlantreeButton();
			}
		});
	}

	//~ Methods ----------------------------------------------------------------
	,setFlamingoButtons: function() {
		switch(Roo.currentTabFilter.name)
		{
			case 'JURIDISCH':
			case 'ACTUEEL':
				this.flamingo.setButtonVisible('filterButton', true);
				this.flamingo.setButtonVisible('legendaButton', true);
				this.flamingo.setButtonVisible('planTreeButton', false);
				break;
			case 'VISIE':
			case 'INSTRUCTIE':
				this.flamingo.setButtonVisible('filterButton', false);
				this.flamingo.setButtonVisible('legendaButton', false);
				this.flamingo.setButtonVisible('planTreeButton', true);
				break;
			case 'NONE':
				break;
		}
	}
	,clearMapFilters: function() {
		Roo.parentVVPPath = '';
		this.flamingo.removeFilter('plangebied', false);

		switch(Roo.currentTabFilter.name)
		{
		case 'VISIE':
			this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','vgLayer,voLayer,vvvpLayer', true);
			this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','vGrayLayer', false);
			this.flamingo.drawLayerVVP('');
			break;
		case 'INSTRUCTIE':
			this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','igLayer,ioLayer,vvvpLayer', true);
			this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','iGrayLayer', false);
			this.flamingo.drawLayerVVP('');
			break;
		case 'NONE':
			// do nothing
			break;
		}

		this.setJuridischFilter();
	}
	,setMapFilters: function(plan) {
		if(plan == null) { console.warn('incorrect call of LargemapPanel.setMapFilters'); this.clearMapFilters(); return; }

		this.flamingo.addFilter('plangebied', 'plangebied='+plan.identificatie, false);

		// filter for a specific plan, if bestemmingsplan, use planfilter, on tab VISIE or INSTRUCTIE draw using vvvp-wms
		switch(Roo.currentTabFilter.name)
		{
		case 'VISIE':
			if(Roo.isVormVrijPlan(plan)) {
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','voLayer,vGrayLayer', false);
				this.flamingo.drawLayerVVP(plan.identificatie + '/1');
			} else {
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','voLayer,vGrayLayer', true);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','vvvpLayer', false);
			}
			break;
		case 'INSTRUCTIE':
			if(Roo.isVormVrijPlan(plan)) {
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','ioLayer,iGrayLayer', false);
				this.flamingo.drawLayerVVP(plan.identificatie + '/1');
			} else {
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','ioLayer,iGrayLayer', true);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','vvvpLayer', false);
			}
			break;
		case 'NONE':
			// do nothing
			break;
		}

		this.setJuridischFilter();
	}

	,setJuridischFilter: function() {
		// juridischbindend filter
		if(Roo.currentTabFilter.name == 'JURIDISCH') {
			this.flamingo.addFilter('juridisch-bindend', 'juridisch-bindend=true', true);
		} else {
			this.flamingo.removeFilter('juridisch-bindend', true);
		}
	}

	,setLayers: function() {
		/**
			- Laag "bgLayer" bevat alle lagen van plangebieden voor de tabs
			"juridisch bindend" en "juridisch bindend + ontwerp"

			- Laag "boLayer" bevat alle lagen (plangebieden & planobjecten) voor de
			tabs "juridisch bindend" en "juridisch bindend + ontwerp"

			- Laag "igLayer" bevat alle lagen van plangebieden voor de tab
			"instructies"

			- Laag "ioLayer" bevat alle lagen (plangebieden & planobjecten) voor de
			tab "instructies"

			- Laag "vgLayer" bevat alle lagen van plangebieden voor de tab "visies"

			- Laag "voLayer" bevat alle lagen (plangebieden & planobjecten) voor de
			tab "visies"
		 */
		console.debug('LargeMapPanel.setLayers(): %s', Roo.currentTabFilter.name);

		this.flamingo.setAlpha("bpHoofdgroepenLayer,bgLayer,boLayer,vgLayer,voLayer,igLayer,ioLayer,vvvpLayer,vGrayLayer,iGrayLayer");

		switch(Roo.currentTabFilter.name)
		{
			case 'JURIDISCH':
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','bgLayer,boLayer,bgLayerStaticJuridisch1,bgLayerStaticJuridisch2', true);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','vgLayer,voLayer,igLayer,ioLayer,vvvpLayer,vGrayLayer,iGrayLayer,bgLayerStatic1,bgLayerStatic2', false);
				this.flamingo.setLayersVisible('overviewFlamingoEmbed', 'overview', 'gaOverzichtLayer', true);
				break;
			case 'ACTUEEL':
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','bgLayer,boLayer,bgLayerStatic1,bgLayerStatic2', true);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','vgLayer,voLayer,igLayer,ioLayer,vvvpLayer,vGrayLayer,iGrayLayer,bgLayerStaticJuridisch1,bgLayerStaticJuridisch2', false);
				this.flamingo.setLayersVisible('overviewFlamingoEmbed', 'overview', 'gaOverzichtLayer', true);
				break;
			case 'VISIE':
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','bgLayer,boLayer,igLayer,ioLayer,vGrayLayer,iGrayLayer,bgLayerStatic1,bgLayerStatic2,bgLayerStaticJuridisch1,bgLayerStaticJuridisch2', false);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','vgLayer,voLayer,vvvpLayer', true);
				this.flamingo.setLayersVisible('overviewFlamingoEmbed', 'overview', 'gaOverzichtLayer', false);
				this.flamingo.drawLayerVVP('');
				break;
			case 'INSTRUCTIE':
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','bgLayer,boLayer,vgLayer,voLayer,vGrayLayer,iGrayLayer,bgLayerStatic1,bgLayerStatic2,bgLayerStaticJuridisch1,bgLayerStaticJuridisch2', false);
				this.flamingo.setLayersVisible('mainFlamingoEmbed', 'map','igLayer,ioLayer,vvvpLayer', true);
				this.flamingo.setLayersVisible('overviewFlamingoEmbed', 'overview', 'gaOverzichtLayer', false);
				this.flamingo.drawLayerVVP('');
				break;
		}
	}

	,setBestemmingsHoofdgroepen: function(layers) {
		var enkel = [], dubbel = [];

		for(var i = 0; i < layers.length; i++) {
			if(layers[i].value) {
				switch(layers[i].itemType) {
				case 'Enkelbestemming' :
					enkel[enkel.length] = layers[i].layerName;
					break;
				case 'Dubbelbestemming' :
					dubbel[dubbel.length] = layers[i].layerName;
					break;
				default:
					break;
				}
			}
		}

		this.flamingo.showEnkelBestemmingHoofdgroepen(enkel.join(','));
		this.flamingo.showDubbelBestemmingen(dubbel.join(','));
	}

	,enableMapTips: function(onOff) {
		var layer = '';
		switch(Roo.currentTabFilter.name) {
		case "JURIDISCH":
		case "ACTUEEL":
			layer = 'bgLayer';
			break;
		case "INSTRUCTIE":
			layer = 'igLayer';
			break;
		case "VISIE":
			layer = 'vgLayer';
			break;
		}
		this.flamingo.enableMaptip(layer, onOff);
	}

	,moveExtendToSearchResult: function() {
		var bbox = {
			minX: 1000000
			,minY: 1000000
			,maxX: 0
			,maxY: 0
		};

		var boxes = plansStore.getRange();

		for(var i=0; i< boxes.length; i++) {
			var box = boxes[i].data.box;
			if(box.minX < bbox.minX) {bbox.minX = box.minX;}
			if(box.minY < bbox.minY) {bbox.minY = box.minY;}
			if(box.maxX > bbox.maxX) {bbox.maxX = box.maxX;}
			if(box.maxY > bbox.maxY) {bbox.maxY = box.maxY;}
		}

		this.flamingo.moveToExtent(bbox.minX, bbox.minY, bbox.maxX, bbox.maxY);
	}
	,renderVVPath: function(path) {
		this.flamingo.drawLayerVVP(path);
	}
	,renderVVPKaart: function(path) {
		var parts = path.split('/');
		if(Roo.currentPlan.identificatie != parts[0]) {
			Ext.getCmp('planDetailPanel').showPlan(parts[0]);
		}
		this.renderVVPath(path);
		if(!Ext.fly('plantreeWindow')) {
			openPlanTreeWindow();
		}
	}

});

//register xtype
Ext.reg('largemapPanel', Roo.gui.LargemapPanel);
