"""
Mock output implementation for testing.

Collects all output messages instead of sending via WebSocket.
"""
from typing import List, Dict, Any
import copy


class MockGameOutput:
    """
    Mock implementation of IGameOutput for testing.

    Collects all messages instead of sending via WebSocket.
    """

    def __init__(self):
        """Initialize mock output with empty message lists."""
        self.events: List[Any] = []
        self.updates: List[Any] = []
        self.dicts: List[Dict] = []
        self.all_messages: List[Dict] = []

    async def send_event_message(self, event_obj):
        """
        Collect an event message.

        Args:
            event_obj: Event object to collect
        """
        event_copy = copy.deepcopy(event_obj)
        self.events.append(event_copy)
        self.all_messages.append({
            'type': 'event',
            'data': event_copy
        })

    async def send_user_info(self, player):
        """
        Collect a player update.

        Args:
            player: Player object to collect
        """
        update_copy = copy.deepcopy(player)
        self.updates.append(update_copy)
        self.all_messages.append({
            'type': 'update',
            'data': update_copy
        })

    async def send_dict(self, obj):
        """
        Collect a dictionary message.

        Args:
            obj: Dictionary to collect
        """
        dict_copy = copy.deepcopy(obj)
        self.dicts.append(dict_copy)
        self.all_messages.append({
            'type': 'dict',
            'data': dict_copy
        })

    def clear(self):
        """Clear all collected messages (for test cleanup)."""
        self.events.clear()
        self.updates.clear()
        self.dicts.clear()
        self.all_messages.clear()

    def get_events(self) -> List[Any]:
        """Get all collected event messages."""
        return self.events.copy()

    def get_updates(self) -> List[Any]:
        """Get all collected player updates."""
        return self.updates.copy()

    def get_dicts(self) -> List[Dict]:
        """Get all collected dictionary messages."""
        return self.dicts.copy()

    def get_all_messages(self) -> List[Dict]:
        """Get all collected messages in order."""
        return self.all_messages.copy()

    def get_event_count(self) -> int:
        """Get number of event messages collected."""
        return len(self.events)

    def get_update_count(self) -> int:
        """Get number of update messages collected."""
        return len(self.updates)

    def find_events_by_type(self, event_type: str) -> List[Any]:
        """
        Find all events of a specific type.

        Args:
            event_type: Event type to search for ('messageEvent', 'questionEvent', etc.)

        Returns:
            List of matching events
        """
        return [
            event for event in self.events
            if hasattr(event, 'type') and event.type == event_type
        ]

    def find_event_by_id(self, event_id: str) -> Any:
        """
        Find an event by its ID.

        Args:
            event_id: Event ID to search for

        Returns:
            Event object if found, None otherwise
        """
        for event in self.events:
            if hasattr(event, 'id') and event.id == event_id:
                return event
        return None
