#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Test widget integration with new message structures."""

def test_widget_field_access():
    """Test that widgets can access message fields."""
    from pymsc.gui.command_registry import CHECKBOXES, COMBOBOXES
    from pymsc.core.message_definitions import msg_a2, msg_b7
    
    print("\nTesting Widget Field Access:")
    print("=" * 60)
    
    # Test Checkbox widget data
    print("\n1. Checkbox Widgets:")
    for cb in CHECKBOXES[:3]:  # Test first 3
        msg = cb.get('message')
        field = cb.get('field')
        if msg and field:
            try:
                value = msg.get_value_for_field(field)
                print(f"   ✓ {cb['label']:20} -> {field:20} = {value}")
            except Exception as e:
                print(f"   ✗ {cb['label']:20} -> {field:20} ERROR: {e}")
    
    # Test Combobox widget data
    print("\n2. Combobox Widgets:")
    for cmb in COMBOBOXES[:3]:  # Test first 3
        msg = cmb.get('message')
        field = cmb.get('field')
        if msg and field:
            try:
                value = msg.get_value_for_field(field)
                print(f"   ✓ {cmb['label']:20} -> {field:20} = {value}")
            except Exception as e:
                print(f"   ✗ {cmb['label']:20} -> {field:20} ERROR: {e}")
    
    # Test Tellback fields
    print("\n3. Tellback Fields:")
    for cb in CHECKBOXES[:3]:
        msg_tb = cb.get('message_tb')
        field_tb = cb.get('field_tb')
        if msg_tb and field_tb:
            try:
                value = msg_tb.get_value_for_field(field_tb)
                print(f"   ✓ {cb['label']:20} (TB) -> {field_tb:20} = {value}")
            except Exception as e:
                print(f"   ✗ {cb['label']:20} (TB) -> {field_tb:20} ERROR: {e}")
    
    print("\n" + "=" * 60)


def test_widget_field_write():
    """Test that widgets can write to message fields."""
    from pymsc.core.message_definitions import msg_a2
    
    print("\nTesting Widget Field Write:")
    print("=" * 60)
    
    test_fields = [
        ('stby', 1),
        ('freeze', 0),
        ('range_scale', 2),
    ]
    
    for field, test_value in test_fields:
        try:
            # Read original
            original = msg_a2.get_value_for_field(field)
            
            # Write new value (without actually sending to hardware)
            msg_a2._internal_set = True  # Flag to prevent actual transmission
            # Just test the path resolution
            from pymsc.core.field_mappings import get_field_path
            path = get_field_path('A2', field)
            
            print(f"   ✓ {field:20} -> {path:35} (current={original})")
        except Exception as e:
            print(f"   ✗ {field:20} ERROR: {e}")
    
    print("\n" + "=" * 60)


if __name__ == '__main__':
    print("\n" + "=" * 60)
    print("Widget Integration Test")
    print("=" * 60)
    
    test_widget_field_access()
    test_widget_field_write()
    
    print("\n" + "=" * 60)
    print("Widget Integration Tests Complete!")
    print("=" * 60 + "\n")
