const cookiePrompt = document.getElementById('cookie-prompt');
let acceptedCookies = getCookie("acceptCookies");

makePopup();

const cookieAccept = document.getElementById('cookie-accept');
const cookieDecline = document.getElementById('cookie-decline');

cookieAccept.addEventListener('click', function(e){
	e.preventDefault();
	// Only add cookie if it's not there
    if (acceptedCookies == 0){
		acceptCookie();
	}
});
cookieDecline.addEventListener('click', function(e){
	e.preventDefault();
	alert('Boohoo');
});

function makePopup(){
    // Cookie exists
    if (acceptedCookies == 0){
        showCookiePopup();
    }
    else if (acceptedCookies == 1){
        // Nothing, no need to do anything
    }
    else{
        showCookiePopup();
        setCookie("acceptCookies", 0, 365);
    }
}

function showCookiePopup(){
	cookiePrompt.classList.remove('cookie-prompt--hidden');
}

function acceptCookie(){
    setCookie("acceptCookies", 1, 365);
	cookiePrompt.classList.add('cookie-prompt--hidden');
}

function setCookie(cname, cvalue, exdays) {
    const d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    let expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/" + ";SameSite=Lax";
  }

function getCookie(cname) {
    let name = cname + "=";
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(';');
    for(let i = 0; i <ca.length; i++) {
      let c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }

