from event_manager.async_response.dto import AsyncResponseRequest
from model import EventQueue, EventProcessorListener
from timeutil import Clock


class AsyncResponseInteractor(object):
    def __init__(
        self,
        event_queue: EventQueue,
        event_ack_listener: EventProcessorListener,
        clock: Clock,
    ):
        self.event_queue = event_queue
        self.event_ack_listener = event_ack_listener
        self.clock = clock

    def process_async_response(self, request: AsyncResponseRequest):
        publish_detail = self.event_queue.get_by_async_target_subject_and_id(
            reply_subject=request.subject,
            event_data=request.data,
        )
        if publish_detail is None:
            return
        if publish_detail.is_finished:
            return
        publish_detail.acked_at = self.clock.utc_now()
        self.event_ack_listener.event_processed(publish_detail=publish_detail)
