from typing import List

from boss.interactor.get_info.dto import GetInfoRequest
from boss.model import User
from boss.util import convert_user_dto_into_user_response
from messagebus import (
    AckMessage,
    Event,
    Message,
    MessageBus,
)
from ._BaseProcessor import BaseProcessor


class GetUsersListProcessor(BaseProcessor):

    def parse_data(
        self,
        data: bytes,
    ) -> None:

        return None

    def format_response(
        self,
        message_bus: MessageBus,
        message: Message,
        event: Event,
        request: GetInfoRequest,
        result: List[User],
    ) -> None:

        if result is None:
            reply_message = AckMessage(data=b"<UserInfo/>")
        else:
            sorted_result = sorted(result, key=lambda x: x.user_id)
            reply_data = "<UserInfo>"
            for user_dto in sorted_result:
                user_data = convert_user_dto_into_user_response(user=user_dto)
                reply_data += "{}".format(user_data.get_info_response())
            reply_data += "</UserInfo>"

            reply_message = AckMessage(data=reply_data.encode())

        message_bus.reply_message(
            message=message,
            reply_message=reply_message,
        )
