# chooseMonstersUI

from models.heroes.monster import Monster

from sty import fg
from random import randint

# pour éviter les pb d'import circulaire
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from controllers.PveController import PveController


class ChooseMonstersUI:
    def __init__(
        self, pveController: "PveController", difficulty: int, is_AI: bool
    ) -> None:
        MAX_NUMBER_MONSTER = 3
        self.pveController = pveController
        self.is_AI = is_AI
        self.difficulty = difficulty

        self.monster: Monster = None
        self.monsters: list[Monster] = []

        for i in range(0, MAX_NUMBER_MONSTER):
            self.set_monster()

            self.monsters.append(self.monster)

    def set_monster(self):
        monsters = self.pveController.db.monster_db.get_all_monsters()
        len_monsters = len(monsters)

        choice = -1  # Initialiser à une valeur qui n'est pas un index valide
        while choice < 1 or choice > len_monsters:
            for i in range(0, len_monsters):
                monster = monsters[i]
                if not self.is_AI:
                    print(f"{i+1}. {monster.get_type_str()} - {monster.name}")

            try:
                if self.is_AI:
                    choice = randint(1, len_monsters)
                # else:
                #     choice = int(input("Give your choice: "))
            except ValueError:
                print(f"{fg.red}Input a valid number{fg.rs}")
                continue

            if choice < 1 or choice > len_monsters:
                print(f"{fg.red}Invalid choice. Try again{fg.rs}")

            # L'indice dans la liste est `choice - 1`
            # car choice commence à partir de 1
            self.monster = monsters[choice - 1]
            self.monster.name = fg.da_blue + self.monster.name + fg.rs

            if self.monster.HP > self.difficulty:
                choice = -1
