from django.contrib import admin
from .models import *
from unfold.admin import ModelAdmin as UnfoldModelAdmin

from import_export.admin import ImportExportModelAdmin
from unfold.contrib.import_export.forms import ExportForm, ImportForm, SelectableFieldsExportForm
from unfold.contrib.forms.widgets import ArrayWidget, WysiwygWidget
from django.contrib.postgres.fields import ArrayField
from unfold.decorators import action, display
from django.utils.html import format_html
from unfold.decorators import display
from utils.admin import ModelAdmin
from unfold.contrib.filters.admin import ChoicesDropdownFilter
from django.core.cache import cache

# Cache key for home page
HOME_CACHE_KEY = 'home_view_data_anonymous'


@admin.register(ShowCaseSlider)
class ShowCaseSliderAdmin(ModelAdmin, ImportExportModelAdmin):
    import_form_class = ImportForm
    export_form_class = ExportForm
    search_fields = ['title']

    compressed_fields = False
    warn_unsaved_form = True

    formfield_overrides = {
        ArrayField: {
            "widget": ArrayWidget,
        }
    }
    
    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        cache.delete(HOME_CACHE_KEY)

@admin.register(LearnVideoModel)
class LearnVideoAdmin(UnfoldModelAdmin):
    list_display = ['title', 'section', 'display_viewd']
    search_fields = ['title', 'section', 'content_type__app_label']
    # autocomplete_fields = ['section']
    compressed_fields = False
    list_filter = ['viewd', 'section']
    warn_unsaved_form = True
    list_filter_submit = True
    change_form_template = 'video_change_form_after.html'
    formfield_overrides = {
        ArrayField: {
            "widget": ArrayWidget,
        }
    }
    def has_delete_permission(self, request, obj=None):
        return request.user.video_uploader

    def has_add_permission(self, request, obj=None):
        return request.user.video_uploader

    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        cache.delete(HOME_CACHE_KEY)


    @display(description='دیده شده')
    def display_viewd(self, instance):
        if instance.viewd:
            svg = f'<a href="/home/video/view/{instance.id}"><svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" viewBox="0 0 24 24"><path fill="green" d="M12 16q1.875 0 3.188-1.312T16.5 11.5t-1.312-3.187T12 7T8.813 8.313T7.5 11.5t1.313 3.188T12 16m0-1.8q-1.125 0-1.912-.788T9.3 11.5t.788-1.912T12 8.8t1.913.788t.787 1.912t-.787 1.913T12 14.2m0 4.8q-3.65 0-6.65-2.037T1 11.5q1.35-3.425 4.35-5.462T12 4t6.65 2.038T23 11.5q-1.35 3.425-4.35 5.463T12 19m0-2q2.825 0 5.188-1.487T20.8 11.5q-1.25-2.525-3.613-4.012T12 6T6.813 7.488T3.2 11.5q1.25 2.525 3.613 4.013T12 17"/></svg></a>'
        else:
            svg = f'<a href="/home/video/view/{instance.id}"><svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" viewBox="0 0 24 24"><path fill="#c30009" d="m19.1 21.9l-3.5-3.45q-.875.275-1.775.413T12 19q-3.35 0-6.125-1.8t-4.35-4.75q-.125-.225-.187-.462t-.063-.488t.063-.488t.187-.462q.55-.975 1.175-1.9T4.15 7L2.075 4.9Q1.8 4.625 1.8 4.213t.3-.713q.275-.275.7-.275t.7.275l17 17q.275.275.288.688t-.288.712q-.275.275-.7.275t-.7-.275M12 16q.275 0 .525-.025t.5-.1l-5.4-5.4q-.075.25-.1.5T7.5 11.5q0 1.875 1.313 3.188T12 16m0-12q3.35 0 6.138 1.813t4.362 4.762q.125.2.188.438t.062.487t-.05.488t-.175.437q-.475.925-1.062 1.75t-1.313 1.55q-.35.35-.825.325t-.825-.375l-2-2q-.175-.175-.225-.413t.025-.487q.1-.325.15-.625t.05-.65q0-1.875-1.312-3.187T12 7q-.35 0-.65.05t-.625.15q-.25.075-.5.025T9.8 7l-.825-.825q-.475-.475-.312-1.1t.787-.8q.625-.125 1.263-.2T12 4m1.975 5.65q.275.325.462.713t.238.812q.025.2-.15.275t-.325-.075l-2.05-2.05Q12 9.175 12.088 9t.287-.175q.475.05.875.263t.725.562"/></svg></a>'

        return format_html(
            svg
        )




@admin.register(SliderModel)
class SliderAdmin(ModelAdmin, ImportExportModelAdmin):
    import_form_class = ImportForm
    export_form_class = ExportForm
    search_fields = ['description', 'title']

    compressed_fields = False
    warn_unsaved_form = True

    formfield_overrides = {
        ArrayField: {
            "widget": ArrayWidget,
        }
    }
    
    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        cache.delete(HOME_CACHE_KEY)

@admin.register(HomeImageModel)
class HomeImageAdmin(ModelAdmin, ImportExportModelAdmin):
    import_form_class = ImportForm
    export_form_class = ExportForm
    

    compressed_fields = True
    warn_unsaved_form = True

    formfield_overrides = {
        ArrayField: {
            "widget": ArrayWidget,
        }
    }
    
    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        cache.delete(HOME_CACHE_KEY)


# admin.py
from django.contrib import admin
from unfold.admin import ModelAdmin

from django_celery_beat.models import (
    ClockedSchedule,
    CrontabSchedule,
    IntervalSchedule,
    PeriodicTask,
    SolarSchedule,
)
from django_celery_beat.admin import ClockedScheduleAdmin as BaseClockedScheduleAdmin
from django_celery_beat.admin import CrontabScheduleAdmin as BaseCrontabScheduleAdmin
from django_celery_beat.admin import PeriodicTaskAdmin as BasePeriodicTaskAdmin
from django_celery_beat.admin import PeriodicTaskForm, TaskSelectWidget
from unfold.widgets import *
admin.site.unregister(PeriodicTask)
admin.site.unregister(IntervalSchedule)
admin.site.unregister(CrontabSchedule)
admin.site.unregister(SolarSchedule)
admin.site.unregister(ClockedSchedule)


class UnfoldTaskSelectWidget(UnfoldAdminSelectWidget, TaskSelectWidget):
    pass


class UnfoldPeriodicTaskForm(PeriodicTaskForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["task"].widget = UnfoldAdminTextInputWidget()
        self.fields["regtask"].widget = UnfoldTaskSelectWidget()


@admin.register(PeriodicTask)
class PeriodicTaskAdmin(BasePeriodicTaskAdmin, ModelAdmin):
    form = UnfoldPeriodicTaskForm


@admin.register(IntervalSchedule)
class IntervalScheduleAdmin(ModelAdmin):
    pass


@admin.register(CrontabSchedule)
class CrontabScheduleAdmin(BaseCrontabScheduleAdmin, ModelAdmin):
    pass


@admin.register(SolarSchedule)
class SolarScheduleAdmin(ModelAdmin):
    pass

@admin.register(ClockedSchedule)
class ClockedScheduleAdmin(BaseClockedScheduleAdmin, ModelAdmin):
    pass