from typing import (
    Optional,
    Callable,
    Dict,
)

from messagebus import (
    Event,
)


class SyncEventRequestValidator(object):
    def __init__(
        self,
        subject: str,
        event_type: Optional[str],
        data_validator: Optional[Callable[[bytes], bool]],
        headers_validator: Optional[Callable[[Dict[str, str]], bool]],
        callback: Callable[[Event], bool],
    ):
        self.subject = subject
        self.event_type = event_type
        self.data_validator = data_validator
        self.headers_validator = headers_validator
        self.callback = callback

    def is_valid_for(
        self,
        event: Event,
    ):
        if not event.synchronous:
            return False

        if event.subject != self.subject:
            return False

        if self.event_type is not None and event.event_type != self.event_type:
            return False

        if self.data_validator is not None and not self.data_validator(event.data):
            return False

        if self.headers_validator is not None:
            return self.headers_validator(event.headers)

        if self.callback is not None:
            return self.callback(event)

        return True
