import bustoken
from typing import List

from model.customexception import MessageBusError
from actions.util.get_menu_user_logged import get_menu_manager_authenticated_user
from actions.util.get_operator_name import get_operator_name
import json

import sysactions
from msgbus import (
    TK_EVT_EVENT,
    TK_SYS_ACK,
    FM_PARAM,
)
from sysactions import (
    action,
    show_info_message,
    get_custom,
    get_operator_session,
    get_model,
)

from bustoken import (
    TK_SLCTRL_PMGR_RUPTURE_GET_DISABLED,
    TK_SLCTRL_PMGR_RUPTURE_UPDATE_ITEMS,
)


class Rupture(object):

    @staticmethod
    def get_disabled_products():
        # type: (...) -> List[int]

        msg = sysactions.send_message("ORDERMGR", TK_SLCTRL_PMGR_RUPTURE_GET_DISABLED)

        if msg.token != TK_SYS_ACK:
            raise MessageBusError(
                component="ORDERMGR",
                token=msg.token,
                data=msg.data,
            )

        return json.loads(msg.data.decode("utf-8"))

    @staticmethod
    def update_items(
        user_id,                # type: str
        parts_to_enable,        # type: List[int]
        parts_to_disable,       # type: List[int]
    ):
        # type: (...) -> None

        data = json.dumps({
            "user_id": user_id,
            "operator_name": "",
            "enabled": parts_to_enable,
            "disabled": parts_to_disable
        })

        msg = sysactions.send_message(
            dest_name="ORDERMGR",
            token=TK_SLCTRL_PMGR_RUPTURE_UPDATE_ITEMS,
            format=FM_PARAM,
            data=data,
        )
        if msg.token != TK_SYS_ACK:
            raise MessageBusError(
                component="ORDERMGR",
                token=msg.token,
                data=msg.data,
            )
