/**
 * BotFend Challenges & Honeypot Handler
 * Combined: JS challenge verification + honeypot trap interception
 */
(function () {
    'use strict';

    // ========== Honeypot Trap Handler ==========
    function initHoneypots() {
        var honeypots = document.querySelectorAll('[data-bot-trap="true"]');
        if (!honeypots.length || typeof botfendHoneypot === 'undefined') return;

        var nonce = botfendHoneypot.nonce;
        var beaconUrl = botfendHoneypot.beaconUrl;

        honeypots.forEach(function (honeypot) {
            honeypot.addEventListener('click', function (e) {
                e.preventDefault();
                if (navigator.sendBeacon) {
                    navigator.sendBeacon(
                        beaconUrl + '&honeypot_id=' + encodeURIComponent(this.id) + '&nonce=' + encodeURIComponent(nonce)
                    );
                }
                return false;
            });
        });
    }

    // ========== JS Challenge Handler ==========
    function initChallenge() {
        if (typeof botfendChallenge === 'undefined') return;

        // Progress bar animation
        var progress = 0;
        var progressInterval = setInterval(function () {
            progress += 1;
            var fill = document.getElementById('botfend-progress-fill');
            if (fill) {
                fill.style.width = progress + '%';
            }
            if (progress >= 100) {
                clearInterval(progressInterval);
            }
        }, 15);

        // Learn more toggle
        var learnMoreLink = document.getElementById('botfend-learn-more-link');
        if (learnMoreLink) {
            learnMoreLink.addEventListener('click', function (e) {
                e.preventDefault();
                var box = document.getElementById('botfend-explanation-box');
                if (box) {
                    box.classList.toggle('show');
                }
            });
        }

        // AJAX verification
        setTimeout(function () {
            var xhr = new XMLHttpRequest();
            xhr.open('POST', botfendChallenge.ajaxUrl, true);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.timeout = 10000;

            xhr.onload = function () {
                if (xhr.status === 200) {
                    try {
                        var response = JSON.parse(xhr.responseText);
                        if (response.success) {
                            window.location.reload();
                        } else {
                            showFallback();
                        }
                    } catch (e) {
                        showFallback();
                    }
                } else {
                    showFallback();
                }
            };

            xhr.onerror = showFallback;
            xhr.ontimeout = showFallback;

            var params =
                'action=botfend_verify_challenge' +
                '&token=' + encodeURIComponent(botfendChallenge.token) +
                '&nonce=' + encodeURIComponent(botfendChallenge.nonce);

            xhr.send(params);
        }, 1500);

        function showFallback() {
            var fallback = document.getElementById('botfend-challenge-fallback');
            if (fallback) {
                fallback.classList.remove('botfend-hidden');
            }
        }

        // Fallback: show after 5s
        setTimeout(showFallback, 5000);
    }

    // ========== Init ==========
    document.addEventListener('DOMContentLoaded', function () {
        initHoneypots();
        initChallenge();
    });
})();