from django.contrib import admin
from .models import (
    Case,
    CaseDetail,
    CaseCollection,
    NormalCase,
    Differential,
    Series,
    SeriesImage,
    Condition,
    ConditionRelationship,
    Subspecialty,
    Finding,
    SeriesFinding,
    Structure,
    PathologicalProcess,
    Presentation,
    UncategorisedDicom,
    SeriesDetail,
    Resource,
    CaseDisplaySet,
    UserReportAnswer,
    CidReportAnswer,
)

from django.forms import ModelForm

from reversion.admin import VersionAdmin
from django.forms.widgets import RadioSelect

from tinymce.widgets import TinyMCE

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

# Register your models here.
# admin.site.register(Examination)
# admin.site.register(Site)
admin.site.register(SeriesImage)
admin.site.register(Condition)
admin.site.register(ConditionRelationship)
admin.site.register(Subspecialty)
admin.site.register(Finding)
admin.site.register(Structure)
admin.site.register(SeriesFinding)
admin.site.register(Differential)
admin.site.register(PathologicalProcess)
admin.site.register(Presentation)
admin.site.register(CaseDetail)
admin.site.register(UncategorisedDicom)
admin.site.register(SeriesDetail)
admin.site.register(Resource)
admin.site.register(CaseDisplaySet)
admin.site.register(UserReportAnswer)
admin.site.register(CidReportAnswer)


class DifferentialInline(admin.TabularInline):
    model = Differential

class CasesInline(admin.TabularInline):
    model = CaseDetail

class CaseCollectionAdminForm(ModelForm):
    class Meta:
        model = CaseCollection

        exclude = []


class CaseCollectionAdmin(VersionAdmin):
    form = CaseCollectionAdminForm

    save_on_top = True
    save_as = True
    view_on_site = True

    inlines = (CasesInline,)


admin.site.register(CaseCollection, CaseCollectionAdmin)


class NormalCaseAdmin(VersionAdmin):
    list_display = ("case", "display_age", "examination", "modality", "added_by", "added_date")
    list_filter = ("examination", "modality")
    search_fields = ("case__title", "case__pk")

    def display_age(self, obj):
        try:
            return obj.display_age()
        except Exception:
            return ""
    display_age.short_description = "Age"


admin.site.register(NormalCase, NormalCaseAdmin)

class AtlasAdminForm(ModelForm):
    class Meta:
        model = Case

        fields = [
            "title",
            "description",
            "history",
            "discussion",
            "condition",
            "differential",
            "subspecialty",
            "author",
            "series",
            "cimar_uuid"
        ]

        widgets = {
            # "normal": RadioSelect(
            # choices=[(True, 'Yes'),
            #     (False, 'No')])
            "history": TinyMCE(attrs={"cols": 80, "rows": 30}),
            "discussion": TinyMCE(attrs={"cols": 80, "rows": 30}),
        }


class AtlasAdmin(VersionAdmin):
    form = AtlasAdminForm

    save_on_top = True
    save_as = True
    view_on_site = True

    inlines = (DifferentialInline,)


admin.site.register(Case, AtlasAdmin)


class SeriesAdmin(VersionAdmin):
    pass


admin.site.register(Series, SeriesAdmin)
