/*!
 * Author:        Pierre-Henry Soria <hi@ph7.me>
 * Copyright:     (c) 2020, Pierre-Henry Soria. All Rights Reserved.
 */

const localKeyName = 'agreed18';
const strings = {
    welcome: 'Vítáme Vás!',
    site_contains_adult_materials: 'Tyto stránky obsahují sexuálně orientované materiály pro dospělé, které mohou být pro některé diváky urážlivé.',
    acknowledge_confirm_majority: 'Chcete-li pokračovat, potvrďte, že jste starší <span class="underline">18</span> let.',
    button_over18: 'Jsem starší 18-ti let',
    button_under18: 'Jsem mladší 18-ti let',
    footer_imprint_paragraph: 'Vstupem na tyto webové stránky vyjadřujete souhlas s našimi podmínkami používání a zásadami ochrany osobních údajů.',
};

class Disclaimer {
    constructor() {
        this.backgroundElement = document.getElementById('disclaimer-background');
        this.dialogElement = document.getElementById('disclaimer-dialog');

        this.dialogStatus = 0;
    }

    loadDialog() {
        if (this.dialogStatus === 0) {
            this.backgroundElement.style.opacity = '0.95';
            this.backgroundElement.style.display = 'block';
            this.dialogElement.style.display = 'block';

            this.dialogStatus = 1;
        }
    }

    disableDialog() {
        if (this.dialogStatus === 1) {
            this.dialogElement.style.display = 'none';
            this.backgroundElement.style.display = 'none';

            this.dialogStatus = 0;
        }
    }

    centerDialog() {
        const windowHeight = document.documentElement.clientHeight;
        const windowWidth = document.documentElement.clientWidth;
        const dialogHeight = parseInt(window.getComputedStyle(this.dialogElement).height);
        const dialogWidth = parseInt(window.getComputedStyle(this.dialogElement).width);

        this.dialogElement.style.position = 'absolute';
        this.dialogElement.style.top = (windowHeight / 2 - dialogHeight / 2).toString() + 'px';
        this.dialogElement.style.left = (windowWidth / 2 - dialogWidth / 2).toString() + 'px';
        this.backgroundElement.style.height = windowHeight;
    }

    isAccepted() {
        try {
            return sessionStorage.getItem(localKeyName);
        } catch (e) {
            console.log('Cannot use sessionStorage', e);
        }

        return null;
    }

    setAccepted() {
        try {
            sessionStorage.setItem(localKeyName, '1');
        } catch (e) {
            console.log('Cannot use sessionStorage', e);
        }
    }

    static generateDialog() {
        const code = `<div id="disclaimer-dialog">
      <div class="center">
      <h1>${strings.welcome}</h1>
      <p class="italic">${strings.site_contains_adult_materials}</p>
      <p class="bold">${strings.acknowledge_confirm_majority}</p>
      <p><button id="agree-over18" class="agree">${strings.button_over18}</button>
      <button id="disagree-under18" class="disagree">${strings.button_under18}</button></p>
      <p><small>${strings.footer_imprint_paragraph}</small></p>
      </div></div>
      <div id="disclaimer-background"></div>`;

        document.write(code);
    }
}