// FI

const nogModal = {
	html: `
    <div class="nogModalContainer">
        <div class="nogModal">{CONTENT}</div>
    </div>

    <style>
        .nogModalContainer {
            display: flex;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 10;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100%;
        }

        .nogModal {
            padding: 20px;
            border-radius: 3px;
            background: #ffffff;
            position: relative;
            border: 1px solid #000;
            max-width: 75%;
            max-width: 400px;
            max-height: 75%;
            overflow: auto;
        }
    </style>
	`,

	activeElement: null,
	container:     null,
	overflow:      null,

	show: function(content) {
			this.container = document.createElement('div');
			this.container.innerHTML=this.html.replace('{CONTENT}', content);
			document.body.appendChild(this.container);
			this.overflow = document.body.style.overflow;
			document.body.style.overflow='hidden';
			setTimeout(()=>{focus()},1000);
			
			if (document.activeElement) {
					this.activeElement = document.activeElement;
			}
			document.activeElement.blur();
	},

	hide: function() {
		 this.container.remove();
		 document.body.style.overflow = this.overflow;
			if (this.activeElement) {
					this.activeElement.focus();
			}
	}
}

let html = `
    <h3>
        We value your privacy
    </h3>
    <p>
        We use third party services for advertising and
        analytics. These might use cookies and other tracking
        technologies to show personalized content and targeted
        ads. To learn more, please read our
        <a style="text-decoration: underline" href="/privacy">privacy policy</a>.
    </p>

    <p>
        By choosing "OK", you consent to the data processing described
        in our privacy policy
    </p>

    <div style="text-align: center; padding-top: 20px;">
     <button id=consentModalConsent>OK</button>
    </div>
`;

function hasCookie() {
    // return document.cookie.split('; ').find(row => row.startsWith('nogConsent'))
    return document.cookie.includes('nogConsent=1');
}

function setCookie() {
    document.cookie = "nogConsent=1; expires=Fri, 31 Dec 9999 23:59:59 GMT;";
}

function activateScripts() {
    const scripts = document.querySelectorAll('script.nogConsent');
    for (const script of scripts) {
        script.src = script.dataset.src;
    }
    if (typeof(nogConsentCallback)=='function')
        nogConsentCallback();
}

function consent() {
    setCookie();
    nogModal.hide();
    activateScripts();
}

function showModal() {
    nogModal.show(html);
    document
        .querySelector('#consentModalConsent')
        .addEventListener('click', consent);
}

// --------------------------------------------------------
// Main
// --------------------------------------------------------

let mode = 'normal';

if (location.pathname.includes('privacy'))
    mode = 'disabled';

if (mode == 'normal') {
    if (hasCookie()) activateScripts();
    else             showModal();
}

if (mode == 'unimplemented') {
    activateScripts();
}
