// ==================== 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);

// 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', "#forecast-tab", function () {
		url = "?tab=forecast";
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR TABS 
$(document).ready(function () {
    $(document).on('click', "#evaluation-tab", function () {
		url = "?tab=evaluation";
		sendURL(url);
	})	
});

// CREATE OUTPUT URL FOR TABS 
$(document).ready(function () {
    $(document).on('click', "#observations-tab", function () {
		url = "?tab=observations";
		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 () {
    $(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);
	})	
});

