# Game imports
from utils.serializable import Serializable
from entities.entity import Entity
from events.turn import Turn, TurnAction

# Native imports
import json
import uuid

class Event(Serializable):
    def __init__(self, location:str, initial_description:str, entities:list[str]):
        super().__init__()
        self.id:str = str(uuid.uuid4())
        self.location:str = location
        self.initial_description:str = initial_description

        self.entities:list[str] = entities
        self.turns:list[Turn] = []
        self.add_turn()
    
    def remove_entity(self, entity_id:str):
        if entity_id in self.entities:
            self.entities.remove(entity_id)
    
    def add_entity(self, entity_id:str):
        if not entity_id in self.entities:
            self.entities.append(entity_id)

    def get_current_turn(self):
        idx = len(self.turns) - 1
        if idx < 0:
            raise IndexError("There is no turns yet, you should create one!")
        return self.turns[idx]
    
    def add_turn(self):
        self.turns.append(Turn())
    
    def perform_action(self, action:TurnAction, entity_id:str, description:str):
        current_turn = self.get_current_turn()
        current_turn.add_action(action_type=action, entity_id=entity_id, description=description)

        if current_turn.is_finished(len(self.entities)):
            self.add_turn()
            return True
        
        return False
