"""
Tests d'intégration pour Phase 2 - Mode Enrichi.
Vérifie que la détection d'éléments UI fonctionne correctement.
"""

import sys
import numpy as np
import cv2
from pathlib import Path

# Ajouter le chemin du module
sys.path.insert(0, str(Path(__file__).parent))

from geniusia2.core import (
    UIElementDetector,
    EnrichedScreenCapture,
    WindowInfo,
    BoundingBox
)
from geniusia2.core.logger import Logger


def test_bounding_box():
    """Test de la classe BoundingBox."""
    print("\n" + "=" * 70)
    print("TEST 1: BoundingBox")
    print("=" * 70)
    
    bbox1 = BoundingBox(10, 10, 50, 50, confidence=0.9, source="test")
    bbox2 = BoundingBox(30, 30, 70, 70, confidence=0.8, source="test")
    
    print(f"✓ BBox1 créée: ({bbox1.x1},{bbox1.y1},{bbox1.x2},{bbox1.y2})")
    print(f"  Aire: {bbox1.area()}")
    print(f"  Centre: {bbox1.center()}")
    print(f"  Confiance: {bbox1.confidence}")
    
    print(f"✓ BBox2 créée: ({bbox2.x1},{bbox2.y1},{bbox2.x2},{bbox2.y2})")
    
    iou = bbox1.iou(bbox2)
    print(f"✓ IoU calculé: {iou:.3f}")
    
    assert 0 <= iou <= 1, "IoU doit être entre 0 et 1"
    assert bbox1.area() > 0, "L'aire doit être positive"
    
    print("✓ Test BoundingBox réussi")


