from dmmc.grpc.media_app_pb2 import MediaBulkRetrieveRequest, MediaRetrieveRequest
from dmmc.grpc.media_app_pb2_grpc import MediaControllerStub

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:
    FETCH_ONE_MEDIA = MediaRetrieveRequest
    FETCH_BULK_MEDIA = MediaBulkRetrieveRequest


class RPCMethodNames:
    FETCH_ONE = "Retrieve"
    FETCH_BULK = "BulkRetrieve"


@_singletonize_class_vars
class Stubs:
    MEDIA = MediaControllerStub
