from typing import List

from model.customexception import (
    MessageBusError,
    UpdateRuptureException,
)
import json

import sysactions
from msgbus import TK_SYS_ACK

from api.product import get_product_service
from bustoken import TK_SLCTRL_PMGR_RUPTURE_GET_DISABLED


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: int
        parts_to_enable,    # type: List[int]
        parts_to_disable,   # type: List[int]
    ):
        # type: (...) -> None

        product_service = get_product_service()
        try:
            product_service.update_items(
                user_id=user_id,
                enabled=parts_to_enable,
                disabled=parts_to_disable,
            )
        except (Exception,) as ex:
            raise UpdateRuptureException(ex)
