from sysactions import (
    get_model,
    get_posot,
)
from typing import (
    Optional,
    Dict,
)


class OrderService(object):
    @staticmethod
    def set_custom_property(
        pos_id,             # type: str
        key,                # type: str
        value,              # type: str
        order_id="",        # type: str
        pos_ot=None,        # type: Optional[bool]
        blk_notify=False,   # type: Optional[bool]
    ):
        # type: (...) -> None

        try:
            if pos_ot is None:
                model = get_model(pos_id)
                pos_ot = get_posot(model)

            pos_ot.blkopnotify = blk_notify
            pos_ot.setOrderCustomProperty(
                key=key,
                value=value,
                orderid=order_id,
            )
        finally:
            pos_ot.blkopnotify = False

    @staticmethod
    def set_custom_properties(
        pos_id,             # type: str
        custom_properties,  # type: Dict
        order_id,           # type: str
        pos_ot=None,        # type: Optional[bool]
        blk_notify=False,   # type: Optional[bool]
    ):
        # type: (...) -> None

        try:
            if len(custom_properties) == 0:
                return

            if pos_ot is None:
                model = get_model(posid=pos_id)
                pos_ot = get_posot(model=model)

            pos_ot.blkopnotify = blk_notify
            pos_ot.setOrderCustomProperties(
                properties=custom_properties,
                orderid=order_id,
            )
        finally:
            pos_ot.blkopnotify = False

