#!/usr/bin/env python3
"""
Test for Entity property setters - fixing "new style getargs format" error

Verifies that Entity position and sprite_number setters work correctly.
"""

def test_entity_setters(timer_name):
    """Test that Entity property setters work correctly"""
    import mcrfpy
    
    print("Testing Entity property setters...")
    
    # Create test scene and grid
    mcrfpy.createScene("entity_test")
    ui = mcrfpy.sceneUI("entity_test")
    
    # Create grid with texture
    texture = mcrfpy.Texture("assets/kenney_ice.png", 16, 16)
    grid = mcrfpy.Grid(10, 10, texture, (10, 10), (400, 400))
    ui.append(grid)
    
    # Create entity
    initial_pos = mcrfpy.Vector(2.5, 3.5)
    entity = mcrfpy.Entity(initial_pos, texture, 5, grid)
    grid.entities.append(entity)
    
    print(f"✓ Created entity at position {entity.pos}")
    
    # Test position setter with Vector
    new_pos = mcrfpy.Vector(4.0, 5.0)
    try:
        entity.pos = new_pos
        assert entity.pos.x == 4.0, f"Expected x=4.0, got {entity.pos.x}"
        assert entity.pos.y == 5.0, f"Expected y=5.0, got {entity.pos.y}"
        print(f"✓ Position setter works with Vector: {entity.pos}")
    except Exception as e:
        print(f"✗ Position setter failed: {e}")
        raise
    
    # Test position setter with tuple (should also work via PyVector::from_arg)
    try:
        entity.pos = (7.5, 8.5)
        assert entity.pos.x == 7.5, f"Expected x=7.5, got {entity.pos.x}"
        assert entity.pos.y == 8.5, f"Expected y=8.5, got {entity.pos.y}"
        print(f"✓ Position setter works with tuple: {entity.pos}")
    except Exception as e:
        print(f"✗ Position setter with tuple failed: {e}")
        raise
    
    # Test draw_pos setter (collision position)
    try:
        entity.draw_pos = mcrfpy.Vector(3, 4)
        assert entity.draw_pos.x == 3, f"Expected x=3, got {entity.draw_pos.x}"
        assert entity.draw_pos.y == 4, f"Expected y=4, got {entity.draw_pos.y}"
        print(f"✓ Draw position setter works: {entity.draw_pos}")
    except Exception as e:
        print(f"✗ Draw position setter failed: {e}")
        raise
    
    # Test sprite_number setter
    try:
        entity.sprite_number = 10
        assert entity.sprite_number == 10, f"Expected sprite_number=10, got {entity.sprite_number}"
        print(f"✓ Sprite number setter works: {entity.sprite_number}")
    except Exception as e:
        print(f"✗ Sprite number setter failed: {e}")
        raise
    
    # Test invalid position setter (should raise TypeError)
    try:
        entity.pos = "invalid"
        print("✗ Position setter should have raised TypeError for string")
        assert False, "Should have raised TypeError"
    except TypeError as e:
        print(f"✓ Position setter correctly rejects invalid type: {e}")
    except Exception as e:
        print(f"✗ Unexpected error: {e}")
        raise
    
    # Test invalid sprite number (should raise TypeError)
    try:
        entity.sprite_number = "invalid"
        print("✗ Sprite number setter should have raised TypeError for string")
        assert False, "Should have raised TypeError"
    except TypeError as e:
        print(f"✓ Sprite number setter correctly rejects invalid type: {e}")
    except Exception as e:
        print(f"✗ Unexpected error: {e}")
        raise
    
    # Cleanup timer
    mcrfpy.delTimer("test_timer")
    
    print("\n✅ Entity property setters test PASSED - All setters work correctly")

# Execute the test after a short delay to ensure window is ready
import mcrfpy
mcrfpy.setTimer("test_timer", test_entity_setters, 100)