from .grpc.place_tag_pb2 import (
    ContentRequest,
    MediaRequest,
    SeoRequest,
    SeoSearchRequest,
    SitemapRequest,
)
from .grpc.place_tag_pb2_grpc import (
    ContentControllerStub,
    MediaControllerStub,
    SeoControllerStub,
)

SUCCESS_CODE = "0"


def _singletonize_class_vars(cls):
    for attr_name, attr_value in cls.__dict__.items():
        if isinstance(attr_value, type):  # it's a class
            orig_cls = attr_value

            class SingletonWrapper(orig_cls):
                _instances = {}

                def __new__(cls, *args, **kwargs):
                    if cls not in cls._instances:
                        cls._instances[cls] = super().__new__(cls)
                    return cls._instances[cls]

            setattr(cls, attr_name, SingletonWrapper)
    return cls


class RequestMessages:
    ADD_MEDIA = MediaRequest
    ADD_CONTENT = ContentRequest
    ADD_SEO = SeoRequest
    SITEMAP = SitemapRequest
    # search messages are all the same so we use one
    SEARCH = SeoSearchRequest


class RPCMethodNames:
    CREATE = "Create"
    SEARCH = "Search"
    GET_SITEMAP = "GetSitemap"


@_singletonize_class_vars
class Stubs:
    MEDIA = MediaControllerStub
    CONTENT = ContentControllerStub
    SEO = SeoControllerStub


class Entities:
    COURSE = "course"
    LESSON = "lesson"
    MASTER_COURSE = "master_course"
    BLOG = "blog"
