#!/usr/bin/env python3
"""
Test for Issue #27: EntityCollection.extend() method

Verifies that EntityCollection can extend with multiple entities at once.
"""

def test_entity_extend(timer_name):
    """Test that EntityCollection.extend() method works correctly"""
    import mcrfpy
    import sys
    
    print("Issue #27 test: EntityCollection.extend() method")
    
    # Create test scene and grid
    mcrfpy.createScene("test")
    ui = mcrfpy.sceneUI("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)
    
    # Add some initial entities
    entity1 = mcrfpy.Entity((1, 1), texture, 1, grid)
    entity2 = mcrfpy.Entity((2, 2), texture, 2, grid)
    grid.entities.append(entity1)
    grid.entities.append(entity2)
    
    print(f"✓ Initial entities: {len(grid.entities)}")
    
    # Test 1: Extend with a list of entities
    new_entities = [
        mcrfpy.Entity((3, 3), texture, 3, grid),
        mcrfpy.Entity((4, 4), texture, 4, grid),
        mcrfpy.Entity((5, 5), texture, 5, grid)
    ]
    
    try:
        grid.entities.extend(new_entities)
        assert len(grid.entities) == 5, f"Expected 5 entities, got {len(grid.entities)}"
        print(f"✓ Extended with list: now {len(grid.entities)} entities")
    except Exception as e:
        print(f"✗ Failed to extend with list: {e}")
        raise
    
    # Test 2: Extend with a tuple
    more_entities = (
        mcrfpy.Entity((6, 6), texture, 6, grid),
        mcrfpy.Entity((7, 7), texture, 7, grid)
    )
    
    try:
        grid.entities.extend(more_entities)
        assert len(grid.entities) == 7, f"Expected 7 entities, got {len(grid.entities)}"
        print(f"✓ Extended with tuple: now {len(grid.entities)} entities")
    except Exception as e:
        print(f"✗ Failed to extend with tuple: {e}")
        raise
    
    # Test 3: Extend with generator expression
    try:
        grid.entities.extend(mcrfpy.Entity((8, i), texture, 8+i, grid) for i in range(3))
        assert len(grid.entities) == 10, f"Expected 10 entities, got {len(grid.entities)}"
        print(f"✓ Extended with generator: now {len(grid.entities)} entities")
    except Exception as e:
        print(f"✗ Failed to extend with generator: {e}")
        raise
    
    # Test 4: Verify all entities have correct grid association
    for i, entity in enumerate(grid.entities):
        # Just checking that we can iterate and access them
        assert entity.sprite_number >= 1, f"Entity {i} has invalid sprite number"
    print("✓ All entities accessible and valid")
    
    # Test 5: Invalid input - non-iterable
    try:
        grid.entities.extend(42)
        print("✗ Should have raised TypeError for non-iterable")
    except TypeError as e:
        print(f"✓ Correctly rejected non-iterable: {e}")
    
    # Test 6: Invalid input - iterable with non-Entity
    try:
        grid.entities.extend([entity1, "not an entity", entity2])
        print("✗ Should have raised TypeError for non-Entity in iterable")
    except TypeError as e:
        print(f"✓ Correctly rejected non-Entity in iterable: {e}")
    
    # Test 7: Empty iterable (should work)
    initial_count = len(grid.entities)
    try:
        grid.entities.extend([])
        assert len(grid.entities) == initial_count, "Empty extend changed count"
        print("✓ Empty extend works correctly")
    except Exception as e:
        print(f"✗ Empty extend failed: {e}")
        raise
    
    print(f"\n✅ Issue #27 test PASSED - EntityCollection.extend() works correctly")
    sys.exit(0)

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