import logging

from bustoken import TK_SLCTRL_OMGR_UPDATEORDERTENDERDETAIL
from msgbus import (
    FM_PARAM,
    TK_SYS_ACK,
)

from sysactions import (
    get_model,
    get_posot,
)

from .. import mb_context


logger = logging.getLogger("PosActions")


def update_tenders(
    pos_id,             # type: str
    order_id,           # type: str
    tender_id,          # type: str
    new_tender_detail   # type: str
):
    # type: (...) -> bool
    model = get_model(pos_id)
    pos_ot = get_posot(model)
    try:
        timeout = 600000000
        params = ('1' if pos_ot.blkopnotify else '0', pos_id, order_id, new_tender_detail)
        req_buf = '\x00'.join((str(p) for p in params))
        result = mb_context.MB_EasySendMessage(
            'ORDERMGR%s' % pos_id,
            TK_SLCTRL_OMGR_UPDATEORDERTENDERDETAIL,
            FM_PARAM,
            req_buf,
            timeout
        )
        if result.token == TK_SYS_ACK:
            return True

    except Exception as _:
        logger.exception("Fail salve receipt cancel: Tender: {}".format(tender_id))

    return False
