import cookie from "cookie_js";

export default class CookieHandler {
    static COOKIE_NAME = "gdpr_consent";
    static COOKIE_VERSION = "v1";

    static hasConsentCookie = () => {
        return CookieHandler.getConsentCookie() !== null;
    }

    static getConsentCookie = () => {
        try {
            const consentValue = cookie.get(CookieHandler.COOKIE_NAME, null);
            if (!consentValue) {
                return null;
            }

            const parsedValue = JSON.parse(consentValue);
            return parsedValue.version ? parsedValue.data : parsedValue;
        } catch (e) {
            return null;
        }
    }

    static setConsentCookie = (consentData, options = {}) => {
        const cookieStructure = {
            version: CookieHandler.COOKIE_VERSION,
            data: consentData
        };

        cookie.set(CookieHandler.COOKIE_NAME, JSON.stringify(cookieStructure), {
            ...{
                expires: 180,
                path: '/',
            },
            ...options
        });
    }

    static updateConsentCookie = (section, value) => {
        const consentData = CookieHandler.getConsentCookie() || {};
        const newConsentData = {...consentData};

        switch (section) {
            case 'analytics':
                newConsentData.analytics_storage = value ? "granted" : "denied";
                break;
            case 'marketing':
                newConsentData.ad_storage = value ? "granted" : "denied";
                newConsentData.personalization_storage = value ? "granted" : "denied";
                break;
            case 'functional':
                newConsentData.functional_storage = "granted";
                newConsentData.security_storage = "granted";
                break;
        }

        CookieHandler.setConsentCookie(newConsentData);
        return newConsentData;
    }

    static updateGTMConsentState = (consentData) => {
        if (typeof window === 'undefined' || typeof gtag !== 'function') {
            return;
        }

        gtag('consent', 'update', {
            analytics_storage: consentData.analytics_storage || 'denied',
            ad_user_data: consentData.analytics_storage || 'denied', // child of analytics_storage
            ad_storage: consentData.ad_storage || 'denied',
            personalization_storage: consentData.personalization_storage || 'denied',
            ad_personalization: consentData.personalization_storage || 'denied', // child of personalization_storage
            functionality_storage: consentData.functional_storage || 'granted',
            security_storage: consentData.security_storage || 'granted'
        });

        gtag('event', 'page_view');
    }

    static isCurrentVersion = () => {
        try {
            const consentValue = cookie.get(CookieHandler.COOKIE_NAME, null);
            if (!consentValue) {
                return false;
            }

            const parsedValue = JSON.parse(consentValue);
            return parsedValue && parsedValue.version === CookieHandler.COOKIE_VERSION;
        } catch (e) {
            return false;
        }
    }

    static clearCookiesIfVersionChanged = () => {
        if (!CookieHandler.isCurrentVersion()) {
            cookie.removeSpecific(CookieHandler.COOKIE_NAME, {path: '/'});
            return true;
        }

        return false;
    }
}
