from .ingredient import Ingredient

class Recette:
    def __init__(self, nom: str, ingredients: dict[str, int], description: str = ""):
        self.nom = nom
        self.description = description
        self.ingredients = ingredients

        # Calcul des valeurs nutritionnelles totales de la recette
        for key, value in self.calculate_values().items():
            setattr(self, key, value)

        self.__dict__.update(self.calculate_values())

    def __repr__(self):
        return f"Recette(Nom={self.nom}, Ingredients={len(self.ingredients)})"
    
    def calculate_values(self):
        from app.services.data_ingredients import get_ingredient
        total_values = {}
        for str_ingredient, quantity in self.ingredients.items():
            ingredient = get_ingredient(str_ingredient)
            for key, value in ingredient.get_all_values().items():
                if isinstance(value, (int, float)):
                    total_values[key] = total_values.get(key, 0) + value * quantity/100
        return total_values
    
    def set_description(self, description):
        self.description = description
    
    def get_description(self):
        return self.description
    
    def get_ingredients(self):
        return self.ingredients
    
    def get_value(self, attribute_name):
        return getattr(self, attribute_name, None)
    
    def get_name(self):
        return self.nom.split('__')[0] if '__' in self.nom else self.nom
    
    def info(self):
        info_str = f"Information sur la recette: {self.nom}\n"
        info_str += f"Description: {self.description}\n"
        info_str += "Ingrédients :\n"
        for ingredient, quantity in self.ingredients.items():
            info_str += f"- {ingredient} : {quantity}g\n"
        info_str += "Valeurs nutritionnelles:\n"
        for attr, value in self.__dict__.items():
            if attr not in ['nom', 'description', 'ingredients']:
                unite = attr.split("__")[-1] if "__" in attr else ""
                nom = attr.split("__")[0] if "__" in attr else attr
                info_str += f"{nom} : {value} {unite}\n"
        return info_str
    
    def info_short(self):
        info_short_str = f"Information sur la recette: {self.nom}\n"
        info_short_str += f"Description: {self.description}\n"
        info_short_str += "Ingrédients :\n"
        for ingredient, quantity in self.ingredients.items():
            info_short_str += f"- {ingredient} : {quantity}g\n"
        info_short_str += "Valeurs nutritionnelles:\n"
        info_short_str += f"Energie : {self.get_value('energie__kcal')} kcal\n"
        info_short_str += f"Protéines : {self.get_value('proteines__g')} g\n"
        info_short_str += f"Glucides : {self.get_value('glucides__g')} g\n"
        info_short_str += f"Lipides : {self.get_value('lipides__g')} g\n"
        info_short_str += f"Sucres : {self.get_value('sucres__g')} g\n"

        return info_short_str
    
    def add_ingredient(self, ingredient, quantity):
        self.ingredients[ingredient] = int(quantity) if ingredient not in self.ingredients.keys() else int(self.ingredients[ingredient]) + int(quantity)
        self.__dict__.update(self.calculate_values())