from enum import Enum

from .grpc.course_lesson_pb2 import (
    AddCommentRequest,
    CategoryRetrieveRequest,
    ConsultantRequest,
    CourseBulkRetrieveRequest,
    CourseListRequest,
    CourseRetrieveCommentsRequest,
    CourseRetrieveRatesRequest,
    CourseRetrieveRequest,
    GeneralRetrieveRequest,
    InstallmentTermRequest,
    LessonBulkRetrieveRequest,
    LessonListRequest,
    LessonRetrieveRequest,
    MasterCourseNotificationRequest,
    MasterCoursePreRegistrationRequest,
    MasterCourseStateRequest,
    PackageListRequest,
    PackageRetrieveRequest,
    RateRequest,
    TriggerLikeRequest,
)
from .grpc.course_lesson_pb2_grpc import (
    CategoryControllerStub,
    ConsultantControllerStub,
    CourseControllerStub,
    ExpertCourseControllerStub,
    GeneralControllerStub,
    LessonControllerStub,
    MasterCourseControllerStub,
    PackageControllerStub,
    TopBannerControllerStub,
)


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


SUCCESS_CODE = "0"


class RequestMessages:
    CREATE_CONSULTANT_REQUEST = ConsultantRequest
    CREATE_NOTIFICATION = MasterCourseNotificationRequest
    CREATE_PRE_REGISTRATION = MasterCoursePreRegistrationRequest
    COMMENT = AddCommentRequest
    RATE = RateRequest
    LIKE = TriggerLikeRequest
    UNLIKE = TriggerLikeRequest
    FETCH_COURSE = CourseRetrieveRequest
    FETCH_BULK_COURSE = CourseBulkRetrieveRequest
    FETCH_ALL_COURSE = CourseListRequest
    FETCH_LESSON = LessonRetrieveRequest
    FETCH_BULK_LESSON = LessonBulkRetrieveRequest
    FETCH_ALL_LESSON = LessonListRequest
    FETCH_PACKAGE = PackageRetrieveRequest
    FETCH_ALL_PACKAGE = PackageListRequest
    FETCH_ITEM = GeneralRetrieveRequest
    FETCH_BULK_ITEM = GeneralRetrieveRequest
    FETCH_COURSE_RATES = CourseRetrieveRatesRequest
    FETCH_COURSE_STATE = MasterCourseStateRequest
    FETCH_COURSE_INSTALLMENT_TERM = InstallmentTermRequest
    FETCH_COURSE_COMMENTS = CourseRetrieveCommentsRequest
    FETCH_CATEGORY = CategoryRetrieveRequest


class RPCMethodNames:
    COMMENT = "Comment"
    CREATE = "Create"
    CREATE_NOTIFICATION = "CreateNotification"
    CREATE_PRE_REGISTRATION = "CreatePreRegistration"
    RATE = "Rate"
    LIKE = "Like"
    UNLIKE = "UnLike"
    FETCH_ONE = "Retrieve"
    FETCH_BULK = "BulkRetrieve"
    FETCH_CURRENT = "RetrieveCurrent"
    FETCH_COMMENTS = "RetrieveComments"
    FETCH_STATE = "RetrieveState"
    FETCH_INSTALLMENT_TERM = "RetrieveInstallmentTerm"
    FETCH_RATES = "RetrieveRates"
    FETCH_ALL = "List"
    HIERARCHICAL_FETCH_ALL = "HierarchicalList"


@_singletonize_class_vars
class Stubs:
    COURSE = CourseControllerStub
    CONSULTANT = ConsultantControllerStub
    MASTER_COURSE = MasterCourseControllerStub
    EXPERT_COURSE = ExpertCourseControllerStub
    LESSON = LessonControllerStub
    PAKCAGE = PackageControllerStub
    CATEGORY = CategoryControllerStub
    TOP_BANNER = TopBannerControllerStub
    GENERAL = GeneralControllerStub


class InvokerUnits(str, Enum):
    ACCOUNT_ASSET = "account_asset"
    CORE_BASKET = "core_basket"
    CORE_ASSET_MANAGEMENT = "core_asset_management"
    INVOICE_INQUIRY = "invoice_inquiry"
    PLACETAG_SITEMAP = "placetag_sitemap"
    SEARCH = "search"
