# roomUI

from models.heroes.hero import Hero
from models.heroes.monster import Monster
from views.PVE.chooseMonstersUI import ChooseMonstersUI

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 RoomUI:
    def __init__(
        self,
        pveController: "PveController",
        difficulty: int,
        heroes: list[Hero],
    ) -> None:
        self.pveController = pveController
        self.difficulty = difficulty
        self.heroes = heroes
        self.monsters: list[Monster] = None

        if self.heroes:
            print("Monsters creation")
            for i in range(0, 3):
                self.monsters = ChooseMonstersUI(
                    pveController, difficulty, True
                ).monsters
                print(f"{self.monsters[i].name} join the room")

            monster_name = [monster.name for monster in self.monsters]
            print(f"Here your new ennemis: {(", ".join(monster_name)).strip()}")

            while self.heroes and self.monsters:
                self.fight(self.heroes, self.monsters)

                for monster in self.monsters:
                    if monster.HP < 0:
                        self.monsters.remove(monster)

                if self.monsters:
                    self.fight(self.monsters, self.heroes, True)

                    for hero in self.heroes:
                        if hero.HP < 0:
                            self.heroes.remove(hero)

            if self.heroes:
                print("Congrats ! You passed next room")
            elif self.monsters:
                print(f"You {fg.red}lose{fg.rs}")
                self.pveController.mController.close_game()
            else:
                raise Exception("An error occured")

    def fight(
        self,
        heroes: list[Hero] | list[Monster],
        opponents: list[Hero] | list[Monster],
        is_ai_turn: bool = False,
    ):
        heroes_list_len = len(heroes)

        if heroes_list_len > 1:
            choice = -1
            while choice < 1 or choice > heroes_list_len:
                if is_ai_turn:
                    choice = randint(1, heroes_list_len + 1)
                else:
                    print("Who will attack?")
                    for i in range(0, heroes_list_len):
                        hero = heroes[i]
                        print(
                            f"{i+1} - {hero.name} | {hero.attack_power} {fg.red}Att{fg.rs} | {hero.defense} {fg.li_magenta}Blo{fg.rs} | {hero.HP}{fg.green} HP{fg.rs}"
                        )

                    try:
                        choice = int(input("Give your choice: "))
                    except ValueError:
                        print(f"{fg.red}Input a valid number{fg.rs}")
                        continue

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

            choosen_hero = heroes[choice - 1]
            print(f"{choosen_hero.name} will attack")
        else:
            choosen_hero = heroes[0]

        opponents_list_len = len(opponents)

        choice = -1
        while choice < 1 or choice > opponents_list_len:
            if is_ai_turn:
                choice = randint(1, opponents_list_len + 1)
            else:
                print("Who do you want to attack?")
                for i in range(0, opponents_list_len):
                    opponent = opponents[i]
                    print(
                        f"{i+1} - {opponent.name} | {opponent.attack_power} {fg.red}Att{fg.rs} | {opponent.defense} {fg.li_magenta}Blo{fg.rs} | {opponent.HP} {fg.green}HP{fg.rs}"
                    )

                try:
                    choice = int(input("Give your choice: "))
                except ValueError:
                    print(f"{fg.red}Input a valid number{fg.rs}")
                    continue

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

        choosen_opponent = opponents[choice - 1]
        print(f"{choosen_hero.name} attacks {choosen_opponent.name}")

        self.attacks(choosen_hero, choosen_opponent, is_ai_turn)

    def attacks(self, hero: Hero, opponent: Monster, is_ai_turn: bool):

        attacks_list = hero.attacks_choice
        len_attack_list = len(attacks_list)

        choice = -1
        while choice < 1 or choice > len_attack_list:
            if is_ai_turn:
                choice = randint(1, len_attack_list + 1)
            else:
                print("Choose an attack")
                for i in range(0, len_attack_list):
                    print(f"{i+1} - {attacks_list[i]}")

                try:
                    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_attack_list:
                    print(f"{fg.red}Invalid choice. Try again{fg.rs}")

        print(f"You choose {attacks_list[choice-1]}")

        if choice == 1:
            opponent.HP -= hero.attacks(opponent)
        elif choice == 2:
            hero.HP = hero.cure()

        if opponent.HP > 0:
            print(f"{opponent.name}: {opponent.HP} {fg.green}HP{fg.rs}")
        else:
            print(f"{opponent.name} is {fg.red}dead{fg.rs}")
