import json

from interactor.message.get_operators_logout_sessions import GetOperatorsLogoutSessionsInteractor
from interactor.message.get_operators_logout_sessions.dto import GetOperatorsLogoutSessionsResponse
from messagebus import (
    DataType,
    DefaultToken,
    Event,
    Message,
    MessageBus,
)
from processor import BaseProcessor


class GetOperatorsLogoutSessionsProcessor(BaseProcessor):

    def __init__(
        self,           # type: GetOperatorsLogoutSessionsProcessor
        interactor,     # type: GetOperatorsLogoutSessionsInteractor
    ):
        # type: (...) -> None
        super(GetOperatorsLogoutSessionsProcessor, self).__init__()

        self.interactor = interactor

    def parse_data(
        self,           # type: GetOperatorsLogoutSessionsProcessor
        data,           # type: str
        event_type,     # type: str
    ):
        # type: (...) -> str

        return data

    def call_business(
        self,       # type: GetOperatorsLogoutSessionsProcessor
        request,    # type: str
    ):
        # type: (...) -> GetOperatorsLogoutSessionsResponse

        return self.interactor.execute(business_period=request)

    def format_response(
        self,           # type: GetOperatorsLogoutSessionsProcessor
        message_bus,    # type: MessageBus
        message,        # type: Message
        event,          # type: Event
        request,        # type: str
        result,         # type: GetOperatorsLogoutSessionsResponse
    ):
        # type: (...) -> None

        response = {
            "business_period": result.business_period,
            "operators_logout_sessions": result.operators_logout_sessions,
        }

        reply = Message(
            token=DefaultToken.TK_SYS_ACK,
            data_type=DataType.string,
            data=json.dumps(response),
        )
        message_bus.reply_message(
            message=message,
            reply_message=reply,
        )
