// ==================== ROUTING FUNCTIONS =============================
//convert date from DD MON YYYY to YYYYMMDD
function getDate() {
	const dict = {
		'Jan': '01',
		'Feb': '02',
		'Mar': '03', 
		'Apr': '04', 
		'May': '05', 
		'Jun': '06',
		'Jul': '07', 
		'Aug': '08', 
		'Sep': '09', 
		'Oct': '10', 
		'Nov': '11', 
		'Dec': '12'
	};
 	date = document.getElementById('date').value;
	splitDate = date.split(' ');
	month = dict[splitDate[1]];
	return '&date=' + splitDate[2] + month + splitDate[0];
}

function getVar() {
	//get the variable value
	curvar = document.querySelector('.Select-value-label').innerHTML;
	curvar = curvar.split(' ').join('_')
	return 'var=' + curvar.toLowerCase() + '&';	
}

function getModels(){
		// get models checked
		const data = [...document.querySelectorAll('.custom-control-input:checked')].map(e => e.value);
		data.forEach(function(item, index){
			this[index] = 'model=' + item
		}, data);		
		return data.join('&');
}

// CREATE OUTPUT URL for models-apply
$(document).ready(function () {
    $(document).on('click', "#models-apply", function () {
		curvar = getVar()
		models = getModels()
		outputDate = getDate();
		//convert to the output URL
		url = "/dashboard/?" + curvar + models + outputDate;
		window.history.pushState("Models", "Models", url);
	})	
});

// CREATE OUTPUT URL for TABS 
$(document).ready(function () {
    $(document).on('click', ".tab--selected", function () {
		tab = document.getElementsByClassName('tab--selected')[0].firstElementChild.innerHTML
		tab = tab.toLowerCase()
		url = "/dashboard/?tab=" + tab;
		window.history.pushState("Models", "Models", url);
	})	
});

// CREATE OUTPUT URL for group-1-toggle(models)
$(document).ready(function () {
    $(document).on('click', "#group-1-toggle", function () {
		url = "/dashboard/?tab=forecast&section=models";
		window.history.pushState("Models", "Models", url);
	})	
});

// CREATE OUTPUT URL for group-2-toggle(probability)
$(document).ready(function () {
    $(document).on('click', "#group-2-toggle", function () {
		url = "/dashboard/?tab=forecast&section=prob";
		window.history.pushState("Probability of Exceedance", "Probability", url);
	})	
});

// CREATE OUTPUT URL for group-3-toggle(was)
$(document).ready(function () {
    $(document).on('click', "#group-3-toggle", function () {
		url = "/dashboard/?tab=forecast&section=was";
		window.history.pushState("Warning Advisory System", "WAS", url);
	})	
});

// CREATE OUTPUT URL for nrt-evaluation(visual_comparision)
$(document).ready(function () {
    $(document).on('click', "#nrt-evaluation", function () {
		url = "/dashboard/?tab=evaluation&section=visual_comparison";
		window.history.pushState("Warning Advisory System", "WAS", url);
	})	
});

// CREATE OUTPUT URL for scores-evaluation(statistics)
$(document).ready(function () {
    $(document).on('click', "#scores-evaluation", function () {
		url = "/dashboard/?tab=evaluation&section=statistics";
		window.history.pushState("Warning Advisory System", "WAS", url);
	})	
});

// CREATE OUTPUT URL for scores-apply(statistics)
$(document).ready(function () {
    $(document).on('click', "#scores-apply", function () {
		url = "/dashboard/?tab=evaluation&section=statistics";
		window.history.pushState("Warning Advisory System", "WAS", url);
	})	
});

// CREATE OUTPUT URL for rgb(eumetsat rgb)
$(document).ready(function () {
    $(document).on('click', "#rgb", function () {
		url = "/dashboard/?tab=observations&section=eumetsat-rgb";
		window.history.pushState("EUMETSAT RGB", "RGB", url);
	})	
});

// CREATE OUTPUT URL for Visibility
$(document).ready(function () {
    $(document).on('click', "#visibility", function () {
		url = "/dashboard/?tab=observations&section=visibility";
		window.history.pushState("VISIBILITY", "VISIBILITY", url);
	})	
});

