// JavaScript Document
//require script fist to load all app
//The nature of this script is "config", so all function are generated inside of it
script_manager(["assets/js/utility/plotting.js","assets/js/functionality/ifis_watershed.js"]);	

//wbd=[[41.653,-91.563],[41.768,-92.018]]
//http://s-iihr32.iihr.uiowa.edu/dev/develope_portfolio/gallery/watershed_dss/discharge_predict.html?ws_route=no_ws&dss-ws=clear_creek&wbd=[[41.653,-91.563],[41.768,-92.018]]



 

 var dss_app_settings_hydro_tool={
	 "calculate_discharge":{
	 "sidebar_ui_html":'<p style="color:#0493d3; font-weight: bold"><label for="discharge_result">Discharge Prediction (cfs):</label> <div id="discharge_result" style="height:250px;" class="form-control" rows="8"></div> <br><button id="culvert_hydro-clear_search" class="form-control btn btn-primary" > &nbsp;Clear Result&nbsp; </button></p>',
	 "sidebar_ui_title":"Discharge Estimation",
	 "app_btn_id":"culvert_hydraulics-calculate_discharge",
	 "app_leaflet_layer":"main",//window["script_temp_data"]+"_leaflet_layers",
	 "panel_id":"culvert_hydraulics-calculate_discharge_panel", //this is for configure only
	 },
 };
var function_btn_event_list={	
	"calculate_discharge":{
			//the order of this does matters as it is referenced in the corresponding script
			"name":"Estimate Discharge",
			"btn_id":dss_app_settings_hydro_tool["calculate_discharge"]["app_btn_id"],
			"dss_script":[], 
			"event":function(){
						
						loading_sidebar(dss_app_settings_hydro_tool["calculate_discharge"]);						
						$("#eash_watershed_search").bootstrapSwitch(); 						
						culvert_hydraulic_discharge_event();
	
				
			},			
			},//end of the first btn	
}

var tool_set_plugin_spec={
		"planning_category_Tool":[
			function_btn_event_list["calculate_discharge"],
		],	
	};
//parse_app_btn_event(tool_set_plugin_spec);	

	$("#utility_panel").append(dss_app_settings_hydro_tool["calculate_discharge"]["sidebar_ui_html"]);
	loading_sidebar(dss_app_settings_hydro_tool["calculate_discharge"]);						
	$("#eash_watershed_search").bootstrapSwitch(); 						
	culvert_hydraulic_discharge_event();
	$("#eash_watershed_search").hide();

var Huc_ws_trace_style2={
	            fill:false,
				stroke:"#F80303",
				weight: 2,
				opacity: 0.6,
				color: '#F80303',
				fillOpacity: 0.1	
						};

function culvert_hydraulic_discharge_event(){	

	$("#reset_map").hide();
	$(".leaflet-legend").hide(); 
	window["ifis_search_result_marker"]= new L.LayerGroup().addTo(map);
	map.on('mousemove', function(e){
		ifis_ws_search(map,Huc_ws_trace_style2,e.latlng.lat,e.latlng.lng,90,"fit","single_search","first_cell_for_hover_only"); 		
		//console.log(window["ifis_search_on_hover"]); 
		for(var each in window["ifis_search_on_hover"]["_layers"]){
			window["ifis_search_on_hover"]["_layers"][each].on('click', function(e){
				window["ifis_search_result"].clearLayers();
				window["ifis_search_result_marker"].clearLayers();				
				var newMarker = new L.marker(e.latlng).addTo(window["ifis_search_result_marker"]);
				ifis_ws_search(map,Huc_ws_trace_style2,e.latlng.lat,e.latlng.lng,90,"fit","single_search","full_ws"); 
			});
		}
	}); 
	
	
	
	/*.on('click', (e) => {
		alert("!");
	}); */
	
	map.on('click', function(e) {
			
			//if($("#eash_watershed_search").bootstrapSwitch('state')==true){
	
			 console.log($("#eash_watershed_search").val());
			 
			window["ifis_search_result"].clearLayers();
			window["ifis_search_result_marker"].clearLayers();				
			var newMarker = new L.marker(e.latlng).addTo(window["ifis_search_result_marker"]);				
		
				//instant_watershed(map,Huc_ws_trace_style,e.latlng.lat,e.latlng.lng, 500,"fit");
			    ifis_ws_search(map,Huc_ws_trace_style2,e.latlng.lat,e.latlng.lng,90,"fit","single_search","full_ws"); 
				 
				iowa_culvert_cal_discharge({'layer':window["ifis_search_result"],'display_id':"discharge_result"});
				
				setTimeout(function(){ 
									if(($("#discharge_result").html()).length==0){
										
										setTimeout(function(){ 
										ifis_ws_search(map,Huc_ws_trace_style2,e.latlng.lat,e.latlng.lng, 90,"fit","single_search","full_ws");
										iowa_culvert_cal_discharge({'layer':window["ifis_search_result"],'display_id':"discharge_result"});
										}, 300);
										
									}else{
										
									}
									}, 500);//outer set time out
				
				//layer:window["ifis_search_result"],display_id:
			//}
		
	});	//end of map click
		
	 $(document).on("click","#culvert_hydro-clear_search",function() {
		window["ifis_search_result"].clearLayers();	
        $("#discharge_result").html("");	

	});
	
	//$("#eash_watershed_search").on('switchChange.bootstrapSwitch', function(event, state) { 
	//if(state==true){
		$('#map').css('cursor','crosshair');
	//}else{
		
		//$('#map').css('cursor','grab');
		//$('#discharge_result').html("");
		
	//}	
	//});	  

	function iowa_culvert_cal_discharge(options){
		$("#"+result_box).html("");
		var result_ws_layer=options["layer"]
		var result_box=options["display_id"]
				 for(var each in result_ws_layer["_layers"]){
					 
					 
				 each_geom=result_ws_layer["_layers"][each]["_layers"];
				 
					var first_geom;
						for(var key in each_geom) {
							if(each_geom.hasOwnProperty(key)) {
								first_geom = each_geom[key];
								break;
							}
						}//end of inner for loop, identify only the first geom
						
				 //console.log(first_geom["feature"]);
				 $.ajax({
								  url : "assets/php/iowa_culvert/Eash_Lara.php?",							  
								  type: "post",
								  async: false,
								  dataType : 'json',
								  jsonpCallback: 'getJson',


								  data: {geom:JSON.stringify(first_geom["feature"])},
						success: function(data){
								   //console.log(data);							   
								   var result="Q2: "+data["Q2"].toFixed(2)+"<br>Q5: "+data["Q5"].toFixed(2)+"<br>Q10: "+data["Q10"].toFixed(2)+"<br>Q25: "+data["Q25"].toFixed(2)+"<br>Q50: "+data["Q50"].toFixed(2)+"<br>Q100: "+data["Q100"].toFixed(2)+"<br>Q200: "+data["Q200"].toFixed(2)+"<br>Q500: "+data["Q500"].toFixed(2);
								   $("#"+result_box).html(result);
							}
					});	//end of ajax	
			 }
	}	
}




