"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 22.07.21

"""
from django.contrib import admin

from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP


class GeometryAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "created",
        "st_area",
    ]
    readonly_fields = [
        "st_area",
        "created",
        "modified",
    ]

    def st_area(self, obj):
        val = None
        geom = obj.geom
        if geom is not None:
            geom.transform(ct=DEFAULT_SRID_RLP)
            val = geom.area
        return val
    st_area.short_description = f"Area (srid={DEFAULT_SRID_RLP})"


class ParcelAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "parcel_group",
        "flr",
        "flrstck_nnr",
        "flrstck_zhlr",
        "updated_on",
    ]


class DistrictAdmin(admin.ModelAdmin):
    list_display = [
        "name",
        "key",
        "id",
    ]


class MunicipalAdmin(admin.ModelAdmin):
    list_display = [
        "name",
        "key",
        "district",
        "id",
    ]


class ParcelGroupAdmin(admin.ModelAdmin):
    list_display = [
        "name",
        "key",
        "municipal",
        "id",
    ]


class GeometryConflictAdmin(admin.ModelAdmin):
    list_display = [
        "conflicting_geometry",
        "affected_geometry",
        "detected_on",
    ]


class AbstractDocumentAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "title",
        "comment",
        "created",
    ]


class DeadlineAdmin(admin.ModelAdmin):
    list_display = [
        "id",
        "type",
        "date",
        "comment",
    ]


class DeletableObjectMixinAdmin(admin.ModelAdmin):
    class Meta:
        abstract = True

    def restore_deleted_data(self, request, queryset):
        queryset = queryset.filter(
            deleted__isnull=False
        )
        for entry in queryset:
            entry.deleted.delete()


class BaseResourceAdmin(admin.ModelAdmin):
    fields = [
        "created",
        "modified",
    ]
    readonly_fields = [
        "modified",
        "created",
    ]


class BaseObjectAdmin(BaseResourceAdmin, DeletableObjectMixinAdmin):
    search_fields = [
        "id",
        "identifier",
        "title",
    ]
    actions = [
        "restore_deleted_data"
    ]

    def get_fields(self, request, obj=None):
        return super().get_fields(request, obj) + ["deleted"]

    def get_readonly_fields(self, request, obj=None):
        return super().get_readonly_fields(request, obj) + [
            "deleted",
        ]


class ResubmissionAdmin(BaseResourceAdmin):
    list_display = [
        "resubmit_on"
    ]
    fields = [
        "comment",
        "resubmit_on",
        "resubmission_sent",
    ]


# Outcommented for a cleaner admin backend on production
#admin.site.register(Geometry, GeometryAdmin)
#admin.site.register(Parcel, ParcelAdmin)
#admin.site.register(District, DistrictAdmin)
#admin.site.register(Municipal, MunicipalAdmin)
#admin.site.register(ParcelGroup, ParcelGroupAdmin)
#admin.site.register(GeometryConflict, GeometryConflictAdmin)
#admin.site.register(Deadline, DeadlineAdmin)
#admin.site.register(Resubmission, ResubmissionAdmin)
