from django import forms
from django.contrib import admin
from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin
from django_better_admin_arrayfield.forms.widgets import DynamicArrayWidget
from django_better_admin_arrayfield.models.fields import DynamicArrayField

from solo.admin import SingletonModelAdmin

from .models import (
    BatchImportation,
    Category,
    DuplicatedEvents,
    Event,
    Message,
    Organisation,
    Place,
    RecurrentImport,
    ReferenceLocation,
    StaticContent,
    Tag,
    UserProfile,
    SiteConfiguration,
    SpecialPeriod,
)

admin.site.register(SiteConfiguration, SingletonModelAdmin)

admin.site.register(Category)
admin.site.register(Tag)
admin.site.register(StaticContent)
admin.site.register(DuplicatedEvents)
admin.site.register(BatchImportation)
admin.site.register(RecurrentImport)
admin.site.register(Place)
admin.site.register(Message)
admin.site.register(ReferenceLocation)
admin.site.register(Organisation)
admin.site.register(UserProfile)
admin.site.register(SpecialPeriod)


class URLWidget(DynamicArrayWidget):
    def __init__(self, *args, **kwargs):
        kwargs["subwidget_form"] = forms.URLField()
        super().__init__(*args, **kwargs)


@admin.register(Event)
class Eventdmin(admin.ModelAdmin, DynamicArrayMixin):
    formfield_overrides = {
        DynamicArrayField: {"urls": URLWidget},
    }
