from enum import Enum
from json import dumps, loads


from .. import db
from app.models.database.kitchen_model import DBKitchen
from .tools import parse_int, parse_text, parse_style


class KitchenStatus(Enum):
    open = "KitchenStatus.open"
    closed = "KitchenStatus.closed"


class Kitchen:
    def __init__(self):
        self.id = -1
        self.title = ""
        self.status = ""

    # end def

    def from_json(self, jsonObject):
        """
        Extracts the attributes of the jsonObject.

        Example of the expected json :
        {
            "id": 1,
            "title": "Kitchen #1",
        },
        """

        self.id = parse_int(jsonObject.get("id"))
        self.title = parse_text(jsonObject.get("title"))
        self.status = parse_text(
            jsonObject.get("status"), default=str(KitchenStatus.open)
        )

    # end def

    def to_json(self):
        """
        TODO: Add definition ... .
        """
        jsonObject = {
            "id": self.id,
            "title": self.title,
            "status": self.status,
        }

        return jsonObject

    # enf def

    def to_db(self):
        """
        TODO: Add definition ... .
        """

        db_kitchen = DBKitchen()
        db_kitchen.kth_kitchen_id = self.id
        db_kitchen.kth_kitchen = self.title
        db_kitchen.kth_status = str(KitchenStatus.open)

        # Inserting a new record
        db.session.add(db_kitchen)
        db.session.commit()
        # Setting id
        self.id = db_kitchen.kth_kitchen_id

    # enf def

    def from_db(self, db_kitchen: DBKitchen):
        """
        TODO: Add definition ... .
        """

        self.id = db_kitchen.kth_kitchen_id
        self.title = db_kitchen.kth_kitchen
        self.status = db_kitchen.kth_status

    # enf def


# end class
