let cookie_today = new Date(),
	cookie_expires_date = new Date (cookie_today.getTime() + (365*24*60*60*1000)),
	cAnalyticsId,
	template;
	
function sendData(cookiesAnalyticsId, headline, text, textButton){
	cAnalyticsId = cookiesAnalyticsId;
	let t_headline = headline || 'Contenido\x20de\x20terceros\x20bloqueado',
		t_text = text || 'El\x20contenido\x20original\x20no\x20puede\x20ser\x20mostrado\x20debido\x20a\x20que\x20eligió\x20no\x20aceptar\x20cookies\x20de\x20terceros\x2e',
		t_textButton = textButton || 'Revisar\x20mi\x20elección';
	template = "<div class='blocked-content'><p class='blocked-content__headline'><span class='filled-menos ico-rojo blocked-content__icon'></span><span class='bold'>" + t_headline + "</span></p><p class='italic'>" + t_text + "</p><p class='text-center'><button class='blocked-content__check'>" + t_textButton + "</button></p></div>";
}

function getCookie(cname) {
	let name = cname + "=";
	let decodedCookie = decodeURIComponent(document.cookie);
	let ca = decodedCookie.split(';');
	for(let value of ca) {
		let c = value.trim();
		if (c.indexOf(name) == 0) { 
			return c.substring(name.length, c.length);
		}
	}
	return "";
}

function showThirdPartyContent(){
	$("iframe[data-jscookies=thirdparty]").each(function(){
		let $iframe = $(this),
			url = $iframe.data('src');
		$iframe.attr("src", url);
		$iframe.css('display', "initial");
	});
	$("script[data-jscookies=thirdparty]").each(function(){
		let $script = $(this);
		$script.attr('type','text/javascript');
		let code = $script[0].outerHTML;
		$script.parent().append(code);
	});
}

function blockThirdPartyContent(){
	$('[data-jscookies=thirdparty]').each(function(){
		$(this).replaceWith(template);
	})
}
function acceptCookies(){
	$("#cookies-rejected").attr('disabled', 'disabled');
	// ****** LOCAL  ******
	// document.cookie = "COOKIES_CHECKED=true; expires="+cookie_expires_date.toUTCString()+"; path=/;";
	// document.cookie = "COOKIES_ACCEPTED=true; expires="+cookie_expires_date.toUTCString()+"; path=/;";

	// ****** ENTORNOS HTTPS  ******
	document.cookie = "COOKIES_CHECKED=true; expires="+cookie_expires_date.toUTCString()+"; path=/; Secure";
	document.cookie = "COOKIES_ACCEPTED=true; expires="+cookie_expires_date.toUTCString()+"; path=/; Secure";

	showThirdPartyContent();
	consentGranted();
	closeCookiesBanner();
}

function rejectCookies(){
	$("#cookies-accepted").attr('disabled', 'disabled');
	// ****** LOCAL  ******
	// document.cookie = "COOKIES_CHECKED=true; expires="+cookie_expires_date.toUTCString()+"; path=/;";
	// document.cookie = "COOKIES_ACCEPTED=false; expires="+cookie_expires_date.toUTCString()+"; path=/;";

	// ****** ENTORNOS HTTPS  ******
	document.cookie = "COOKIES_CHECKED=true; expires="+cookie_expires_date.toUTCString()+"; path=/; Secure";
	document.cookie = "COOKIES_ACCEPTED=false; expires="+cookie_expires_date.toUTCString()+"; path=/; Secure";
	
	blockThirdPartyContent();
	denyGranted();
	closeCookiesBanner();
}

function resetCookies(){
	document.cookie = "COOKIES_CHECKED=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
	document.cookie = "COOKIES_ACCEPTED=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
	denyGranted();
	location.reload();
}
// Update google analytics options 
function denyGranted() {
	if(cAnalyticsId){
		let analyticsId = cAnalyticsId;
		window.dataLayer = window.dataLayer || [];
		function gtag(){window.dataLayer.push(arguments);}

		gtag('consent', 'default', {'ad_storage': 'denied','analytics_storage':'denied'});
		
		gtag('js', new Date());
		gtag('config', analyticsId);
	}
}
function consentGranted() {
	if(cAnalyticsId){
		let analyticsId = cAnalyticsId;
		window.dataLayer = window.dataLayer || [];
		function gtag(){window.dataLayer.push(arguments);}
		
		gtag('consent', 'update', {'ad_storage': 'granted','analytics_storage':'granted'});
		
		gtag('js', new Date());
		gtag('config', analyticsId);
	}
}
function closeCookiesBanner(){
	$(".cookies-banner").hide();
}
function manageCookies(){
	if (getCookie('COOKIES_CHECKED') == 'true'){
		if (getCookie('COOKIES_ACCEPTED') == 'false'){
			blockThirdPartyContent();
			denyGranted();
		}else if(getCookie('COOKIES_ACCEPTED') == 'true'){
			showThirdPartyContent();
			consentGranted();
		}else{
			denyGranted();
			$(".cookies-banner").show();
		}
	}else{
		denyGranted();
		$(".cookies-banner").show();
	}
}

function initCookies(){
	$(document).on('click','.blocked-content__check', resetCookies);
	$(document).on('click','#cookies-accepted', acceptCookies);
	$(document).on('click','#cookies-rejected', rejectCookies);

	manageCookies();
}
