import json

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

import get_products

from .. import logger


@action
def open_rupture(pos_id):
    try:
        enabled_ingredients = get_products.get_enabled_products()
        disabled_ingredients = get_products.get_disabled_products()

    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()

    dialog_contents = {
        "type": "rupture",
        "availableItems": enabled_ingredients,
        "selectedItems": disabled_ingredients
    }

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