/**
 * @file
 * Age Gate.
 *
 */
(function ($, Drupal, Cookies) {
  "use strict";

  Drupal.behaviors.ageGateVerification = {
    attach: function (context, settings) {
      var path = window.location.pathname;
      var ageGate = $("#age-gate-old").length;
      var hasGlobalGate = $("#age-gate-old div.contry-select-age-gate").length;
      // Check if it is login or admin page
      if (path !== "/user/login" && path !== "/admin/content" && ageGate == 1) {
        //check if it has Global Gate to choose country
        // Cohesion Componenet Machine-name: cpt_age_gate_global
        if (hasGlobalGate > 0) {
          $(
            "#age-gate-old div.contry-select-age-gate div.views-element-container div.views-field-name span a"
          ).one("click", function () {
            var link = $(this).attr("data-atribute");
            var prohibited = $(this).attr("data-prohibited");
            var prohibitedMessage = $(this).attr("data-prohibited-message");
            var ageGateMainContent = $(".age-main-content");
            var ageGateMainContentTitle = ageGateMainContent.find("h3");
            var ageGateCountrySelect = $(".contry-select-age-gate");
            var legalText = ageGateMainContentTitle.text();

            if (prohibited === "On") {
              ageGateMainContentTitle.html(prohibitedMessage);
              ageGateCountrySelect.hide();
              ageGateMainContent.find(".coh-link").hide();
              ageGateMainContent.fadeIn();
            } else {
              legalText = legalText.replace("[legal-age]", link);
              ageGateMainContentTitle.html(legalText);
              ageGateCountrySelect.hide();
              ageGateMainContent.fadeIn();
            }
          });
        }

        var isUnderage = Cookies.get("isUnderage") != "0";

        if (isUnderage === true) {
          // Force click to close Age Gate Verification Modal
          // Cohesion Component ID: 62d3af08
          // Cohesion Componenet Machine-name: cpt_age_gate_modal
          $("div#age-gate-old")
            .removeClass("coh-style-hide")
            .addClass("coh-style-container-full-screen");
          // Add position relative, height 100% and overflow-y hidden in body and html to disable scroll on page
          $("html, body").css({ height: "100%" });
          $("html, body").css({ position: "relative" });
          $("html, body").css({ overflowY: "hidden" });
        }

        // Add a trigger to link element
        // and after user has click set cookie to true.
        $("div#age-gate-old a#age-gate--no").on("click", function () {
          Cookies.set("isUnderage", 1, { expires: 3 });
        });

        // If this button is clicked then the underage is set
        // to false and will store this info on cookie.
        $("div#age-gate-old a#age-gate--yes").on("click", function (e) {
          Cookies.set("isUnderage", 0, { expires: 3 });
          e.preventDefault();
          // Remove position, height and overflow-y to enable scroll on page again
          $("html, body").css({ height: "" });
          $("html, body").css({ position: "" });
          $("html, body").css({ overflowY: "" });
          $("#age-gate-old").remove();
          var $url = window.location.href;
          window.location.href = $url;
          var $headerHeight = $(".header-height").height();
          var $top = (window.pageYOffset || document.documentElement.scrollTop) - $headerHeight;
          window.scroll(0, $top);
        });
      }
    },
  };
})(jQuery, Drupal, Cookies);
