/*var viewer = new Cesium.Viewer('cesiumContainer', {
    sceneMode : Cesium.SceneMode.SCENE2D,
    timeline : false,
    animation : false
});

var dataSource = Cesium.GeoJsonDataSource.load('../SampleData/simplestyles.geojson');
viewer.dataSources.add(dataSource);
viewer.zoomTo(dataSource);

*/
$(function () {
	Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5ODI4ZTYyZS1mMTg2LTQ5NGEtYjdiOS02ODg2NzVhNjc0MTAiLCJpZCI6MjkwNCwiaWF0IjoxNTM1MTA5OTAzfQ.kyDX_0ScvJBkYnvXI0DW5NfZbiaRL5ezwtAUhxYnk1Y';

	var imageryViewModels = [];
	imageryViewModels.push(new Cesium.ProviderViewModel({
		name: 'Sentinel-2',
		iconUrl: Cesium.buildModuleUrl('Widgets/Images/ImageryProviders/sentinel-2.png'),
		tooltip: 'Sentinel-2 cloudless.',
		creationFunction: function () {
			return new Cesium.IonImageryProvider({ assetId: 3954 });
		}
	}));
	
	imageryViewModels.push(new Cesium.ProviderViewModel({
		name: 'Blue Marble',
		iconUrl: Cesium.buildModuleUrl('Widgets/Images/ImageryProviders/blueMarble.png'),
		tooltip: 'Blue Marble Next Generation July, 2004 imagery from NASA.',
		creationFunction: function () {
			return new Cesium.IonImageryProvider({ assetId: 3845 });
		}
	}));

    var viewer = new Cesium.Viewer('cesiumContainer',
		{
			imageryProvider: new Cesium.IonImageryProvider({ assetId: 3954 }),
				
			terrainProvider : new Cesium.CesiumTerrainProvider({
					url: Cesium.IonResource.fromAssetId(1)
				}),
			scene3DOnly: false,
			shouldAnimate: true,
			animation: true,
			infoBox: true,
			baseLayerPicker: true,
			fullscreenButton: false,
			timeline: false,
			navigationHelpButton: true,
			navigationInstructionsInitiallyVisible: false,
			homeButton: false,
			selectionIndicator: true,
			geocoder: true,
			// imageryProviderViewModels: imageryViewModels
		});

		//Cesium.ShadowMode.ENABLED
		Cesium.ShadowMode.DISABLED
		
		// var layer = viewer.imageryLayers.addImageryProvider(
		// 	new Cesium.IonImageryProvider({ assetId: 3 })
		// );

		var imageryLayer = viewer.imageryLayers.addImageryProvider(
			new Cesium.IonImageryProvider({ assetId: 3954 })
		  );

	var tileset = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
		url: 'buildingTiles/StoeckachLOD1/tileset.json',			
		show: true
	}));
	
	tileset.readyPromise.then(function (tileset){ 
		
		// set tileset height
       var height = 49;       
       var cartographic = Cesium.Cartographic.fromCartesian(tileset.boundingSphere.center);
       var surface = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, 0.0);
       var offset = Cesium.Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, height);
       var translation = Cesium.Cartesian3.subtract(offset, surface, new Cesium.Cartesian3());
       tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation);

       return zoomAll(tileset);
	});


	 var geojsonOptions = {
	    	    clampToGround: true
	    	};
		
				
	var heatmapHexEntity;
	var heatmapHexPromise;
	var load_hexGrid_already =false;
