from carte import Carte
from quartier import Quartier


class UI:
    """Utilitaire pour un affichage console agréable"""

    @staticmethod
    def clear():
        """Efface la console"""
        print("\033c", end="")

    @staticmethod
    def title(text: str):
        """Affiche un titre centré avec séparateur"""
        print()
        print("=" * 60)
        print(text.center(60))
        print("=" * 60)

    @staticmethod
    def subtitle(text: str):
        """Affiche un sous-titre"""
        print()
        print("-" * 60)
        print(text)
        print("-" * 60)

    @staticmethod
    def player_turn(name: str):
        """Affiche le tour d'un joueur"""
        print()
        print(f"--> C'est au tour de {name}")

    @staticmethod
    def success(text: str):
        """Affiche un message de succès"""
        print(f"[OK] {text}")

    @staticmethod
    def error(text: str):
        """Affiche un message d'erreur"""
        print(f"[ERREUR] {text}")

    @staticmethod
    def info(text: str):
        """Affiche une information"""
        print(f"[INFO] {text}")

    @staticmethod
    def warning(text: str):
        """Affiche un avertissement"""
        print(f"[ATTENTION] {text}")

    @staticmethod
    def separator():
        """Séparateur visuel"""
        print("-" * 60)

    @staticmethod
    def pause(message: str = "Appuyez sur Entrée pour continuer...") -> None:
        """
        Affiche un message et attend que l'utilisateur appuie sur Entrée
        
        Args:
            message: Message à afficher (optionnel)
        """
        print()
        print(message)
        input()

    @staticmethod
    def prompt(text: str) -> str:
        """Affiche un prompt et retourne la réponse"""
        return input(f"\n{text} ")

    @staticmethod
    def _format_option(item, index: int) -> str:
        """Formate une option pour affichage dans un menu"""
        if isinstance(item, Quartier):
            # Quartier : affiche couleur, coût, points
            couleur_str = f"[{item._couleur.value}]"
            cout_str = f" | Coût: {item._cout}"
            points_str = f" | Points: {item._points}"
            return f"  {index}. {item._nom} {couleur_str}{cout_str}{points_str}"
        elif isinstance(item, Carte):
            # Personnage ou autre carte : affiche juste le nom
            return f"  {index}. {item._nom}"
        else:
            # Chaîne de caractères simple
            return f"  {index}. {item}"

    @staticmethod
    def menu(options: list, title: str = "") -> int:
        """
        Affiche un menu et retourne l'index choisi (0-based)
        options: liste des options à afficher (peut être des chaînes ou des objets Carte)
        """
        if title:
            UI.subtitle(title)

        for i, opt in enumerate(options):
            print(UI._format_option(opt, i + 1))

        choice = -1
        while choice < 0 or choice >= len(options):
            try:
                choice = int(input(f"\nVotre choix [1-{len(options)}]: ")) - 1
            except ValueError:
                UI.error("Veuillez entrer un nombre valide")
        return choice

    @staticmethod
    def yes_no_prompt(question: str) -> bool:
        """
        Affiche une question oui/non et retourne True/False
        """
        choice = UI.menu(["Oui", "Non"], question)
        return choice == 0

    @staticmethod
    def show_cards(cards: list, title: str = "Cartes disponibles"):
        """Affiche une liste de cartes avec détails"""
        UI.subtitle(title)
        for i, card in enumerate(cards):
            if isinstance(card, Quartier):
                couleur_str = f"[{card._couleur.value}]"
                cout_str = f" | Coût: {card._cout}"
                points_str = f" | Points: {card._points}"
                card_str = f"{card._nom} {couleur_str}{cout_str}{points_str}"
            elif isinstance(card, Carte):
                card_str = card._nom
            else:
                card_str = str(card)
            print(f"  {i+1}. {card_str}")
