# chooseCharacterUI

from models.loots.armor import Armor
from models.loots.weapon import Weapon

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.PvpController import PvpController


class ChooseCharacterUI:
    def __init__(self, pvpController: "PvpController", is_AI: bool) -> None:
        self.pvpController = pvpController
        self.is_AI = is_AI

        self.hero = None

        self.set_character()
        self.set_weapon()
        self.set_armor()

    def set_character(self):
        heroes = self.pvpController.db.hero_db.get_all_heroes()
        len_heroes = len(heroes)

        print("Choose your character")

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

            try:
                if self.is_AI:
                    choice = randint(1, len_heroes)
                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_heroes:
                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.hero = heroes[choice - 1]
        self.hero.name = fg.da_magenta + self.hero.name + fg.rs

        print(f"You choose {self.hero.name}!")

    def set_weapon(self):
        weapons: list[Weapon] = self.pvpController.db.weapon_db.get_weapon_by_hero_type(
            self.hero
        )
        len_weapons = len(weapons)

        print("Choose your weapon now")

        choice = -1
        while choice < 1 or choice > len_weapons:
            for i in range(0, len_weapons):
                weapon = weapons[i]
                print(f"{i+1}. {weapon.name} - {weapon.description}")

            try:
                if self.is_AI:
                    choice = randint(0, len_weapons)
                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_weapons:
                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.hero.weapon = weapons[choice - 1]
        self.hero.weapon.name = fg.da_magenta + self.hero.weapon.name + fg.rs

        print(f"You choose the weapon:  {self.hero.weapon.name}")

    def set_armor(self):
        armors: list[Armor] = self.pvpController.db.armor_db.get_armor_by_hero_type(
            self.hero
        )
        len_armors = len(armors)

        print("Now, choose your armor")

        choice = -1
        while choice < 1 or choice > len_armors:
            for i in range(0, len_armors):
                armor = armors[i]
                print(f"{i+1}. {armor.name}")

            try:
                if self.is_AI:
                    choice = randint(1, len_armors)
                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_armors:
                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.hero.armor = armors[choice - 1]
        self.hero.armor.name = fg.da_magenta + self.hero.armor.name + fg.rs

        print(f"You choose {self.hero.armor.name}")
