#!/usr/bin/env python3
"""
Test for Issue #33: Sprite index validation

Verifies that Sprite and Entity objects validate sprite indices
against the texture's actual sprite count.
"""

def test_sprite_index_validation(timer_name):
    """Test that sprite index validation works correctly"""
    import mcrfpy
    import sys
    
    print("Issue #33 test: Sprite index validation")
    
    # Create test scene
    mcrfpy.createScene("test")
    ui = mcrfpy.sceneUI("test")
    
    # Create texture - kenney_ice.png is 11x12 sprites of 16x16 each
    texture = mcrfpy.Texture("assets/kenney_ice.png", 16, 16)
    # Total sprites = 11 * 12 = 132 sprites (indices 0-131)
    
    # Test 1: Create sprite with valid index
    try:
        sprite = mcrfpy.Sprite(100, 100, texture, 50)  # Valid index
        ui.append(sprite)
        print(f"✓ Created sprite with valid index 50")
    except Exception as e:
        print(f"✗ Failed to create sprite with valid index: {e}")
        raise
    
    # Test 2: Set valid sprite index
    try:
        sprite.sprite_number = 100  # Still valid
        assert sprite.sprite_number == 100
        print(f"✓ Set sprite to valid index 100")
    except Exception as e:
        print(f"✗ Failed to set valid sprite index: {e}")
        raise
    
    # Test 3: Set maximum valid index
    try:
        sprite.sprite_number = 131  # Maximum valid index
        assert sprite.sprite_number == 131
        print(f"✓ Set sprite to maximum valid index 131")
    except Exception as e:
        print(f"✗ Failed to set maximum valid index: {e}")
        raise
    
    # Test 4: Invalid negative index
    try:
        sprite.sprite_number = -1
        print("✗ Should have raised ValueError for negative index")
    except ValueError as e:
        print(f"✓ Correctly rejected negative index: {e}")
    except Exception as e:
        print(f"✗ Wrong exception type for negative index: {e}")
        raise
    
    # Test 5: Invalid index too large
    try:
        sprite.sprite_number = 132  # One past the maximum
        print("✗ Should have raised ValueError for index 132")
    except ValueError as e:
        print(f"✓ Correctly rejected out-of-bounds index: {e}")
    except Exception as e:
        print(f"✗ Wrong exception type for out-of-bounds index: {e}")
        raise
    
    # Test 6: Very large invalid index
    try:
        sprite.sprite_number = 1000
        print("✗ Should have raised ValueError for index 1000")
    except ValueError as e:
        print(f"✓ Correctly rejected large invalid index: {e}")
    
    # Test 7: Entity sprite_number validation
    grid = mcrfpy.Grid(10, 10, texture, (10, 10), (400, 400))
    ui.append(grid)
    
    entity = mcrfpy.Entity((5, 5), texture, 50, grid)
    grid.entities.append(entity)
    
    try:
        entity.sprite_number = 200  # Out of bounds
        print("✗ Entity should also validate sprite indices")
    except ValueError as e:
        print(f"✓ Entity also validates sprite indices: {e}")
    except Exception as e:
        # Entity might not have the same validation yet
        print(f"Note: Entity validation not implemented yet: {e}")
    
    # Test 8: Different texture sizes
    # Create a smaller texture to test different bounds
    small_texture = mcrfpy.Texture("assets/Sprite-0001.png", 32, 32)
    small_sprite = mcrfpy.Sprite(200, 200, small_texture, 0)
    
    # This texture might have fewer sprites, test accordingly
    try:
        small_sprite.sprite_number = 100  # Might be out of bounds
        print("Note: Small texture accepted index 100")
    except ValueError as e:
        print(f"✓ Small texture has different bounds: {e}")
    
    print(f"\n✅ Issue #33 test PASSED - Sprite index validation works correctly")
    sys.exit(0)

# Execute the test after a short delay
import mcrfpy
mcrfpy.setTimer("test", test_sprite_index_validation, 100)