(function ($) {
    'use strict';

    if (typeof window.shieldforgeAjax === 'undefined') {
        return;
    }

    var SF = window.shieldforgeAjax;

    function flash($el, ok) {
        $el.css('transition', 'background-color .4s');
        $el.css('background-color', ok ? '#e7f7ec' : '#fde7e7');
        setTimeout(function () { $el.css('background-color', ''); }, 1200);
    }

    $(document).on('click', '.sf-quick-unblock', function (e) {
        e.preventDefault();
        var $btn = $(this);
        var ip   = $btn.data('ip');
        if (!ip) { return; }
        if (!window.confirm(SF.i18n.confirmUnblock)) { return; }

        $btn.prop('disabled', true);
        $.post(SF.url, {
            action: 'shieldforge_unblock_ip',
            nonce:  SF.nonce,
            ip:     ip
        }).done(function (resp) {
            if (resp && resp.success) {
                var $row = $btn.closest('tr');
                flash($row, true);
                setTimeout(function () { $row.fadeOut(220, function () { $row.remove(); }); }, 200);
            } else {
                flash($btn.closest('tr'), false);
                window.alert((resp && resp.data && resp.data.message) || SF.i18n.genericError);
                $btn.prop('disabled', false);
            }
        }).fail(function () {
            flash($btn.closest('tr'), false);
            window.alert(SF.i18n.genericError);
            $btn.prop('disabled', false);
        });
    });

    $(document).on('click', '.sf-quick-block', function (e) {
        e.preventDefault();
        var $btn = $(this);
        var ip   = $btn.data('ip');
        if (!ip) { return; }
        if (!window.confirm(SF.i18n.confirmBlock + '\n\n' + ip)) { return; }

        $btn.prop('disabled', true);
        $.post(SF.url, {
            action:  'shieldforge_quick_block',
            nonce:   SF.nonce,
            ip:      ip,
            minutes: $btn.data('minutes') || 60
        }).done(function (resp) {
            if (resp && resp.success) {
                flash($btn.closest('tr'), true);
                $btn.text('Blocked');
            } else {
                flash($btn.closest('tr'), false);
                window.alert((resp && resp.data && resp.data.message) || SF.i18n.genericError);
                $btn.prop('disabled', false);
            }
        }).fail(function () {
            flash($btn.closest('tr'), false);
            window.alert(SF.i18n.genericError);
            $btn.prop('disabled', false);
        });
    });

    $(document).on('click', '.sf-copy-btn', function (e) {
        e.preventDefault();
        var $btn    = $(this);
        var target  = $btn.data('target');
        var $source = target ? $(target) : null;
        if (!$source || !$source.length) { return; }
        var text = $source.is('textarea, input') ? $source.val() : $source.text();
        if (navigator.clipboard && navigator.clipboard.writeText) {
            navigator.clipboard.writeText(text).then(function () {
                var orig = $btn.text();
                $btn.text('Copied');
                setTimeout(function () { $btn.text(orig); }, 1200);
            });
        } else {
            $source.trigger('select');
            try { document.execCommand('copy'); } catch (err) { /* no-op */ }
        }
    });

})(jQuery);
