/* Maple GDPR Cookies - Admin JavaScript */
(function ($) {
  "use strict";

  $(document).ready(function () {
    // Initialize color picker
    if ($.fn.wpColorPicker) {
      $(".mgc-color-picker").wpColorPicker();
    }

    // Custom color input preview
    function updateColorPreview(input) {
      var color = $(input).val();
      var hexPattern = /^#[0-9A-F]{6}$/i;

      if (hexPattern.test(color)) {
        $(input).css({
          "border-left": "5px solid " + color,
          "border-color": color,
        });
      } else {
        $(input).css({
          "border-left": "",
          "border-color": "",
        });
      }
    }

    // Add event listeners for custom color inputs
    $("#mgc_custom_button_color, #mgc_custom_button_hover_color").on(
      "input change",
      function () {
        updateColorPreview(this);
      },
    );

    // Initialize preview on page load
    $("#mgc_custom_button_color, #mgc_custom_button_hover_color").each(
      function () {
        updateColorPreview(this);
      },
    );

    // Preview functionality
    $("#mgc-preview-button").on("click", function (e) {
      e.preventDefault();
      // Trigger preview update
      updatePreview();
    });

    function updatePreview() {
      // Simple preview update logic
      console.log("Preview updated");
    }

    // Handle preference display type radio buttons
    var $preferenceRadios = $('input[name="mgc_preference_display_type"]');
    var $gdprWarning = $("#mgc-gdpr-warning");

    // Function to toggle GDPR warning visibility
    function toggleGdprWarning() {
      var selectedType = $(
        'input[name="mgc_preference_display_type"]:checked',
      ).val();

      if (selectedType === "neither") {
        // Create warning if it doesn't exist
        if ($gdprWarning.length === 0) {
          var warningHtml =
            '<div id="mgc-gdpr-warning" style="background: #fff3cd; padding: 12px; border-left: 4px solid #ffc107; margin-top: 10px; margin-bottom: 15px;">' +
            '<strong style="color: #856404;">⚠️ GDPR Compliance Reminder:</strong>' +
            '<p style="margin: 5px 0; color: #856404;">To remain GDPR compliant, you must add this shortcode to your footer or make it easily accessible on all pages:</p>' +
            '<code style="background: #fff; padding: 6px 10px; border-radius: 3px; display: inline-block; font-size: 14px; border: 1px solid #ffc107;">[mgc_cookie_preferences]</code>' +
            '<p style="margin: 10px 0 0 0; font-size: 12px; color: #856404;">GDPR requires users to have easy access to withdraw their consent at any time.</p>' +
            "</div>";

          $(warningHtml).insertAfter($preferenceRadios.last().parent());
          $gdprWarning = $("#mgc-gdpr-warning");
        }
        $gdprWarning.slideDown(200);
      } else {
        if ($gdprWarning.length > 0) {
          $gdprWarning.slideUp(200);
        }
      }
    }

    // Initialize warning on page load
    toggleGdprWarning();

    // Handle radio button changes
    $preferenceRadios.on("change", function () {
      toggleGdprWarning();
    });

    // Form validation
    $("form").on("submit", function (e) {
      // Validate hex colors
      var customColor = $("#mgc_custom_button_color").val();
      var customHoverColor = $("#mgc_custom_button_hover_color").val();
      var hexPattern = /^#[0-9A-F]{6}$/i;

      if (customColor && !hexPattern.test(customColor)) {
        alert("Custom Button Color must be a valid hex color (e.g., #3498db)");
        $("#mgc_custom_button_color").focus();
        return false;
      }

      if (customHoverColor && !hexPattern.test(customHoverColor)) {
        alert(
          "Custom Button Hover Color must be a valid hex color (e.g., #2980b9)",
        );
        $("#mgc_custom_button_hover_color").focus();
        return false;
      }

      return true;
    });
  });
})(jQuery);
