jQuery(function ($) {
  // Generate a random UUID
  const random_uuid = chama_uuidv4();
  var id = 0;
  MicroModal.init({
    onShow: modal => console.info(`${modal.id} is shown`), // [1]
    onClose: modal => console.info(`${modal.id} is hidden`), // [2]
    openTrigger: 'data-custom-open', // [3]
    closeTrigger: 'data-custom-close', // [4]
    openClass: 'is-open', // [5]
    disableScroll: true, // [6]
    disableFocus: false, // [7]
    awaitOpenAnimation: false, // [8]
    awaitCloseAnimation: true, // [9]
    debugMode: false // [10]
  });
  //launch delete account modal
  $(".chama-delete-account").on("click", function (e) {
    e.preventDefault();


    $('#chama-modal-title span').text('Delete Account');
    $('#chama-modal-warning').text('Are you sure you want to delete your account? This will delete your account and its associated subscriptions. This action cannot be undone.');
    $('#chama-confirm-deletion').show();
    $('#chama-modal-close').addClass('chama_modal_btn_red_focus');
    // $('#chama-confirm-cancelation').data('id', id);
    MicroModal.show('chama-modal');
  });
  //launch cancel subscription modal
  $(".chama-cancel-subscription").on("click", function (e) {
    e.preventDefault();
    id = $(this).data('id');
    chama_reset_modal_buttons();
    $('#chama-modal-title span').text('Cancel Subscription');
    $('#chama-modal-warning').text('Are you sure you want to cancel your subscription?');
    $('#chama-confirm-cancelation').show();
    $('#chama-modal-close').addClass('chama_modal_btn_red_focus');
    // $('#chama-confirm-cancelation').data('id', id);
    MicroModal.show('chama-modal');
  });
  //launch pause subscription modal
  $(".chama-pause-subscription").on("click", function (e) {
    e.preventDefault();
    id = $(this).data('id');
    chama_reset_modal_buttons();
    $('#chama-modal-title span').text('Pause Subscription');
    $('#chama-modal-warning').text('Are you sure you want to pause your subscription?');
    $('#chama-confirm-pause').show();
    $('#chama-modal-close').addClass('chama_modal_btn_orange_focus');
    // $('#chama-confirm-cancelation').data('id', id);
    MicroModal.show('chama-modal');
  });
  //launch resume subscription modal
  $(".chama-resume-subscription").on("click", function (e) {
    e.preventDefault();
    id = $(this).data('id');
    chama_reset_modal_buttons();
    $('#chama-modal-title span').text('Resume Subscription');
    $('#chama-modal-warning').text('Do you want to resume your subscription?');
    $('#chama-confirm-resume').show();
    $('#chama-modal-close').addClass('chama_modal_btn_orange_focus');
    // $('#chama-confirm-cancelation').data('id', id);
    MicroModal.show('chama-modal');
  });
  //handle account deletion modal
  $("#chama-confirm-deletion").on("click", function (e) {
    $(this).addClass('disabled');
    $('.chama-button-spinner').removeClass('chama-hide');
    var base_rest_url = $('#chama_base_rest_url').val();
    var nonce = $('#_wpnonce').val();
    var logout_nonce = $('#chama_logout_nonce').val();
    chama_logout_nonce
    const form_data = {
      '_wpnonce': nonce
    };
    const { status, message } = $.ajax({
      url: base_rest_url + "v1/stripe/delete",
      type: "POST",
      data: form_data,
      success: function () {
        window.location.href = window.location.href + '?chama-logout=true&uid=' + random_uuid + '&chama-logout-nonce=' + logout_nonce;
      },
      error: function (data) {
        var response = JSON.parse(data.responseText);

        $('#chama-transaction-errors').html(response.message);
        $('#chama-transaction-errors').addClass('show');
        $('#chama-delete-account').removeClass('disabled');
        $('.chama-button-spinner ').addClass('chama-hide');
        return;
      }
    });

  });

  //handle cancel subscription modal
  $("#chama-confirm-cancelation").on("click", function (e) {
    $(this).addClass('disabled');
    $('.chama-button-spinner ').removeClass('chama-hide');
    var base_rest_url = $('#chama_base_rest_url').val();
    var nonce = $('#_wpnonce').val();
    const form_data = {
      '_wpnonce': nonce,
      'id': id,
    };
    const { status, message } = $.ajax({
      url: base_rest_url + "v1/stripe/cancel",
      type: "POST",
      data: form_data,
      success: function () {
        window.location.href = window.location.href + '&status=canceled';
      },
      error: function (data) {
        var response = JSON.parse(data.responseText);

        $('#chama-transaction-errors').html(response.message);
        $('#chama-transaction-errors').addClass('show');
        $('#chama-cancel-subscription').removeClass('disabled');
        $('.chama-button-spinner ').addClass('chama-hide');
        return;
      }
    });

  });
  //handle pause subscription modal
  $("#chama-confirm-pause").on("click", function (e) {
    $(this).addClass('disabled');
    $('.chama-button-spinner ').removeClass('chama-hide');
    var base_rest_url = $('#chama_base_rest_url').val();
    var nonce = $('#_wpnonce').val();
    const form_data = {
      '_wpnonce': nonce,
      'id': id,
    };
    const { status, message } = $.ajax({
      url: base_rest_url + "v1/stripe/pause",
      type: "POST",
      data: form_data,
      success: function () {
        window.location.href = window.location.href + '&status=paused';
      },
      error: function (data) {
        var response = JSON.parse(data.responseText);

        $('#chama-transaction-errors').html(response.message);
        $('#chama-transaction-errors').addClass('show');
        $('#chama-cancel-subscription').removeClass('disabled');
        $('.chama-button-spinner ').addClass('chama-hide');
        return;
      }
    });

  });

  //handle resume subscription modal
  $("#chama-confirm-resume").on("click", function (e) {
    $(this).addClass('disabled');
    $('.chama-button-spinner ').removeClass('chama-hide');
    var base_rest_url = $('#chama_base_rest_url').val();
    var nonce = $('#_wpnonce').val();
    const form_data = {
      '_wpnonce': nonce,
      'id': id,
    };
    const { status, message } = $.ajax({
      url: base_rest_url + "v1/stripe/resume",
      type: "POST",
      data: form_data,
      success: function () {
        window.location.href = window.location.href + '&status=resumed';
      },
      error: function (data) {
        var response = JSON.parse(data.responseText);

        $('#chama-transaction-errors').html(response.message);
        $('#chama-transaction-errors').addClass('show');
        $('#chama-cancel-subscription').removeClass('disabled');
        $('.chama-button-spinner ').addClass('chama-hide');
        return;
      }
    });

  });

  function chama_reset_modal_buttons() {
    $('#chama-confirm-pause').hide();
    $('#chama-confirm-resume').hide();
    $('#chama-confirm-cancelation').hide();
    $('#chama-modal-close').removeClass('chama_modal_btn_orange_focus');
    $('#chama-modal-close').removeClass('chama_modal_btn_red_focus');
  }
  function chama_uuidv4() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
      .replace(/[xy]/g, function (c) {
        const r = Math.random() * 16 | 0,
          v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
      });
  }


});


