# heroDB

from models.heroes.hero import Hero
from models.heroes.archery import Archery
from models.heroes.warrior import Warrior
from models.heroes.wizard import Wizard


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

if TYPE_CHECKING:
    from database.mainDB import MainDB


class HeroDB:
    def __init__(self, db: "MainDB") -> None:
        self.db = db

    def get_all_heroes(self) -> list[Hero]:
        query = "SELECT * FROM heroes WHERE ?"
        values = (1,)

        self.db.sql_query(query, values)

        results = self.db.cursor.fetchall()

        heroes = []
        for result in results:
            heroes.append(self.define_hero(result))

        return heroes

    def get_archers(self) -> list[Hero]:
        query = "SELECT * FROM heroes WHERE types_id = ?"
        values = (Hero.ARCHERY,)

        self.db.sql_query(query, values)

        results = self.db.cursor.fetchall()

        list_return = []
        for result in results:
            list_return.append(self.define_hero(result))

        return list_return

    def get_warriors(self) -> list[Hero]:
        query = "SELECT * FROM heroes WHERE types_id = ?"
        values = (Hero.WARRIOR,)

        self.db.sql_query(query, values)

        results = self.db.cursor.fetchall()

        list_return = []
        for result in results:
            list_return.append(self.define_hero(result))

        return list_return

    def get_wizards(self) -> list[Hero]:
        query = "SELECT * FROM heroes WHERE types_id = ?"
        values = (Hero.WIZARD,)

        self.db.sql_query(query, values)

        results = self.db.cursor.fetchall()

        list_return = []
        for result in results:
            list_return.append(self.define_hero(result))

        return list_return

    def define_hero(self, result: list) -> Hero:
        id = result[0]
        title = result[1]
        types_id = result[2]
        health_points = result[3]
        attack_power = result[4]
        defense = result[5]
        mana = result[6]
        description = result[7]

        if types_id == Hero.ARCHERY:
            hero = Archery(
                title,
                description,
                health_points,
                attack_power,
                defense,
            )
        elif types_id == Hero.WARRIOR:
            hero = Warrior(
                title,
                description,
                health_points,
                attack_power,
                defense,
            )
        elif types_id == Hero.WIZARD:
            hero = Wizard(
                title,
                description,
                health_points,
                attack_power,
                defense,
                mana,
            )

        hero.id = id

        return hero
