// 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) => {
    // Create list of addresses
    const hosts = [
	'http://bscesdust02.bsc.es',
	'http://bscesdust02.bsc.es/products/daily-dust-products',
	'https://dust.aemet.es',
	'https://dust02.bsc.es',
	'https://dust02.bsc.es/products/daily-dust-products',
	'https://dust03.bsc.es'
    ];
    if (hosts.includes(event.origin)){
	console.log('DASH: Success!! ' + event.data);
	window.history.pushState("Models", "Models", event.data);
	return;
    }else{
	console.log('DASH: that is not the right address' + event.origin);
	return;
    }
}, false);

// 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('.form-check-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 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 A DICTIONARY OF IDs TO CREATE URL OUTPUTS
$(document).ready(function () {
    var toggleMap = {
	"#group-1-toggle": "?tab=forecast&section=models",
	"#group-2-toggle": "?tab=forecast&section=prob",
	"#group-3-toggle": "?tab=forecast&section=was",
	"#forecast-tab": "?tab=forecast",
	"#evaluation-tab": "?tab=evaluation",
	"#observations-tab": "?tab=observations",
	"#nrt-evaluation": "?tab=evaluation&section=visual_comparison",
	"#scores-evaluation": "?tab=evaluation&section=statistics",
	"#rgb": "?tab=observations&section=eumetsat-rgb",
	"#visibility": "?tab=observations&section=visibility"
    };
    // Match ID with the tooglemap key and output url value
    $(document).on('click', Object.keys(toggleMap).join(', '), function () {
	var selector = "#" + $(this).attr('id');
	var url = toggleMap[selector];
	sendURL(url);
    });
});


