
document.addEventListener("DOMContentLoaded", function () {
  var privacyNotice = document.querySelector(".privacy-notice");

  document.showPrivacyNotice = function () {
    privacyNotice.classList.remove("collapsed");
  };

  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;
  }

  function readCookie() {
    var privacyCookie = getCookie("privacy-settings");
    document.privacyNotice = JSON.parse(privacyCookie)
      ? JSON.parse(privacyCookie)
      : {};
  }

  function setCookie() {
    var cookie = {};
    cookie.essential = !!document.privacyNotice.essential;
    cookie.media = !!document.privacyNotice.media;
    cookie.analytics = !!document.privacyNotice.analytics;
    var date = new Date();
    date.setTime(date.getTime() + 365 * 24 * 60 * 60 * 1000);
    var expires = `expires=${date.toUTCString()}`;
    document.cookie = `privacy-settings=${JSON.stringify(
      cookie
    )}; ${expires}; path=/`;
  }

  function hideSocialMedia() {
    var iframes = document.querySelectorAll("iframe");
    iframes.forEach(function (iframe) {
      var src = iframe.getAttribute("src");
      var url = iframe.getAttribute("data-url");
      if (
        src.includes("facebook.com") ||
        src.includes("youtube.com") ||
        src.includes("youtube-nocookie.com") ||
        (url !== undefined && url !== null)
      ) {
        var parent = iframe.parentElement;
        iframe.remove();
        parent.innerHTML =
          '<div style="max-width: 300px;padding: 10px;"><p>Bitte akzeptieren sie den Gebraucht von Media Cookies, damit Ihnen Inhalte aus Social Media Kanälen angezeigt werden können.</p><button class="btn btn-info btn-archive" onclick="document.showPrivacyNotice()" style="float: left;margin: 20px 0;">Cookie Einstellungen anpassen</button></div>';
      }
    });
  }

  function showSocialMedia() {
    var iframes = document.querySelectorAll("iframe");
    iframes.forEach(function (iframe) {
      var url = iframe.getAttribute("data-url");
      if (url !== undefined && url !== null) {
        iframe.setAttribute("src", url);
      }
    });
  }

  var privacyNoticeText = privacyNotice.querySelector(".text");
  var privacyNoticeSettings = privacyNotice.querySelector(
    ".settings-container"
  );
  var moreButton = privacyNotice.querySelector(".show-more");
  var lessButton = privacyNotice.querySelector(".show-less");
  var closeButton = privacyNotice.querySelector(".decline");
  var saveAllButton = privacyNotice.querySelector(".accept-all");
  var saveOptionButton = privacyNotice.querySelector(".accept-option");
  var sliderButton = privacyNotice.querySelectorAll(".slider");

  moreButton.addEventListener("click", function () {
    privacyNoticeText.style.height = "0";
    privacyNoticeSettings.style.height = "initial";
    saveAllButton.style.display = "none";
    saveOptionButton.style.display = "inline-flex";
  });

  lessButton.addEventListener("click", function () {
    privacyNoticeText.style.height = "initial";
    privacyNoticeSettings.style.height = "0";
    saveAllButton.style.display = "inline-flex";
    saveOptionButton.style.display = "none";
  });

  sliderButton.forEach(function (slider) {
    slider.addEventListener("click", function (event) {
      var activeSlider = event.target.closest(".slider");
      if (!activeSlider.classList.contains("disabled")) {
        if (activeSlider.getAttribute("data-active") === "true") {
          activeSlider.setAttribute("data-active", "false");
        } else {
          activeSlider.setAttribute("data-active", "true");
        }
      }
    });
  });

  closeButton.addEventListener("click", function () {
    privacyNotice.classList.add("collapsed");
  });

  saveOptionButton.addEventListener("click", function () {
    privacyNotice.classList.add("collapsed");
    privacyNotice.querySelectorAll(".slider").forEach(function (slide) {
      if (slide.getAttribute("data-active") === "true") {
        document.privacyNotice[slide.getAttribute("data-option")] = true;
      } else {
        document.privacyNotice[slide.getAttribute("data-option")] = false;
      }
      location.reload();
    });
    setCookie();
  });

  saveAllButton.addEventListener("click", function () {
    privacyNotice.classList.add("collapsed");
    document.privacyNotice.essential = true;
    document.privacyNotice.media = true;
    document.privacyNotice.analytics = true;
    setCookie();
    location.reload();
  });

  readCookie();
  if (!document.privacyNotice.essential) {
    var cookieRequired = document.querySelectorAll(".require-cookies");
    privacyNotice.classList.remove("collapsed");
    cookieRequired.forEach(function (element) {
      console.log(element);
      element.style.display = "none";
    });
  } else {
    var noCookie = document.querySelectorAll(".no-cookies");
    noCookie.forEach(function (element) {
      element.style.display = "none";
    });
  }
  privacyNotice
    .querySelector('.slider[data-option="media"]')
    .setAttribute("data-active", document.privacyNotice.media);
  privacyNotice
    .querySelector('.slider[data-option="analytics"]')
    .setAttribute("data-active", document.privacyNotice.analytics);
  if (!document.privacyNotice.media) {
    hideSocialMedia();
  } else {
    showSocialMedia();
  }
});

