(function($) {
  function errorSwall() {
    Swal.fire({
      icon: "error",
      title: "Oops...",
      text: "Something went wrong!"
    });
  }

  function importBidSpirit(jTarget) {
    Swal.fire({
      title: "Do you want to update now?",
      text: "We get data from Bidspirit and added it to sync table.",
      footer: "",
      icon: "success",
      showCancelButton: true,
      confirmButtonColor: "#3085d6",
      cancelButtonColor: "#d33",
      confirmButtonText: "Ok"
    }).then(result => {
      if (result.value) {
        jTarget.addClass("loader");
        ajaxQuery(jTarget, "importData", importData);
      }
    });
  }

  function importData(jTarget) {
    var timerInterval;
    Swal.fire({
      title: "Import completed!",
      icon: "success",
      html: "The page will reload after <b></b> milliseconds.",
      timer: 1500,
      timerProgressBar: false,
      onBeforeOpen: () => {
        Swal.showLoading();
        timerInterval = setInterval(() => {
          const content = Swal.getContent();
          if (content) {
            const b = content.querySelector("b");
            if (b) {
              b.textContent = Swal.getTimerLeft();
            }
          }
        }, 100);
      },
      onClose: () => {
        clearInterval(timerInterval);
      }
    }).then(result => {
      if (result.dismiss === Swal.DismissReason.timer) {
        location = location.href;
      }
    });
  }

  function ajaxQuery(jTarget, doAction, callback) {
    let args = {
      action: "do_sync_now",
      do: doAction,
      security: window.wp_nonce,
      limit: $("#bs-count-lot").val(),
      type: Boolean(parseInt($("#auction_type").val()))
    };
    $.ajax({
      type: "post",
      dataType: "json",
      url: ajaxurl,
      data: args
    })
      .done(function(response) {
        console.log(bitSpiritSyncNowText);
        if (response.success) {
          callback(jTarget);
        } else {
          errorSwall();
        }
      })
      .fail(function(response) {
        errorSwall();
      })
      .always(function() {
        jTarget.removeClass("loader");
      });
  }

  function syncNow(target) {
    target.on("click", function(event) {
      console.log("sync data");
      event.preventDefault();

      var $this = $(this);

      Swal.fire({
        title: "Start import?",
        text: "You won't be able to revert this!",
        icon: "warning",
        showCancelButton: true,
        showLoaderOnConfirm: true,
        confirmButtonColor: "#3085d6",
        cancelButtonColor: "#d33",
        confirmButtonText: "Yes!"
      }).then(result => {
        console.log(result);
        if (result.value) {
          $this.addClass("loader");
          ajaxQuery($this, "importData", importData);
        }
      });
    });
  }

  function getDataNow(target) {
    target.on("click", function(event) {
      console.log("get data");
      event.preventDefault();

      var $this = $(this);

      Swal.fire({
        title: "Start import?",
        text: "You won't be able to revert this!",
        icon: "warning",
        showCancelButton: true,
        showLoaderOnConfirm: true,
        confirmButtonColor: "#3085d6",
        cancelButtonColor: "#d33",
        confirmButtonText: "Yes!"
      }).then(result => {
        console.log(result);
        if (result.value) {
          $this.addClass("loader");
          ajaxQuery($this, "importBidSpirit", importData);
        }
      });
    });
  }

  function metaSlider() {
    if ($(".swiper-container").length) {
      new Swiper(".swiper-container", {
        loop: true,
        centeredSlides: false,
        slidesPerView: 2,
        spaceBetween: 5,
        setWrapperSize: true,
        pagination: {
          el: ".swiper-pagination"
        },
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev"
        },
        scrollbar: {
          el: ".swiper-scrollbar"
        }
      });
    }
  }

  $(document).ready(function() {
    var syncBtn = $("#bs-sync-now"),
      getBtn = $("#bs-get-now");

    window.bitSpiritSyncNowText = syncBtn.text();
    getDataNow(getBtn);
    syncNow(syncBtn);

    metaSlider();
  });
})(jQuery);
