from django import forms
from django.contrib import admin

from vbv_lernwelt.course_session.models import (
    CourseSessionAssignment,
    CourseSessionAttendanceCourse,
    CourseSessionEdoniqTest,
)
from vbv_lernwelt.learnpath.models import Circle


@admin.register(CourseSessionAttendanceCourse)
class CourseSessionAttendanceCourseAdmin(admin.ModelAdmin):
    readonly_fields = [
        "course_session",
        "learning_content",
        "due_date",
        "attendance_user_list",
    ]
    list_display = [
        "course_session",
        "circle",
        "learning_content",
        "start_date",
        "end_date",
        "trainer",
        "location",
    ]
    list_filter = ["course_session__course", "course_session"]

    def start_date(self, obj):
        return obj.due_date.start

    start_date.admin_order_field = "due_date__start"

    def end_date(self, obj):
        return obj.due_date.end

    end_date.admin_order_field = "due_date__end"

    def circle(self, obj):
        circle = obj.get_circle()
        if circle:
            return circle.title
        return ""

    # Create a method that serves as a form field
    def circle_display(self, obj=None):
        if obj:
            return self.circle(obj)
        return None

    circle_display.short_description = "Circle"

    # Make circle display-only in the form
    def get_readonly_fields(self, request, obj=None):
        readonly_fields = super(
            CourseSessionAttendanceCourseAdmin, self
        ).get_readonly_fields(request, obj)
        return readonly_fields + ["circle_display"]

    # Override get_form to include circle_display
    def get_form(self, request, obj=None, **kwargs):
        form = super(CourseSessionAttendanceCourseAdmin, self).get_form(
            request, obj, **kwargs
        )
        form.base_fields["circle_display"] = forms.CharField(
            required=False,
            label="Circle",
            widget=forms.TextInput(attrs={"readonly": "readonly"}),
        )
        return form


@admin.register(CourseSessionAssignment)
class CourseSessionAssignmentAdmin(admin.ModelAdmin):
    list_display = [
        "course_session",
        "circle",
        "learning_content",
        "submission_date",
        "evaluation_date",
    ]
    list_filter = ["course_session__course", "course_session"]
    raw_id_fields = [
        "course_session",
        "submission_deadline",
        "evaluation_deadline",
    ]

    def submission_date(self, obj):
        if obj.submission_deadline:
            return obj.submission_deadline.start
        return None

    submission_date.admin_order_field = "submission_deadline__start"

    def evaluation_date(self, obj):
        if obj.evaluation_deadline:
            return obj.evaluation_deadline.start
        return None

    evaluation_date.admin_order_field = "evaluation_deadline__start"

    def circle(self, obj):
        try:
            return obj.learning_content.get_ancestors().exact_type(Circle).first().title
        except Exception:
            # noop
            pass
        return None

    # Create a method that serves as a form field
    def circle_display(self, obj=None):
        if obj:
            return self.circle(obj)
        return None

    circle_display.short_description = "Circle"

    # Make circle display-only in the form
    def get_readonly_fields(self, request, obj=None):
        readonly_fields = super(CourseSessionAssignmentAdmin, self).get_readonly_fields(
            request, obj
        )
        return ["circle_display"]

    # Override get_form to include circle_display
    def get_form(self, request, obj=None, **kwargs):
        form = super(CourseSessionAssignmentAdmin, self).get_form(
            request, obj, **kwargs
        )
        form.base_fields["circle_display"] = forms.CharField(
            required=False,
            label="Circle",
            widget=forms.TextInput(attrs={"readonly": "readonly"}),
        )
        return form


@admin.register(CourseSessionEdoniqTest)
class CourseSessionEdoniqTestAdmin(admin.ModelAdmin):
    readonly_fields = [
        "course_session",
        "learning_content",
        "deadline",
    ]
    list_display = [
        "course_session",
        "circle",
        "learning_content",
        "deadline_date",
    ]
    list_filter = ["course_session__course", "course_session"]

    def deadline_date(self, obj):
        if obj.deadline:
            return obj.deadline.start
        return None

    deadline_date.admin_order_field = "deadline__start"

    def circle(self, obj):
        try:
            return obj.learning_content.get_ancestors().exact_type(Circle).first().title
        except Exception:
            # noop
            pass
        return None

    # Create a method that serves as a form field
    def circle_display(self, obj=None):
        if obj:
            return self.circle(obj)
        return None

    circle_display.short_description = "Circle"

    # Make circle display-only in the form
    def get_readonly_fields(self, request, obj=None):
        readonly_fields = super(CourseSessionEdoniqTestAdmin, self).get_readonly_fields(
            request, obj
        )
        return readonly_fields + ["circle_display"]

    # Override get_form to include circle_display
    def get_form(self, request, obj=None, **kwargs):
        form = super(CourseSessionEdoniqTestAdmin, self).get_form(
            request, obj, **kwargs
        )
        form.base_fields["circle_display"] = forms.CharField(
            required=False,
            label="Circle",
            widget=forms.TextInput(attrs={"readonly": "readonly"}),
        )
        return form
