/**
 * Scan Percentage Dynamic Updates
 * 
 * @package VMPFence
 * @since 1.0.0
 */

jQuery(document).ready(function($) {
    
    // Function to update scan percentage display
    function updateScanPercentageDisplay(status) {
        // Target ONLY the scan circle by specific ID (not firewall)
        const $scanItem = $('#vmpfence-scan-progress');
        
        // Only update if scan item exists
        if ($scanItem.length === 0) {
            // console.log('VMPFence: Scan progress item not found');
            return;
        }
        
        const $circle = $scanItem.find('.vmpfence-scan-progress-bar');
        const $text = $scanItem.find('.vmpfence-scan-text');
        const $message = $scanItem.find('.vmpfence-scan-status-message');
        
        // Update percentage
        const percentage = Math.round(status.percentage * 100);
        $circle.attr('stroke-dasharray', percentage + ',100');
        $circle.attr('stroke', status.color);
        $text.text(percentage + '%');
        $message.text(status.status_message);
        
        // Update CSS class
        $scanItem.removeClass('vmpfence-scan-excellent vmpfence-scan-good vmpfence-scan-fair vmpfence-scan-poor');
        $scanItem.addClass('vmpfence-scan-' + status.status_class);
    }
    
    // Function to refresh scan percentage
    function refreshScanPercentage() {
        $.post(ajaxurl, {
            action: 'vmpfence_get_scan_percentage',
            scan_action: 'get_status',
            nonce: vmpfence_scan_percentage.nonce
        }, function(response) {
            if (response.success) {
                updateScanPercentageDisplay(response.data);
            }
        });
    }
    
    // Auto-refresh when scan options might change
    $(document).on('vmpfence_scan_options_updated', function() {
        refreshScanPercentage();
    });
    
    // NOTE: Auto-refresh disabled - dashboard uses server-side rendering with new unified status calculator
    // Old auto-refresh caused percentage mismatch between main circle (old weighted system) and modal (new unified system)
    
    // Preview scan type percentages
    window.vmpfencePreviewScanType = function(scanType) {
        $.post(ajaxurl, {
            action: 'vmpfence_get_scan_percentage',
            scan_action: 'calculate_for_type',
            scan_type: scanType,
            nonce: vmpfence_scan_percentage.nonce
        }, function(response) {
            if (response.success) {
                // console.log('Scan Type Preview:', response.data);
                // You can use this for tooltips or previews
            }
        });
    };
});
