jQuery(function ($) {
  $("select#woocommerce_gratifypay_testmode")
    .on("change", function (event) {
      if ($(this).val() != "production") {
        $("input#woocommerce_gratifypay_prod-id").closest("tr").hide();
        $("input#woocommerce_gratifypay_prod-secret-key").closest("tr").hide();
        $("input#woocommerce_gratifypay_test-id").closest("tr").show();
        $("input#woocommerce_gratifypay_test-secret-key").closest("tr").show();
      } else {
        $("input#woocommerce_gratifypay_prod-id").closest("tr").show();
        $("input#woocommerce_gratifypay_prod-secret-key").closest("tr").show();
        $("input#woocommerce_gratifypay_test-id").closest("tr").hide();
        $("input#woocommerce_gratifypay_test-secret-key").closest("tr").hide();
      }
    })
    .trigger("change");

  $("input#woocommerce_gratifypay_show-info-on-category-pages")
    .on("change", function (event) {
      if ($(this).is(":checked")) {
        $("input#woocommerce_gratifypay_category-pages-info-text")
          .closest("tr")
          .show();
      } else {
        $("input#woocommerce_gratifypay_category-pages-info-text")
          .closest("tr")
          .hide();
      }
    })
    .trigger("change");

  $("input#woocommerce_gratifypay_show-info-on-product-pages")
    .on("change", function (event) {
      if ($(this).is(":checked")) {
        $("input#woocommerce_gratifypay_product-pages-info-text")
          .closest("tr")
          .show();
      } else {
        $("input#woocommerce_gratifypay_product-pages-info-text")
          .closest("tr")
          .hide();
      }
    })
    .trigger("change");

  $("a#reset-to-default-link").on("click", function (event) {
    $.ajax({
      type: "post",
      url: gratifypay_ajax_object.ajax_url,
      data: { action: "gratifypay_action" },
      success: function (response) {
        $.each(response, function (index, element) {
          var $el = $("#woocommerce_gratifypay_" + index);
          var attr_type = $el.attr("type");

          if (
            attr_type == "text" ||
            attr_type == "textarea" ||
            attr_type == "number" ||
            $el.is("select")
          ) {
            $el.val(element);
          } else if (attr_type == "checkbox") {
            $el.prop("checked", element == "yes");
          } else {
            tinymce.get(index.replace(/-/g, "")).setContent(element);
          }
        });
        alert(
          'Customisations have now been reset to defaults. Please review and click "Save Changes" to accept the new values.'
        );
      },
    });
  });

  $("a.gratifypay-notice-dismiss").on("click", function (event) {
    var review_link;
    if (this.hasAttribute("href")) {
      review_link = $(this).attr("href");
      event.preventDefault();
    }
    var noticeClass = $(this).attr("class");
    $.ajax({
      type: "post",
      url: gratifypay_ajax_object.ajax_url,
      data: { action: "gratifypay_notice_dismiss_action" },
      success: function (response) {
        if (response) {
          if (noticeClass.includes("gratifypay_rate_redirect")) {
            $(".gratifypay-rating-notice").hide();
            window.open(review_link, "_blank");
          } else {
            location.reload();
          }
        }
      },
    });
  });

  $(".wc-stripe-connection-test").on("click", function (event) {
    event.preventDefault();
  });

  const THEMES = {
    blue: {
      assets: {
        "[AMOUNT]": "$25",
        "[NUM_PAYMENTS]": "4",
        "[GRATIFY_BADGE]": `<img style="margin-top:-5px;vertical-align:middle;display:inline-block;border:none;" src="https://assets.gratifypay.com/img/gratify-badge-blue.svg" width="80" height="30" alt="Gratify Pay" />`,
      },
      default:
        '<span class="shadow-lg" style="background:#f5f6f7;padding:8px;border-radius: 5px;color:#3c4257;font-size: 13px; display: inline-block; height:45px;margin-top: 10px;">or [NUM_PAYMENTS] interest-free instalments of <b><strong>[AMOUNT]</strong></b> by [GRATIFY_BADGE]</span>',
    },
    charcoal: {
      assets: {
        "[AMOUNT]": "$25",
        "[NUM_PAYMENTS]": "4",
        "[GRATIFY_BADGE]": `<img style="margin-top:-5px;vertical-align:middle;display:inline-block;border:none;" src="https://assets.gratifypay.com/img/gratify-badge-charcoal.svg" width="80" height="30" alt="Gratify Pay" />`,
      },
      default:
        '<span class="shadow-lg" style="background:#f5f6f7;padding:8px;border-radius: 5px;color:#3c4257;font-size: 13px; display: inline-block; height:45px;margin-top: 10px;">or [NUM_PAYMENTS] interest-free instalments of <b><strong>[AMOUNT]</strong></b> by [GRATIFY_BADGE]</span>',
    },
    white: {
      assets: {
        "[AMOUNT]": "$25",
        "[NUM_PAYMENTS]": "4",
        "[GRATIFY_BADGE]": `<img style="margin-top:-5px;vertical-align:middle;display:inline-block;border:none;" src="https://assets.gratifypay.com/img/gratify-badge-white.svg" width="80" height="30" alt="Gratify Pay" />`,
      },
      default:
        '<span class="shadow-lg" style="background:#3c4257;padding:8px;border-radius: 5px;color:#f5f6f7;font-size: 13px; display: inline-block; height:45px;margin-top: 10px;">or [NUM_PAYMENTS] interest-free instalments of <b><strong>[AMOUNT]</strong></b> by [GRATIFY_BADGE]</span>',
    },
  };

  function processText(cc, selectedtheme) {
    let x = cc;
    for (let i in THEMES[selectedtheme].assets) {
      x = x.replaceAll(i, THEMES[selectedtheme].assets[i]);
    }
    x = x.replaceAll("script", "");
    switch (selectedtheme) {
      case "white":
        x = x.replaceAll("background:#f5f6f7", "background:#3c4257");
        x = x.replaceAll("color:#3c4257", "color:#f5f6f7");
        break;
      default:
        x = x.replaceAll("background:#3c4257", "background:#f5f6f7");
        x = x.replaceAll("color:#f5f6f7", "color:#3c4257");
    }
    console.log(x);
    return x;
  }

  function switchTheme(cc, selectedtheme) {
    let x = cc;
    switch (selectedtheme) {
      case "white":
        x = x.replaceAll("background:#f5f6f7", "background:#3c4257");
        x = x.replaceAll("color:#3c4257", "color:#f5f6f7");
        break;
      default:
        x = x.replaceAll("background:#3c4257", "background:#f5f6f7");
        x = x.replaceAll("color:#f5f6f7", "color:#3c4257");
    }
    console.log(x);
    return x;
  }

  function getDefaultText(selectedtheme) {
    return THEMES[selectedtheme].default;
  }

  $(document).ready(function () {
    let testmode = $("#woocommerce_gratify_pay_testmode").val();
    if (testmode != "production") {
      $("input#woocommerce_gratify_pay_prod-id").closest(".wrapperbox").hide();
      $("input#woocommerce_gratify_pay_prod-secret-key")
        .closest(".wrapperbox")
        .hide();
      $("input#woocommerce_gratify_pay_test-id").closest(".wrapperbox").show();
      $("input#woocommerce_gratify_pay_test-secret-key")
        .closest(".wrapperbox")
        .show();
    } else {
      $("input#woocommerce_gratify_pay_prod-id").closest(".wrapperbox").show();
      $("input#woocommerce_gratify_pay_prod-secret-key")
        .closest(".wrapperbox")
        .show();
      $("input#woocommerce_gratify_pay_test-id").closest(".wrapperbox").hide();
      $("input#woocommerce_gratify_pay_test-secret-key")
        .closest(".wrapperbox")
        .hide();
    }

    $(".testConnectionBtn").on("click", function (e) {
      e.preventDefault();
      $(".woocommerce-save-button").trigger("click");
    });

    const categoryTA = $("#woocommerce_gratify_pay_category-pages-info-text");
    const productTA = $("#woocommerce_gratify_pay_product-pages-info-text");
    const prodvarTA = $("#woocommerce_gratify_pay_product-variant-info-text");
    const cartTA = $("#woocommerce_gratify_pay_cart-page-info-text");
    const theme = $("#woocommerce_gratify_pay_colortheme");

    const cp = $("#p-woocommerce_gratify_pay_category-pages-info-text");
    const pp = $("#p-woocommerce_gratify_pay_product-pages-info-text");
    const pvp = $("#p-woocommerce_gratify_pay_product-variant-info-text");
    const ctp = $("#p-woocommerce_gratify_pay_cart-page-info-text");

    $(".restoreDefBtn").on("click", function (e) {
      e.preventDefault();
      if (window.confirm("Restore Defaults?")) {
        categoryTA.val(getDefaultText(theme.val()));
        productTA.val(getDefaultText(theme.val()));
        prodvarTA.val(getDefaultText(theme.val()));
        cartTA.val(getDefaultText(theme.val()));
        $(".show-info-on-category-pages").html(
          processText(categoryTA.val(), theme.val())
        );
        $(".show-info-on-product-pages").html(
          processText(productTA.val(), theme.val())
        );
        $(".show-info-on-product-variant").html(
          processText(prodvarTA.val(), theme.val())
        );
        $(".show-info-on-cart-page").html(
          processText(cartTA.val(), theme.val())
        );
      }
    });

    console.log(
      "h1",
      processText(categoryTA.val(), theme.val()),
      productTA.val(),
      prodvarTA.val(),
      cartTA.val(),
      theme.val()
    );
    $(".show-info-on-category-pages").html(
      processText(categoryTA.val(), theme.val())
    );
    $(".show-info-on-product-pages").html(
      processText(productTA.val(), theme.val())
    );
    $(".show-info-on-product-variant").html(
      processText(prodvarTA.val(), theme.val())
    );
    $(".show-info-on-cart-page").html(processText(cartTA.val(), theme.val()));

    theme.on("change", function (e) {
      categoryTA.val(switchTheme(categoryTA.val(), $(this).val()));
      productTA.val(switchTheme(productTA.val(), $(this).val()));
      prodvarTA.val(switchTheme(prodvarTA.val(), $(this).val()));
      cartTA.val(switchTheme(cartTA.val(), $(this).val()));

      $(".show-info-on-category-pages").html(
        processText(categoryTA.val(), $(this).val())
      );
      $(".show-info-on-product-pages").html(
        processText(productTA.val(), $(this).val())
      );
      $(".show-info-on-product-variant").html(
        processText(prodvarTA.val(), $(this).val())
      );
      $(".show-info-on-cart-page").html(
        processText(cartTA.val(), $(this).val())
      );
    });

    $(".edit-show-info-on-category-pages").on("click", function (e) {
      e.preventDefault();
      $(this).html("Done");
      $(this).removeClass("border-gray-300");
      $(this).addClass("border-gray-600");

      $(".edit-show-info-on-product-pages").html("Edit");
      $(".edit-show-info-on-product-pages").removeClass("border-gray-600");
      $(".edit-show-info-on-product-pages").addClass("border-gray-300");

      $(".edit-show-info-on-product-variant").html("Edit");
      $(".edit-show-info-on-product-variant").removeClass("border-gray-600");
      $(".edit-show-info-on-product-variant").addClass("border-gray-300");

      $(".edit-show-info-on-cart-page").html("Edit");
      $(".edit-show-info-on-cart-page").removeClass("border-gray-600");
      $(".edit-show-info-on-cart-page").addClass("border-gray-300");
      if (categoryTA.is(":hidden")) {
        categoryTA.show();
        categoryTA.focus();
        cp.show();
        pp.hide();
        pvp.hide();
        ctp.hide();
        productTA.hide();
        prodvarTA.hide();
        cartTA.hide();
      } else {
        categoryTA.hide();
        cp.hide();
        $(this).html("Edit");
        $(this).addClass("border-gray-300");
        $(this).removeClass("border-gray-600");
      }
    });

    $(".edit-show-info-on-product-pages").on("click", function (e) {
      e.preventDefault();
      $(this).html("Done");
      $(this).removeClass("border-gray-300");
      $(this).addClass("border-gray-600");

      $(".edit-show-info-on-category-pages").html("Edit");
      $(".edit-show-info-on-category-pages").removeClass("border-gray-600");
      $(".edit-show-info-on-category-pages").addClass("border-gray-300");

      $(".edit-show-info-on-product-variant").html("Edit");
      $(".edit-show-info-on-product-variant").removeClass("border-gray-600");
      $(".edit-show-info-on-product-variant").addClass("border-gray-300");

      $(".edit-show-info-on-cart-page").html("Edit");
      $(".edit-show-info-on-cart-page").removeClass("border-gray-600");
      $(".edit-show-info-on-cart-page").addClass("border-gray-300");
      if (productTA.is(":hidden")) {
        productTA.show();
        productTA.focus();
        pp.show();
        cp.hide();
        pvp.hide();
        ctp.hide();
        categoryTA.hide();
        prodvarTA.hide();
        cartTA.hide();
      } else {
        productTA.hide();
        pp.hide();
        $(this).html("Edit");
        $(this).addClass("border-gray-300");
        $(this).removeClass("border-gray-600");
      }
    });

    $(".edit-show-info-on-product-variant").on("click", function (e) {
      e.preventDefault();
      $(this).html("Done");
      $(this).removeClass("border-gray-300");
      $(this).addClass("border-gray-600");

      $(".edit-show-info-on-category-pages").html("Edit");
      $(".edit-show-info-on-category-pages").removeClass("border-gray-600");
      $(".edit-show-info-on-category-pages").addClass("border-gray-300");

      $(".edit-show-info-on-product-pages").html("Edit");
      $(".edit-show-info-on-product-pages").removeClass("border-gray-600");
      $(".edit-show-info-on-product-pages").addClass("border-gray-300");

      $(".edit-show-info-on-cart-page").html("Edit");
      $(".edit-show-info-on-cart-page").removeClass("border-gray-600");
      $(".edit-show-info-on-cart-page").addClass("border-gray-300");

      if (prodvarTA.is(":hidden")) {
        prodvarTA.show();
        prodvarTA.focus();
        pvp.show();
        pp.hide();
        cp.hide();
        ctp.hide();
        productTA.hide();
        categoryTA.hide();
        cartTA.hide();
      } else {
        prodvarTA.hide();
        pvp.hide();
        $(this).html("Edit");
        $(this).addClass("border-gray-300");
        $(this).removeClass("border-gray-600");
      }
    });

    $(".edit-show-info-on-cart-page").on("click", function (e) {
      e.preventDefault();
      $(this).html("Done");
      $(this).removeClass("border-gray-300");
      $(this).addClass("border-gray-600");

      $(".edit-show-info-on-category-pages").html("Edit");
      $(".edit-show-info-on-category-pages").removeClass("border-gray-600");
      $(".edit-show-info-on-category-pages").addClass("border-gray-300");

      $(".edit-show-info-on-product-pages").html("Edit");
      $(".edit-show-info-on-product-pages").removeClass("border-gray-600");
      $(".edit-show-info-on-product-pages").addClass("border-gray-300");

      $(".edit-show-info-on-product-variant").html("Edit");
      $(".edit-show-info-on-product-variant").removeClass("border-gray-600");
      $(".edit-show-info-on-product-variant").addClass("border-gray-300");

      if (cartTA.is(":hidden")) {
        cartTA.show();
        cartTA.focus();
        ctp.show();
        pvp.hide();
        pp.hide();
        cp.hide();
        categoryTA.hide();
        productTA.hide();
        prodvarTA.hide();
      } else {
        cartTA.hide();
        ctp.hide();
        $(this).html("Edit");
        $(this).addClass("border-gray-300");
        $(this).removeClass("border-gray-600");
      }
    });

    categoryTA.on("input", function (e) {
      $(".show-info-on-category-pages").html(
        processText($(this).val(), theme.val())
      );
    });
    productTA.on("input", function (e) {
      $(".show-info-on-product-pages").html(
        processText($(this).val(), theme.val())
      );
    });
    prodvarTA.on("input", function (e) {
      $(".show-info-on-product-variant").html(
        processText($(this).val(), theme.val())
      );
    });
    cartTA.on("input", function (e) {
      $(".show-info-on-cart-page").html(
        processText($(this).val(), theme.val())
      );
    });
  });

  $("#woocommerce_gratify_pay_testmode").on("change", function (event) {
    if ($(this).val() != "production") {
      $("input#woocommerce_gratify_pay_prod-id").closest(".wrapperbox").hide();
      $("input#woocommerce_gratify_pay_prod-secret-key")
        .closest(".wrapperbox")
        .hide();
      $("input#woocommerce_gratify_pay_test-id").closest(".wrapperbox").show();
      $("input#woocommerce_gratify_pay_test-secret-key")
        .closest(".wrapperbox")
        .show();
    } else {
      $("input#woocommerce_gratify_pay_prod-id").closest(".wrapperbox").show();
      $("input#woocommerce_gratify_pay_prod-secret-key")
        .closest(".wrapperbox")
        .show();
      $("input#woocommerce_gratify_pay_test-id").closest(".wrapperbox").hide();
      $("input#woocommerce_gratify_pay_test-secret-key")
        .closest(".wrapperbox")
        .hide();
    }
  });

  // $('.woocommerce-save-button').on('click', function (event) {
  //   event.preventDefault();
  // });
});
