import discord
from discord.ext import commands
import random
import csv


class Troll(commands.Cog):
    """
    Une catégorie de commandes s'exécutant par un préfixe
    """
    def __init__(self, bot: commands.Bot) -> None:
        self.bot = bot
        # Liste de phrases connues de World of Warcraft
        self.phrases = [
            "Lok'tar ogar! Victoire ou la mort !",
            "Pour la Horde !",
            "Pour Azeroth !",
            "Ensemble, nous tiendrons !",
            "Que la Lumière soit avec toi !",
            "Par le feu et la fureur !",
        ]

    @commands.Cog.listener(name="on_ready")
    async def affichage_fonction(self) -> None:
        print("Les cogs sont chargés")

    # Commande qui génère une phrase
    @commands.command()
    async def loktarhogar(self, ctx: commands.Context):
        phrase = random.choice(self.phrases)
        await ctx.send(phrase)

class Infos(commands.Cog):
    """
    Une catégorie de commandes s'exécutant par un préfixe
    """

    def __init__(self, bot: commands.Bot) -> None:
        self.bot = bot

    # Fonction pour lire le CSV et trouver l'unité par son nom
    def get_unite_info(self, nom_unite):
        with open('unites_wow.csv', newline='', encoding='utf-8') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                if row['nom'].lower() == nom_unite.lower():
                    return row
        return None

    # Fonction pour lister tous les noms des unités
    def get_liste_unites(self):
        unites = []
        with open('unites_wow.csv', newline='', encoding='utf-8') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                unites.append(row['nom'])
        return unites

    # Commande pour afficher les informations d'une unité ou lister les unités
    @commands.command(name="unite", help="Une commande qui précise les infos utiles sur l'unité sélectionnée ou liste les unités.")
    async def unite(self, ctx: commands.Context, *, nom_unite: str):
        if nom_unite.lower() == "liste":
            # Liste toutes les unités
            unites = self.get_liste_unites()
            message = "**Liste des unités disponibles :**\n" + "\n".join(unites)
            await ctx.send(message)
        else:
            # Cherche une unité spécifique
            unite_info = self.get_unite_info(nom_unite)
            if unite_info:
                message = (
                    f"**{unite_info['nom']}**\n"
                    f"Catégorie: {unite_info['categorie']}\n"
                    f"Type de combat: {unite_info['type_combat']}\n"
                    f"Points de vie: {unite_info['points_vie']}\n"
                    f"Dégâts: {unite_info['degats']}"
                )
                await ctx.send(message)
            else:
                await ctx.send(f"L'unité {nom_unite} n'a pas été trouvée.")

    @commands.command(name="ping", help="Une commande qui joue au ping-pong. Si tu lui dis ping, elle répond pong.")
    async def test_command(self, ctx: commands.Context) -> discord.Message:
        return await ctx.send("pong")