def test_region_proposer():
    """Test du RegionProposer."""
    print("\n" + "=" * 70)
    print("TEST 2: RegionProposer")
    print("=" * 70)
    
    from geniusia2.core.ui_element_detector import RegionProposer
    
    # Créer un screenshot synthétique avec des rectangles
    screenshot = np.ones((600, 800, 3), dtype=np.uint8) * 255
    
    # Dessiner quelques rectangles (simulent des boutons)
    cv2.rectangle(screenshot, (100, 100), (200, 150), (0, 0, 0), 2)
    cv2.rectangle(screenshot, (300, 200), (400, 250), (0, 0, 0), 2)
    cv2.putText(screenshot, "Valider", (110, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
    
    # Créer le proposer
    proposer = RegionProposer(config={
        "use_text_detection": True,
        "use_rectangle_detection": True,
        "use_vlm_detection": False
    })
    
    print(f"✓ RegionProposer créé")
    print(f"  use_text_detection: {proposer.use_text_detection}")
    print(f"  use_rectangle_detection: {proposer.use_rectangle_detection}")
    
    # Proposer des régions
    window_info = WindowInfo(
        app_name="test_app",
        window_title="Test Window",
        screen_resolution=(800, 600)
    )
    
    regions = proposer.propose_regions(screenshot, window_info)
    
    print(f"✓ Régions proposées: {len(regions)}")
    for i, region in enumerate(regions[:5]):  # Afficher les 5 premières
        print(f"  Région {i+1}: ({region.x1},{region.y1},{region.x2},{region.y2}) - {region.source}")
    
    assert len(regions) >= 0, "Doit retourner une liste (peut être vide)"
    
    print("✓ Test RegionProposer réussi")


def test_ui_element_detector():
    """Test du UIElementDetector complet."""
    print("\n" + "=" * 70)
    print("TEST 3: UIElementDetector")
    print("=" * 70)
    
    logger = Logger(log_dir="test_logs")
    
    # Créer un screenshot synthétique
    screenshot = np.ones((600, 800, 3), dtype=np.uint8) * 255
    cv2.rectangle(screenshot, (100, 100), (200, 150), (0, 0, 0), 2)
    cv2.putText(screenshot, "Valider", (110, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
    
    # Créer le détecteur (sans VLM pour les tests)
    detector = UIElementDetector(
        llm_manager=None,  # Pas de VLM pour les tests
        logger=logger,
        config={
            "region_proposer": {
                "use_text_detection": True,
                "use_rectangle_detection": True,
                "use_vlm_detection": False
            }
        }
    )
    
    print(f"✓ UIElementDetector créé")
    
    # Détecter les éléments
    window_info = WindowInfo(
        app_name="test_app",
        window_title="Test Window",
        screen_resolution=(800, 600)
    )
    
    elements = detector.detect_elements(
        screenshot=screenshot,
        window_info=window_info,
        data_dir="test_data"
    )
    
    print(f"✓ Éléments détectés: {len(elements)}")
    for i, elem in enumerate(elements[:3]):  # Afficher les 3 premiers
        print(f"  Élément {i+1}:")
        print(f"    ID: {elem.element_id}")
        print(f"    Type: {elem.type.value}")
        print(f"    Role: {elem.role}")
        print(f"    Label: {elem.label}")
        print(f"    BBox: {elem.bbox}")
        print(f"    Confiance: {elem.confidence:.2f}")
    
    # Vérifications
    for elem in elements:
        assert elem.element_id, "element_id doit être non-vide"
        assert elem.type, "type doit être défini"
        assert elem.role, "role doit être défini"
        assert len(elem.bbox) == 4, "bbox doit avoir 4 coordonnées"
        assert 0 <= elem.confidence <= 1, "confidence doit être entre 0 et 1"
    
    print("✓ Test UIElementDetector réussi")


def test_enriched_screen_capture():
    """Test du EnrichedScreenCapture."""
    print("\n" + "=" * 70)
    print("TEST 4: EnrichedScreenCapture")
    print("=" * 70)
    
    logger = Logger(log_dir="test_logs")
    
    # Test mode light
    print("\n  Test mode light:")
    capture_light = EnrichedScreenCapture(
        logger=logger,
        data_dir="test_data",
        mode="light"
    )
    print(f"  ✓ Mode: {capture_light.get_mode()}")
    print(f"  ✓ UI Detector actif: {capture_light.ui_detector is not None}")
    assert capture_light.get_mode() == "light"
    assert capture_light.ui_detector is None
    
    # Test mode enriched
    print("\n  Test mode enriched:")
    capture_enriched = EnrichedScreenCapture(
        logger=logger,
        data_dir="test_data",
        mode="enriched"
    )
    print(f"  ✓ Mode: {capture_enriched.get_mode()}")
    print(f"  ✓ UI Detector actif: {capture_enriched.ui_detector is not None}")
    assert capture_enriched.get_mode() == "enriched"
    assert capture_enriched.ui_detector is not None
    
    # Test changement de mode
    print("\n  Test changement de mode:")
    capture_enriched.set_mode("light")
    print(f"  ✓ Nouveau mode: {capture_enriched.get_mode()}")
    print(f"  ✓ UI Detector après changement: {capture_enriched.ui_detector is not None}")
    assert capture_enriched.get_mode() == "light"
    assert capture_enriched.ui_detector is None
    
    # Test capture (sans sauvegarde)
    print("\n  Test capture sans sauvegarde:")
    screenshot = np.ones((600, 800, 3), dtype=np.uint8) * 255
    
    screen_state = capture_light.capture_and_enrich(
        screenshot=screenshot,
        session_id="test_session",
        window_title="Test Window",
        app_name="test_app",
        screen_resolution=(800, 600),
        save=False
    )
    
    print(f"  ✓ ScreenState créé: {screen_state.screen_state_id}")
    print(f"  ✓ Mode: {screen_state.mode}")
    print(f"  ✓ UI Elements: {len(screen_state.ui_elements)}")
    
    assert screen_state.screen_state_id
    assert screen_state.mode == "light"
    assert len(screen_state.ui_elements) == 0  # Mode light = pas d'éléments
    
    print("\n✓ Test EnrichedScreenCapture réussi")


def cleanup():
    """Nettoie les fichiers de test."""
    import shutil
    
    if Path("test_data").exists():
        shutil.rmtree("test_data")
    if Path("test_logs").exists():
        shutil.rmtree("test_logs")


def main():
    """Exécute tous les tests."""
    print("\n" + "=" * 70)
    print("TESTS D'INTÉGRATION - PHASE 2 MODE ENRICHI")
    print("UI Element Detection - Détection d'Éléments Basique")
    print("=" * 70)
    
    try:
        # Test 1: BoundingBox
        test_bounding_box()
        
        # Test 2: RegionProposer
        test_region_proposer()
        
        # Test 3: UIElementDetector
        test_ui_element_detector()
        
        # Test 4: EnrichedScreenCapture
        test_enriched_screen_capture()
        
        print("\n" + "=" * 70)
        print("✓ TOUS LES TESTS RÉUSSIS!")
        print("=" * 70)
        print("\nRésumé:")
        print("  ✓ BoundingBox: Calculs IoU, aire, centre")
        print("  ✓ RegionProposer: Détection de régions")
        print("  ✓ UIElementDetector: Pipeline complet de détection")
        print("  ✓ EnrichedScreenCapture: Intégration complète")
        print("\nPhase 2 (Mode Enrichi) implémentée avec succès!")
        print("Le système peut maintenant détecter les éléments UI.")
        print("=" * 70)
        
        return True
        
    except Exception as e:
        print(f"\n✗ ÉCHEC DU TEST: {e}")
        import traceback
        traceback.print_exc()
        return False
        
    finally:
        cleanup()


if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)
