from typing import Optional
from messageprocessor import MessageProcessor
from uuid import uuid4

from .._request_context import get_request_id


class UuidMessageProcessor(MessageProcessor):

    def __init__(self, event_name=None):
        # type: (Optional[str]) -> None
        if event_name is not None:
            self.event_name = event_name
        else:
            self.event_name = type(self).__name__

    def get_processor_name(self):
        # type: () -> str
        if self.event_name is not None:
            return self.event_name

        return self.__class__.__name__

    def generate_unique_id(self):
        # type: () -> str
        request_id = get_request_id()
        if request_id is not None:
            return request_id

        return str(uuid4())
