from .ligne_simple_model import LigneSimple


class Ligne(LigneSimple):
    def __init__(self):
        super().__init__()
        self.formule = []

    def from_json(self, jsonObject):
        """
        Extraire les attributs du jsonObject.

        Exemple de json attendu :
        {
            "designation": "Formule XXXXXXX",
            "qte": 2,
            "style": {},
            "formule": [
                {
                    "designation": "Plat XXXXXXX",
                    "qte": 1,
                    "note": "Sans quelque chose",
                    "style": {}
                },
                {
                    "designation": "Plat XXXXXXX",
                    "qte": 1,
                    "note": "Sans quelque chose",
                    "style": {}
                }
            ]
        }
        """

        self.designation = jsonObject.get("designation")

        try:
            self.qte = int(jsonObject.get("qte"))
        except:
            self.qte = ""

        self.note = jsonObject.get("note")
        self.style = jsonObject.get("style")

        if "formule" in jsonObject:
            if isinstance(jsonObject.get("formule"), list):
                for item in jsonObject.get("formule"):
                    line_simple = LigneSimple()
                    line_simple.from_json(item)
                    self.formule.append(line_simple)
