/*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
        viewer.scene.globe.enableLighting = false;
		
		// 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);
	});
	
//////////////////////////////////////////  load HexGrid as Tiles

var hexTiles = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
		url: 'results/Pressure_2mHexWidth/tileset.json',					
		show: true
	}));
	
	hexTiles.readyPromise.then(function (hexTiles){ 
		
		// set tileset height
       var height = 318;       
       var cartographic = Cesium.Cartographic.fromCartesian(hexTiles.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());
       hexTiles.modelMatrix = Cesium.Matrix4.fromTranslation(translation);

      // return zoomAll(hexTiles);
	});
	
	
	hexTiles.style = new Cesium.Cesium3DTileStyle({

			color: {

				conditions: [		

					['Number(${MEAN_Z}) >= 112999.0022', "color('rgb(255,0,0)',0.7)",],

					['Number(${MEAN_Z}) >= 112996.07599', "color('rgb(230, 107 , 33)',0.7)"],

					['Number(${MEAN_Z}) >= 112993.65973', "color('rgb(255, 179, 0)',0.7)"],

					['Number(${MEAN_Z}) >= 112990.34346', "color('rgb(191,191,0)',0.7)"],

					['Number(${MEAN_Z}) >= 112987.0272', "color('rgb(0,128,255)',0.7)"],

					['Number(${MEAN_Z}) >= 112983.71094', "color('rgb(0,0,255)',0.7)"]
			
				]

            }
        });	
	
	
	    viewer.camera.flyTo({
			// destination: Cesium.Cartesian3.fromDegrees(9.190915, 48.786976,  1020),
			destination: Cesium.Cartesian3.fromDegrees(  9.188375, 48.784283, 1020),			
            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();


});
