'use strict';

jQuery(function ($) {
  if (!window.SPAATGDeactivateFeedback) {
    return;
  }

  var config = window.SPAATGDeactivateFeedback;
  var deactivateURL = $('#' + config.deactivateLinkId);
  var formContainer = $('#' + config.formId);
  var deactivated = true;

  if (!deactivateURL.length || !formContainer.length) {
    return;
  }

  function submitFeedback(data, redirectUrl) {
    data.action = 'spaatg_deactivate_plugin';
    data.security = config.nonce;
    data.dataType = 'json';
    data['keep-settings'] = formContainer.find('#shortpixel-keep-settings:checked').length;

    formContainer.addClass('process-response');
    formContainer.find('.deactivating-spinner').fadeIn();

    $.post(ajaxurl, data, function () {
      window.location.href = redirectUrl;
    });
  }

  deactivateURL.on('click', function (event) {
    event.preventDefault();

    var redirectUrl = deactivateURL.attr('href');
    $('body').toggleClass('shortpixel-deactivate-form-active');
    formContainer.fadeIn({
      complete: function () {
        var offset = formContainer.offset();
        if (offset.top < 50) {
          $(this).parent().css('top', (50 - offset.top) + 'px');
        }
        $('html,body').animate({ scrollTop: Math.max(0, offset.top - 50) });
      }
    });

    formContainer.html(config.html);

    formContainer.off('.spaatgFeedback');

    formContainer.on('change.spaatgFeedback', 'input[type=radio]', function () {
      var detailsLabel = formContainer.find('#shortpixel-deactivate-details-label strong');
      var anonymousLabel = formContainer.find('label[for="anonymous"]')[0];
      var submitSpan = formContainer.find('#shortpixel-deactivate-submit-form span')[0];
      var value = formContainer.find('input[name="shortpixel-deactivate-reason"]:checked').val();
      var commentBox = formContainer.find('textarea[name="shortpixel-deactivate-details"]');
      var detailText = config.detailsStrings[value] || '';

      if (detailText === '') {
        detailsLabel.css('visibility', 'hidden');
        commentBox.css('visibility', 'hidden');
      } else {
        detailsLabel.css('visibility', 'visible');
        commentBox.css('visibility', 'visible');
      }

      detailsLabel.text(detailText);
      anonymousLabel.style.visibility = 'visible';
      submitSpan.style.display = 'inline-block';

      if (deactivated) {
        deactivated = false;
        $('#shortpixel-deactivate-submit-form').removeAttr('disabled');
      }
    });

    formContainer.on('click.spaatgFeedback', '#shortpixel-deactivate-submit-form', function (e) {
      e.preventDefault();

      if (deactivated) {
        window.location.href = redirectUrl;
        return;
      }

      var data = {
        reason: formContainer.find('input[name="shortpixel-deactivate-reason"]:checked').val(),
        details: formContainer.find('#shortpixel-deactivate-details').val(),
        anonymous: formContainer.find('#anonymous').is(':checked') ? 1 : 0
      };

      submitFeedback(data, redirectUrl);
    });

    $('.shortpixel-deactivate-form-bg').off('.spaatgFeedback').on('click.spaatgFeedback', function () {
      formContainer.fadeOut();
      $('body').removeClass('shortpixel-deactivate-form-active');
    });
  });
});
