from typing import Type

from messageprocessor import MessageProcessor
from simplehttprouter import Route

from edpopenapibuilder.builder import OpenApiModelBuilder
from edpopenapibuilder.builder.descriptors import PathModelDescriptor, Descriptors, PayloadDescriptor
from edpopenapibuilder.builder.impl import OpenApiModelBuilderImpl

_DEFAULT_BUILDER = None
_DEFAULT_DESCRIPTORS = None


def get_descriptors() -> Descriptors:
    global _DEFAULT_DESCRIPTORS
    if _DEFAULT_DESCRIPTORS is None:
        _DEFAULT_DESCRIPTORS = Descriptors()
    return _DEFAULT_DESCRIPTORS


def get_builder() -> OpenApiModelBuilder:
    global _DEFAULT_BUILDER
    if _DEFAULT_BUILDER is None:
        _DEFAULT_BUILDER = OpenApiModelBuilderImpl(
            descriptors=get_descriptors(),
        )
    return _DEFAULT_BUILDER


def add_route_descriptor(route: Route, descriptor: PathModelDescriptor):
    get_descriptors().add_route_descriptor(route, descriptor)


def add_processor_descriptor(processor_type: Type[MessageProcessor], descriptor: PathModelDescriptor):
    get_descriptors().add_processor_descriptor(processor_type, descriptor)


def add_interactor_descriptor(interactor_type: Type, descriptor: PathModelDescriptor):
    get_descriptors().add_interactor_descriptor(interactor_type, descriptor)


def add_payload_descriptor(payload_type: Type, descriptor: PayloadDescriptor):
    get_descriptors().add_payload_descriptor(payload_type, descriptor)
