/**
 * PEWIT Labs — deactivation / delete confirmation (plugins.php).
 */
(function ($) {
  'use strict';

  var cfg = window.AISB_DeactivatePrompt || {};
  if (!cfg.pluginId || !cfg.ajax) {
    return;
  }

  var encodedId = encodeURIComponent(cfg.pluginId);

  function targetsPlugin(href) {
    if (!href) {
      return false;
    }
    return href.indexOf('plugin=' + encodedId) !== -1
      || href.indexOf('plugin=' + cfg.pluginId) !== -1
      || href.indexOf(encodeURIComponent(cfg.pluginId)) !== -1;
  }

  function overlay(title, message, onKeep, onPurge) {
    var $el = $('#pewit-deactivate-dialog');
    if (!$el.length) {
      $el = $(
        '<div id="pewit-deactivate-dialog" class="pewit-deactivate-dialog" role="dialog" aria-modal="true">' +
          '<div class="pewit-deactivate-dialog__backdrop"></div>' +
          '<div class="pewit-deactivate-dialog__panel">' +
            '<h2 class="pewit-deactivate-dialog__title"></h2>' +
            '<p class="pewit-deactivate-dialog__message"></p>' +
            '<div class="pewit-deactivate-dialog__actions"></div>' +
          '</div>' +
        '</div>'
      );
      $('body').append($el);
      $el.on('click', '.pewit-deactivate-dialog__backdrop, .pewit-deact-cancel', function () {
        $el.hide();
      });
    }
    $el.find('.pewit-deactivate-dialog__title').text(title);
    $el.find('.pewit-deactivate-dialog__message').text(message);
    var $actions = $el.find('.pewit-deactivate-dialog__actions').empty();

    $('<button type="button" class="button button-primary pewit-deact-keep"></button>')
      .text(cfg.i18n.keep || 'Keep settings & data')
      .appendTo($actions)
      .on('click', function () {
        $el.hide();
        onKeep();
      });

    $('<button type="button" class="button pewit-deact-purge"></button>')
      .text(cfg.i18n.purge || 'Remove all plugin data')
      .appendTo($actions)
      .on('click', function () {
        $el.hide();
        onPurge();
      });

    $('<button type="button" class="button pewit-deact-cancel"></button>')
      .text(cfg.i18n.cancel || 'Cancel')
      .appendTo($actions);

    if (cfg.i18n.keepHint) {
      $('<p class="pewit-deactivate-dialog__hint"></p>').text(cfg.i18n.keepHint).insertAfter($actions);
    }
    if (cfg.i18n.purgeHint) {
      $('<p class="pewit-deactivate-dialog__hint pewit-deactivate-dialog__hint-purge"></p>').text(cfg.i18n.purgeHint).insertAfter($actions);
    }
    $el.show();
  }

  function go(url) {
    window.location.href = url;
  }

  function purgeThenGo(url) {
    $.post(cfg.ajax, {
      action: cfg.action,
      nonce: cfg.nonce
    }).always(function () {
      go(url);
    });
  }

  function intercept(e, title) {
    var href = $(this).attr('href');
    if (!targetsPlugin(href)) {
      return;
    }
    e.preventDefault();
    overlay(
      title,
      cfg.i18n.message || 'What should happen to plugin data?',
      function () { go(href); },
      function () { purgeThenGo(href); }
    );
  }

  $(document).on('click', 'a[href*="action=deactivate"]', function (e) {
    intercept.call(this, e, cfg.i18n.title || ('Deactivate ' + (cfg.pluginName || 'plugin')));
  });

  $(document).on('click', 'a[href*="action=delete"]', function (e) {
    intercept.call(this, e, cfg.i18n.deleteTitle || ('Delete ' + (cfg.pluginName || 'plugin')));
  });
})(jQuery);
