from longs.models import Exam as LongExam
from rapids.models import Exam as RapidExam
from anatomy.models import Exam as AnatomyExam
from physics.models import Exam as PhysicsExam
from sbas.models import Exam as SbasExam
from atlas.models import CaseCollection as AtlasExam

from django.contrib import admin

# Register your models here.
from .models import (
    CidUserGroup,
    CimarCase,
    Examination,
    # Sign,
    Modality,
    Plane,
    Contrast,
    QuestionNote,
    NoteType,
    CidUser,
    CidUserExam,
    ExamUserStatus,
    UserGrades,
    UserProfile,
    Supervisor,
    ExamCollection,
    Site,
)


def custom_title_filter_factory(filter_cls, title):
    class Wrapper(filter_cls):
        def __new__(cls, *args, **kwargs):
            instance = filter_cls(*args, **kwargs)
            instance.title = title
            return instance

    return Wrapper


# from .models import Examination, Sign, Site, Condition

admin.site.register(Examination)
# admin.site.register(Sign)
admin.site.register(Site)
# admin.site.register(Condition)
admin.site.register(Plane)
admin.site.register(Contrast)
admin.site.register(QuestionNote)
admin.site.register(NoteType)
admin.site.register(CidUser)
admin.site.register(CidUserGroup)
admin.site.register(UserProfile)
admin.site.register(UserGrades)
admin.site.register(Supervisor)
admin.site.register(Modality)
admin.site.register(ExamCollection)
admin.site.register(CimarCase)


class ExamListFilter(admin.SimpleListFilter):
    # title = "Long Exams"
    # parameter_name = "exam"
    # exam_model = None

    def __init__(self, request, params, model, model_admin):
        self.parameter_name = self.related_field_name
        super().__init__(request, params, model, model_admin)

    def lookups(self, request, model_admin):
        return [(exam.id, exam.name) for exam in self.exam_model.objects.all()]

    def queryset(self, request, queryset):
        if self.value():
            # return queryset.filter(longs_exams__id=self.value())
            kwargs = {f"{self.related_field_name}__id": self.value()}
            return queryset.filter(**kwargs)
        return queryset


class LongExamListFilter(ExamListFilter):
    title = "Long Exams"
    related_field_name = "longs_exams"
    exam_model = LongExam


class RapidExamListFilter(ExamListFilter):
    title = "Rapid Exams"
    related_field_name = "rapids_exams"
    exam_model = RapidExam


class AnatomyExamListFilter(ExamListFilter):
    title = "Anatomy Exams"
    related_field_name = "anatomy_exams"
    exam_model = AnatomyExam


class PhysicsExamListFilter(ExamListFilter):
    title = "Physics Exams"
    related_field_name = "physics_exams"
    exam_model = PhysicsExam


class SbasExamListFilter(ExamListFilter):
    title = "Sbas Exams"
    related_field_name = "sbas_exams"
    exam_model = SbasExam


class AtlasExamListFilter(ExamListFilter):
    title = "Atlas Exams"
    related_field_name = "atlas_exams"
    exam_model = AtlasExam


class CidUserExamAdmin(admin.ModelAdmin):
    list_filter = (
        "cid_user",
        "user_user",
        "completed",
        LongExamListFilter,
        RapidExamListFilter,
        AnatomyExamListFilter,
        PhysicsExamListFilter,
        SbasExamListFilter,
        AtlasExamListFilter,
    )

    def get_queryset(self, request):
        qs = (
            super()
            .get_queryset(request)
            .prefetch_related("cid_user", "user_user", "exam")
        )
        return qs


admin.site.register(CidUserExam, CidUserExamAdmin)


class ExamUserStatusAdmin(admin.ModelAdmin):

    list_filter = (
        "status",
        "cid_user_exam__user_user",
        "cid_user_exam__cid_user",
        LongExamListFilter,
        RapidExamListFilter,
        AnatomyExamListFilter,
        PhysicsExamListFilter,
        SbasExamListFilter,
        AtlasExamListFilter,
    )

    exclude = (
        "anatomy_exams",
        "longs_exams",
        "rapids_exams",
        "physics_exams",
        "sbas_exams",
        "atlas_exams",
    )

    readonly_fields = ("cid_user_exam",)

    def get_queryset(self, request):
        qs = super().get_queryset(request).prefetch_related("cid_user_exam", "exam")
        return qs


admin.site.register(ExamUserStatus, ExamUserStatusAdmin)
