from ..models import GameModel

from .pong.PongGame import PongGame
from .tictactoe.TicTacToeGame import TicTacToeGame

class GameManager():
    
    def __init__(self) -> None:
        self._game_list: list[PongGame | TicTacToeGame] = []
    
    def remove(self, game: PongGame | TicTacToeGame) -> None:
        if (game not in self._game_list):
            return
        self._game_list.remove(game)
    
    def get(self, game_id: int, game_type: str) -> TicTacToeGame | PongGame:
        
        if (not GameModel.objects.filter(pk=game_id, finished=False, game_type=game_type).exists()):
            return None
        
        for game in self._game_list:
            game: PongGame | TicTacToeGame
            if (game.game_id == game_id):
                return game

        game: PongGame | TicTacToeGame
        if (game_type == "pong"):
            game = PongGame(game_id, self)
        elif (game_type == "tictactoe"):
            game = TicTacToeGame(game_id, self)
        
        self._game_list.append(game)
        
        return game