//	var load_hexGrid = function () {
	    if (!load_hexGrid_already) {
	        heatmapHexPromise = Cesium.GeoJsonDataSource.load('results/Pressure_5mHexWidth.geojson', geojsonOptions);
	    	// heatmapHexPromise = Cesium.GeoJsonDataSource.load('results/fullcfd_wgs84.geojson', geojsonOptions);
	    	
	        heatmapHexPromise.then(function (dataSource) {
	            viewer.dataSources.add(dataSource);
	            heatmapHexGrid = dataSource.entities;
	            //viewer.flyTo(heatmapHexGrid)
	            //heatmapHexGrid.show = true;
	            var neighborhoodEntities = dataSource.entities.values;
	            for (var i = 0; i < neighborhoodEntities.length; i++) {
	                var entity = neighborhoodEntities[i];
	                if (Cesium.defined(entity.polygon)) {
	                	var alpha = 0.9;
	                    var heatmapClass = entity.properties.MEAN_Z._value;
	                    //viewer.imageryLayers.raise(heatmapHexGrid);
	                    // entity.zIndex = 0;
	                    
	                    //use this for  this file: fullcfd_wgs84.geojson
	                    // if (heatmapClass >= 253) {
	                    //     entity.polygon.material = new Cesium.Color(1, 0, 0, alpha);
	                    // } else if (heatmapClass >= 245) {
	                    //     entity.polygon.material = new Cesium.Color(0.9, 0.42, 0.13, alpha);
	                    // } else if (heatmapClass >= 237) {
	                    //     entity.polygon.material = new Cesium.Color(1, 0.7, 0, alpha);
	                    // } else if (heatmapClass >= 229) {
	                    //     entity.polygon.material = new Cesium.Color(0.75, 0.75, 0, alpha);
	                    // } else if (heatmapClass >= 221) {
	                    //     entity.polygon.material = new Cesium.Color(0, 0.5, 1, alpha);
	                    // } else if (heatmapClass >= 213) {
	                    //     entity.polygon.material = new Cesium.Color(0, 0, 1, alpha);
						// }
						

						 // 112983.710940000	112987.787910000	112991.864880000	112995.941850000	113000.018820000	113004.095790000
						if (heatmapClass >= 112999.0957) {
	                        entity.polygon.material = new Cesium.Color(1, 0, 0, alpha);
	                    } else if (heatmapClass >= 112997.0188) {
	                        entity.polygon.material = new Cesium.Color(0.9, 0.42, 0.13, alpha);
	                    } else if (heatmapClass >= 112994.941) {
	                        entity.polygon.material = new Cesium.Color(1, 0.7, 0, alpha);
	                    } else if (heatmapClass >= 112991.8648) {
	                        entity.polygon.material = new Cesium.Color(0.75, 0.75, 0, alpha);
	                    } else if (heatmapClass >= 112987.7879) {
	                        entity.polygon.material = new Cesium.Color(0, 0.5, 1, alpha);
	                    } else if (heatmapClass >= 112983.71094) {
	                        entity.polygon.material = new Cesium.Color(0, 0, 1, alpha);
	                    }
	                    
	                   entity.polygon.outline = false;
	                    
	                    // entity.zIndex = 99;
	                    // entity.polygon.outlineColor = Cesium.Color(1, 1, 1, 0);
	                }
	            }
	            
	        });
	    } else {
	        heatmapHexGrid.show = true;
	        viewer.flyTo(heatmapHexGrid)
	    }
//	}




	    viewer.camera.flyTo({
            destination: Cesium.Cartesian3.fromDegrees(9.190915, 48.786976,  1520),
            orientation: {
                heading: Cesium.Math.toRadians(0.0),
                pitch: Cesium.Math.toRadians(-90.0),
                roll: 0.0
            }
        });

	    
	    function showLegend() {
	        $("#legend").css("display", "block");
	    }
	    function hideLegend() {
	        $("#legend").css("display", "none");
	    }

	    // Legend - Colour Table
	    function emptyColourTable() {
	        $(".inner").empty();
	    }

	 
	    
		function setHeightTable() {
	        $(".inner").append( "<table>" +
	            "<tr><td class='outlined' bgcolor='red'>&nbsp;&nbsp;&nbsp;&nbsp;</td><td> >30 </td></tr>" +
	            "<tr><td class='outlined' bgcolor='#ff6b21'></td><td> 24 </td></tr>" +	            
	    		"<tr><td class='outlined' bgcolor='#FFB300'></td><td> 18</td></tr>" +
	            "<tr><td class='outlined' bgcolor='#BFBF00'></td><td> 12 </td></tr>" +                
				"<tr><td class='outlined' bgcolor='#0080FF',0)'></td><td> 6 </td></tr>" +
				"<tr><td class='outlined' bgcolor='#0000FF',0)'></td><td> 0 </td></tr>" +	            
	        "</table>" );
	    }
	     
	 
	    
	    showLegend();
		setHeightTable();


});
