from messagebus import (
    MessageBus,
    Message,
    DataType,
)

from .dto import (
    PosUpdaterDto,
    BinariesUpdaterDto,
    ChromeUpdaterDto,
)
from .token import PosUpdaterApiTokens


class PosUpdaterService(object):

    def __init__(
        self,           # type: PosUpdaterService
        message_bus,    # type: MessageBus
        pos_id,         # type: int
    ):
        # type: (...) -> None

        self.message_bus = message_bus
        self.pos_id = pos_id

        self.component_name = "PosUpdater"

    def catalog_update(
        self,       # type: PosUpdaterService
        user_id,    # type: str
    ):
        # type: (...) -> Message

        data = self._get_common_update_data(user_id=user_id)
        timeout = 10 * 1000 * 1000
        return self._send_message(
            token=PosUpdaterApiTokens.TK_POS_UPDATER_PERFORM_CATALOG_UPDATE,
            timeout=timeout,
            data=str(data),
        )

    def binaries_update(
        self,           # type: PosUpdaterService
        user_id,        # type: str
        force_update,   # type: bool
    ):
        # type: (...) -> Message

        data = BinariesUpdaterDto(
            pos_id=self.pos_id,
            user_id=user_id,
            force_update=force_update,
        )
        return self._send_message(
            token=PosUpdaterApiTokens.TK_POS_UPDATER_PERFORM_BINARIES_UPDATE,
            timeout=60000000 * 20,
            data=str(data),
        )

    def chrome_update(
        self,                # type: PosUpdaterService
        user_id,             # type: str
        validate_version,    # type: bool
    ):
        # type: (...) -> Message

        data = ChromeUpdaterDto(
            pos_id=self.pos_id,
            user_id=user_id,
            validate_version=validate_version,
        )
        return self._send_message(
            token=PosUpdaterApiTokens.TK_POS_UPDATER_PERFORM_CHROME_UPDATE,
            timeout=60000000 * 20,
            data=str(data),
        )

    def loader_update(
        self,       # type: PosUpdaterService
        user_id,    # type: str
    ):
        # type: (...) -> Message

        data = self._get_common_update_data(user_id=user_id)
        return self._send_message(
            token=PosUpdaterApiTokens.TK_POS_UPDATER_PERFORM_LOADER_UPDATE,
            timeout=60000000 * 20,
            data=str(data),
        )

    def _send_message(
        self,       # type: PosUpdaterService
        token,      # type: int
        timeout,    # type: int
        data,       # type: str
    ):
        # type: (...) -> Message

        return self.message_bus.send_message(
            component=self.component_name,
            message=Message(
                token=token,
                data_type=DataType.json,
                timeout=timeout,
                data=data,
            ),
        )

    def _get_common_update_data(
        self,       # type: PosUpdaterService
        user_id,    # type: str
    ):
        # type: (...) -> PosUpdaterDto

        return PosUpdaterDto(
            pos_id=self.pos_id,
            user_id=user_id,
        )
