// Boot
jQuery(document).ready(function($) { 

    // Check if UI should be shown
    if(getCookie('eccp_status')=='custom' || getCookie('eccp_status')=='granted' || getCookie('eccp_status')=='denied'){
        eccp_close();
        $(".eccp_reselect").show();
    }

    // Define dataLayer and the gtag function.
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}

    // Default to deny all
    gtag('consent', 'default', {
        'ad_storage': 'denied',
        'ad_user_data': 'denied',
        'ad_personalization': 'denied',
        'analytics_storage': 'denied'
      });

    var eccp_state_ad_user_data = 'denied';
    var eccp_state_ad_personalization = 'denied';
    var eccp_state_ad_storage = 'denied';
    var eccp_state_analytics_storage = 'denied';

    // If already approved
    if(getCookie('eccp_status')=='granted'){
        eccp_state_ad_user_data = 'granted';
        eccp_state_ad_personalization = 'granted';
        eccp_state_ad_storage = 'granted';
        eccp_state_analytics_storage = 'granted';
        allConsentGranted();
    } 

    // If custom selected
    if(getCookie('eccp_status')=='custom'){

        if(getCookie('eccp_ad_user_data')=='granted'){
            eccp_state_ad_user_data = 'granted';
        } else {
            eccp_state_ad_user_data = 'denied';
        }

        if(getCookie('eccp_ad_personalization')=='granted'){
            eccp_state_ad_personalization = 'granted';
        } else {
            eccp_state_ad_personalization = 'denied';
        }

        if(getCookie('eccp_ad_storage')=='granted'){
            eccp_state_ad_storage = 'granted';
        } else {
            eccp_state_ad_storage = 'denied';
        }

        if(getCookie('eccp_analytics_storage')=='granted'){
            eccp_state_analytics_storage = 'granted';
        } else {
            eccp_state_analytics_storage = 'denied';
        }

        // Update
        eccp_update();

    } 

    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}

    gtag('js', new Date());
    gtag('config', pluginData.api_key);

    // Button Actios
    $("#eccp_approve").click(function(){
        eccp_state_ad_user_data = 'granted';
        eccp_state_ad_personalization = 'granted';
        eccp_state_ad_storage = 'granted';
        eccp_state_analytics_storage = 'granted';
        allConsentGranted();
        eccp_close();
        $(".eccp_reselect").show();
    });
    $("#eccp_deny").click(function(){
        setCookie('eccp_status','denied',182);
        $(".eccp_reselect").show();
        eccp_close();
    });
    $("#eccp_confirm").click(function(){
        $(".eccp_selection").hide();
        $(".eccp_reselect").show();
        eccp_close();
        eccp_update();
    });
    $(".eccp_custom_manage").click(function(){
        $(".eccp_selection").show();
        $(".eccp_reselect").hide();

        // Update UI
        if(eccp_state_ad_user_data == 'granted'){
            $("#eccp_ad_user_data").prop('checked', true);
        }

        if(eccp_state_ad_personalization == 'granted'){
            $("#eccp_ad_personalization").prop('checked', true);
        }

        if(eccp_state_ad_storage == 'granted'){
            $("#eccp_ad_storage").prop('checked', true);
        }

        if(eccp_state_analytics_storage == 'granted'){
            $("#eccp_analytics_storage").prop('checked', true);
        }
    });
    $(".eccp_manage_cookies_link").click(function(){
        $(".eccp_selection").show();
        $(".eccp_reselect").hide();

        // Update UI
        if(eccp_state_ad_user_data == 'granted'){
            $("#eccp_ad_user_data").prop('checked', true);
        }

        if(eccp_state_ad_personalization == 'granted'){
            $("#eccp_ad_personalization").prop('checked', true);
        }

        if(eccp_state_ad_storage == 'granted'){
            $("#eccp_ad_storage").prop('checked', true);
        }

        if(eccp_state_analytics_storage == 'granted'){
            $("#eccp_analytics_storage").prop('checked', true);
        }
    });
    $(".eccp_reselect").click(function(){

        // Show Manage UI
        $(".eccp_reselect").hide();
        $(".eccp_selection").show();

        // Update UI
        if(eccp_state_ad_user_data == 'granted'){
            $("#eccp_ad_user_data").prop('checked', true);
        }

        if(eccp_state_ad_personalization == 'granted'){
            $("#eccp_ad_personalization").prop('checked', true);
        }

        if(eccp_state_ad_storage == 'granted'){
            $("#eccp_ad_storage").prop('checked', true);
        }

        if(eccp_state_analytics_storage == 'granted'){
            $("#eccp_analytics_storage").prop('checked', true);
        }

    });


    // Individual Changes
    $("#eccp_ad_user_data").change(function(){
        if( $("#eccp_ad_user_data").is(':checked') ){
            eccp_state_ad_user_data = 'granted';
        } else {
            eccp_state_ad_user_data = 'denied';
        }
        eccp_update();
    });

    $("#eccp_ad_personalization").change(function(){
        if( $("#eccp_ad_personalization").is(':checked') ){
            eccp_state_ad_personalization = 'granted';
        } else {
            eccp_state_ad_personalization = 'denied';
        }
        eccp_update();
    });

    $("#eccp_ad_storage").change(function(){
        if( $("#eccp_ad_storage").is(':checked') ){
            eccp_state_ad_storage = 'granted';
        } else {
            eccp_state_ad_storage = 'denied';
        }
        eccp_update();
    });

    $("#eccp_analytics_storage").change(function(){
        if( $("#eccp_analytics_storage").is(':checked') ){
            eccp_state_analytics_storage = 'granted';
        } else {
            eccp_state_analytics_storage = 'denied';
        }
        eccp_update();
    });


    // If user consents
    function allConsentGranted() {
        gtag('consent', 'update', {
            'ad_user_data': 'granted',
            'ad_personalization': 'granted',
            'ad_storage': 'granted',
            'analytics_storage': 'granted'
        });

        // Set
        setCookie('eccp_status','granted',182);
        setCookie('eccp_ad_user_data',eccp_state_ad_user_data,182);
        setCookie('eccp_ad_personalization',eccp_state_ad_personalization,182);
        setCookie('eccp_ad_storage',eccp_state_ad_storage,182);
        setCookie('eccp_analytics_storage',eccp_state_analytics_storage,182);
    }

    // If user selects customer
    function eccp_update(){
        gtag('consent', 'update', {
            'ad_user_data': eccp_state_ad_user_data,
            'ad_personalization': eccp_state_ad_personalization,
            'ad_storage': eccp_state_ad_storage,
            'analytics_storage': eccp_state_analytics_storage
        });

        // Set
        setCookie('eccp_status','custom',182);
        setCookie('eccp_ad_user_data',eccp_state_ad_user_data,182);
        setCookie('eccp_ad_personalization',eccp_state_ad_personalization,182);
        setCookie('eccp_ad_storage',eccp_state_ad_storage,182);
        setCookie('eccp_analytics_storage',eccp_state_analytics_storage,182);
    }

});



// Close UI
function eccp_close(){
    (function ($) {
        $("#eccp_ui").remove();
        })(jQuery);
}

// Set Status Cookie
function setCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}