import json

from sysactions import (
    action,
    show_any_dialog,
    show_messagebox,
    StopAction
)

from get_products import get_daily_dishes
from .. import logger


@action
def open_daily_dishes(
    pos_id  # type: str
):
    # type: (...) -> None

    try:
        available_daily_dishes = get_daily_dishes()
        available_dishes = available_daily_dishes["available"]
        quantity_set_dishes = available_daily_dishes["quantity_set"]
    except Exception as e:
        logger.exception('[rupture_items] Exception {}'.format(e))
        show_messagebox(
            posid=pos_id,
            message="$ERROR_GETTING_DATA",
            title="$INFORMATION",
            buttons="$EXIT"
        )
        raise StopAction()

    if not (available_daily_dishes or quantity_set_dishes):
        show_messagebox(pos_id, "$NO_RUPTURE_ITEMS")
        return

    dialog_contents = {
        "type": "daily_dishes",
        "enabledIngredients": available_dishes,
        "disabledIngredients": quantity_set_dishes
    }

    info_text = json.dumps(dialog_contents)
    show_any_dialog(
        posid=pos_id,
        dlgtype="rupture",
        message="",
        title="$MANAGE_DAILY_DISHES",
        infotext=info_text,
        buttons="",
        icon="",
        timeout=6000000,
        mask="",
        defvalue="",
        minvalue="",
        maxvalue="",
        contents="",
        asynch=False
    )
