// ==================== 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];
}

// LISTEN FOR MESSAGES FROM CMS
window.addEventListener("message", (event) => {
    if (event.origin !== "http://bscesdust02.bsc.es"){
	console.log('DASH: that is not the right address' + event.origin);
	return;
    }else{
	console.log('DASH: Success!! ' + event.data);
	window.history.pushState("Models", "Models", event.data);
	return;
    }
}, false);

// NOW ASSESS HOW THE URL SHOULD BE OUTPUT
function sendURL(url) {
    window.history.pushState("Models", "Models", url);
    parent.postMessage(url, '*');
}

//GET THE VARIABLE VALUE
function getVar() {
	curvar = document.querySelector('.Select-value-label').innerHTML;
	curvar = curvar.split(' ').join('_')
	return 'var=' + curvar.toLowerCase() + '&';	
}

// GET MODELS CHECKED
function getModels(){
		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();
		url = "?" + curvar + models + outputDate;
		sendURL(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 = "?tab=" + tab;
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR GROUP-1-TOGGLE(MODELS)
$(document).ready(function () {
    $(document).on('click', "#group-1-toggle", function () {
		url = "?tab=forecast&section=models";
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR GROUP-2-TOGGLE(PROBABILITY)
$(document).ready(function () {
    $(document).on('click', "#group-2-toggle", function () {
		url = "?tab=forecast&section=prob";
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR GROUP-3-TOGGLE(WAS)
$(document).ready(function () {
    $(document).on('click', "#group-3-toggle", function () {
		url = "?tab=forecast&section=was";
		sendURL(url);
	})	
});

// CREATE WAS OUTPUT
$(document).ready(function () {
    $(document).on('click', "#was-apply", function () {
		const checked = document.getElementById('was-dropdown');
		var text = checked.querySelector('input[type="radio"]:checked').parentElement.innerText;
	    text = text.split(' ').join('_').toLowerCase();
		url = "?tab=forecast&section=was&country=" + text;
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR NRT-EVALUATION(VISUAL_COMPARISION)
$(document).ready(function () {url.includes('dust.hqads.aemet.es')
    $(document).on('click', "#nrt-evaluation", function () {
		url = "?tab=evaluation&section=visual_comparison";
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR SCORES-EVALUATION(STATISTICS)
$(document).ready(function () {
    $(document).on('click', "#scores-evaluation", function () {
		url = "?tab=evaluation&section=statistics";
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR SCORES-APPLY(STATISTICS)
$(document).ready(function () {
    $(document).on('click', "#scores-apply", function () {
		url = "?tab=evaluation&section=statistics";
		sendURL(url);
	})	
});

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

// CREATE OUTPUT URL FOR VISIBILITY
$(document).ready(function () {
    $(document).on('click', "#visibility", function () {
		url = "?tab=observations&section=visibility";
		sendURL(url);
	})	
});

