#!/usr/bin/env python3
"""
Test the progress bar integration in the TUI
"""

import sys
sys.path.append('.')

from analyzer.analysis import EthernetAnalyzer
from analyzer.analysis.background_analyzer import ParsingProgress
from analyzer.tui.textual.widgets.progress_bar import ParsingProgressBar
import time

def test_progress_bar_widget():
    """Test the progress bar widget directly"""
    print("=== Testing Progress Bar Widget ===")
    
    # Create progress bar widget
    progress_bar = ParsingProgressBar()
    
    # Test initial state
    print(f"Initial visibility: {progress_bar.is_visible}")
    print(f"Initial complete: {progress_bar.is_complete}")
    
    # Test starting progress
    total_packets = 2048
    progress_bar.start_parsing(total_packets)
    print(f"After start - visible: {progress_bar.is_visible}, total: {progress_bar.total_packets}")
    
    # Test progress updates
    for i in range(0, total_packets + 1, 200):
        pps = 1000.0 if i > 0 else 0.0
        eta = (total_packets - i) / pps if pps > 0 else 0.0
        progress_bar.update_progress(i, total_packets, pps, eta)
        print(f"Progress: {i}/{total_packets} ({progress_bar.progress:.1f}%) - {pps:.0f} pkt/s")
        
        if i >= total_packets:
            break
    
    # Test completion
    progress_bar.complete_parsing()
    print(f"Complete: {progress_bar.is_complete}")
    
    # Test error handling
    progress_bar.show_error("Test error message")
    print(f"Error: {progress_bar.error_message}")
    
    print("✅ Progress bar widget test completed")

def test_parsing_progress_dataclass():
    """Test the ParsingProgress dataclass"""
    print("\\n=== Testing ParsingProgress Dataclass ===")
    
    # Create progress object
    progress = ParsingProgress(
        total_packets=1000,
        processed_packets=250,
        percent_complete=25.0,
        packets_per_second=500.0,
        elapsed_time=0.5,
        estimated_time_remaining=1.5
    )
    
    print(f"Progress: {progress.processed_packets}/{progress.total_packets} ({progress.percent_complete:.1f}%)")
    print(f"Rate: {progress.packets_per_second:.0f} pkt/s")
    print(f"ETA: {progress.estimated_time_remaining:.1f}s")
    print(f"Complete: {progress.is_complete}")
    
    # Test completed state
    progress.is_complete = True
    print(f"Marked complete: {progress.is_complete}")
    
    print("✅ ParsingProgress dataclass test completed")

def test_background_analyzer_progress():
    """Test progress callback with background analyzer"""
    print("\\n=== Testing Background Analyzer Progress ===")
    
    progress_updates = []
    
    def progress_callback(progress):
        progress_updates.append({
            'processed': progress.processed_packets,
            'total': progress.total_packets,
            'percent': progress.percent_complete,
            'pps': progress.packets_per_second,
            'complete': progress.is_complete
        })
        print(f"Progress callback: {progress.processed_packets}/{progress.total_packets} ({progress.percent_complete:.1f}%)")
    
    # Create analyzer with progress callback
    analyzer = EthernetAnalyzer(enable_realtime=False)
    
    print("✅ Background analyzer progress callback setup completed")
    print(f"Collected {len(progress_updates)} progress updates")

if __name__ == "__main__":
    test_progress_bar_widget()
    test_parsing_progress_dataclass()
    test_background_analyzer_progress()
    
    print("\\n🎉 All progress bar tests completed successfully!")