#!/usr/bin/env python3
"""
Test for UIEntityCollection.remove() accepting Entity instances
Tests the new behavior where remove() takes an Entity object instead of an index
"""

import mcrfpy
import sys

def test_remove_by_entity():
    """Test removing entities by passing the entity object"""
    
    # Create a test scene and grid
    scene_name = "test_entity_remove"
    mcrfpy.createScene(scene_name)
    
    # Create a grid (entities need a grid)
    grid = mcrfpy.Grid()  # Default 2x2 grid is fine for testing
    mcrfpy.sceneUI(scene_name).append(grid)
    
    # Get the entity collection
    entities = grid.entities
    
    # Create some test entities
    # Entity() creates entities with default position (0,0)
    entity1 = mcrfpy.Entity()
    entity1.x = 5
    entity1.y = 5
    
    entity2 = mcrfpy.Entity()
    entity2.x = 10
    entity2.y = 10
    
    entity3 = mcrfpy.Entity()
    entity3.x = 15
    entity3.y = 15
    
    # Add entities to the collection
    entities.append(entity1)
    entities.append(entity2)
    entities.append(entity3)
    
    print(f"Initial entity count: {len(entities)}")
    assert len(entities) == 3, "Should have 3 entities"
    
    # Test 1: Remove an entity that exists
    print("\nTest 1: Remove existing entity")
    entities.remove(entity2)
    assert len(entities) == 2, "Should have 2 entities after removal"
    assert entity1 in entities, "Entity1 should still be in collection"
    assert entity2 not in entities, "Entity2 should not be in collection"
    assert entity3 in entities, "Entity3 should still be in collection"
    print("✓ Successfully removed entity2")
    
    # Test 2: Try to remove an entity that's not in the collection
    print("\nTest 2: Remove non-existent entity")
    try:
        entities.remove(entity2)  # Already removed
        assert False, "Should have raised ValueError"
    except ValueError as e:
        print(f"✓ Got expected ValueError: {e}")
    
    # Test 3: Try to remove with wrong type
    print("\nTest 3: Remove with wrong type")
    try:
        entities.remove(42)  # Not an Entity
        assert False, "Should have raised TypeError"
    except TypeError as e:
        print(f"✓ Got expected TypeError: {e}")
    
    # Test 4: Try to remove with None
    print("\nTest 4: Remove with None")
    try:
        entities.remove(None)
        assert False, "Should have raised TypeError"
    except TypeError as e:
        print(f"✓ Got expected TypeError: {e}")
    
    # Test 5: Verify grid property is cleared (C++ internal)
    print("\nTest 5: Grid property handling")
    # Create a new entity and add it
    entity4 = mcrfpy.Entity()
    entity4.x = 20
    entity4.y = 20
    entities.append(entity4)
    # Note: grid property is managed internally in C++ and not exposed to Python
    
    # Remove it - this clears the C++ grid reference internally
    entities.remove(entity4)
    print("✓ Grid property handling (managed internally in C++)")
    
    # Test 6: Remove all entities one by one
    print("\nTest 6: Remove all entities")
    entities.remove(entity1)
    entities.remove(entity3)
    assert len(entities) == 0, "Collection should be empty"
    print("✓ Successfully removed all entities")
    
    print("\n✅ All tests passed!")
    return True

if __name__ == "__main__":
    try:
        success = test_remove_by_entity()
        sys.exit(0 if success else 1)
    except Exception as e:
        print(f"\n❌ Test failed with exception: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)