from sets.admin import *
from .models import *
from django.contrib import admin
from django.utils.translation import gettext_lazy as _
# from nested_inline.admin import NestedStackedInline, NestedTabularInline, NestedModelAdmin, NestedInline
from super_inlines.admin import SuperInlineModelAdmin, SuperModelAdmin
from django.contrib.admin.models import LogEntry


class LogEntryAdmin(admin.ModelAdmin):
    list_display = (
        '__str__', 'action_time', 'user', 'content_type', 'object_id', 'object_repr', 'action_flag', 'change_message'
    )
    list_filter = ('content_type', 'action_flag')
    search_fields = ['user__username', 'change_message', 'object_id']
    date_hierarchy = 'action_time'

    def has_delete_permission(self, request, obj=None):
        return False

admin.site.register(LogEntry, LogEntryAdmin)



class Admin_Inline_WidgetForBlock(SuperInlineModelAdmin, Admin_Trans_BaseIconStackedInline):

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        field = super(Admin_Inline_WidgetForBlock, self).formfield_for_dbfield(db_field, request, **kwargs)
        return sets_for_formfield_for_dbfield(field, db_field)
        # return field

    model = WidgetForBlock
    extra = 0
    # fields = ['title', 'description', 'picture', 'bg_color', 'but_title', 'but_color', 'url']
    fieldsets = [
        (None, {
            'classes': [],
            'fields': [
                (
                    # 'title',
                    'name', 'description', 'text',
                    'picture',
                ),
                'video_url',
                ('bg_color', ),
                ('but_title', 'but_color', 'but_icon'),
                ('url', 'order'),
            ]
        })
    ]



class Admin_StackedInline_Block(Admin_Trans_GenericBaseIconStackedInline, SuperInlineModelAdmin):

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        field = super(Admin_StackedInline_Block, self).formfield_for_dbfield(db_field, request, **kwargs)
        return sets_for_formfield_for_dbfield(field, db_field)

    model = Block
    extra = 0
    fieldsets = [
        (None, {
            'classes': [],
            'fields': [
                ('block_type',
                 # 'title',
                 'name', 'description'),
                ('picture', ),
                ('video_url',),
                ('bg_color', 'bg_image',),
                # ('bg_video_url',),
                ('but_title', 'but_color', 'but_icon'),
                ('url',),
                (
                    # 'child_in_row_count',
                    'order',
                ),
            ]
        })
    ]

    inlines = [Admin_Inline_WidgetForBlock]





class Admin_StaticPage(SuperModelAdmin, Admin_Trans_BaseModelViewPage):
    from ServicesApp.admin import Admin_StackedInline_BlockPluginPresentation
    from SlidesApp.admin import Admin_StackedInline_Slide

    fieldsets = [
        (None, {
            'classes': ['wide'],
            'fields': ('name',
                       'url',
                       'title',
                       'description', 'text',
                       'picture',
                       'order',
                       )
        }),
        # ('SEO', {
        #     'classes': ['wide', 'collapse'],
        #     'fields': (
        #         'seo_title', 'seo_description', 'seo_keywords', 'seo_text',
        #    )
        # }),
    ]


    list_display = [
        'image_thumb',
        'id',
        'name', 'url',
        # 'title',
        'order', 'modifiedDT', 'createDT'
    ]

    list_display_links = ['id', 'image_thumb', 'name']
    list_editable = ['order']

    list_filter = ['modifiedDT', 'createDT']
    search_fields = ['name', 'title']
    # filter_horizontal = ['options']

    inlines = [
        Admin_StackedInline_Slide,
        Admin_StackedInline_BlockPluginPresentation,
        Admin_StackedInline_Block,
        # AdminStacked_FAQitem,
    ]

    def has_delete_permission(self, request, obj=None):
        if request.user.is_superuser:
           return True

        if obj.url in ('main', 'contacts'):
            return False

admin.site.register(StaticPage,Admin_StaticPage)

class Admin_Block(Admin_Trans_BaseModelViewPage):

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        field = super(Admin_Block, self).formfield_for_dbfield(db_field, request, **kwargs)
        return sets_for_formfield_for_dbfield(field, db_field)

    def obj_name(self, obj):
        try:
            return obj.content_object
        except Exception as e:
            return obj.content_object.id
    obj_name.short_description = _('Название')
    obj_name.allow_tags = True

    fieldsets = [
        (None, {
            'classes': [],
            'fields': [
                ('block_type',
                 # 'title',
                 'name', 'description'),
                ('picture',),
                ('video_url',),
                ('bg_color', 'bg_image'),
                # ('bg_video_url', ),
                ('but_title', 'but_color', 'but_icon'),
                ('url',),
                (
                    # 'child_in_row_count',
                    'order',
                ),
            ]
        })
    ]

    inlines = [Admin_Inline_WidgetForBlock]

    list_display = [
        'name', 'block_type', 'enable', 'content_type', 'content_object', 'order',
        'object_id', 'createDT', 'modifiedDT'
    ]
    list_editable = ['enable', 'order']
    search_fields = ['name']
    list_filter = ['enable', 'content_type', 'block_type']
    ordering = ['content_type', 'object_id', 'order']

    def has_delete_permission(self, request, obj=None):
        if request.user.is_superuser:
           return True

        if obj.name in ('About US', 'machines', 'works'):
            return False

    def has_add_permission(self, request):
        return False

admin.site.register(Block, Admin_Block)

class Admin_Option(Admin_Trans_BaseModel):

    # def get_fieldsets(self, request, obj=None):
    #     fieldsets = super(type(self), self).get_fieldsets(request, obj)
    #     fieldsets.insert(
    #         1, ('Контент', {
    #             'classes': ['wide'],
    #             'fields': (
    #                 'opt_type', 'prefix', 'value', 'picture'
    #             )
    #
    #         })
    #     )
    #     return fieldsets
    fieldsets = [
        (_('Контент'), {
            'classes': ['wide'],
            'fields': (
                'name', 'opt_type', 'order', 'prefix', 'value', 'picture'
            )
        }),
    ]

    list_display = ['image_thumb', 'opt_type', 'name', 'order', 'value', 'prefix']
    list_editable = ['value', 'prefix', 'order']
    list_filter = ['opt_type']

admin.site.register(Option,Admin_Option)



class AdminTabular_ContactModel(Admin_Trans_BaseIconTabularInline):

    fields = [
        'name', 'contact_type', 'contact_label', 'contact_prefix', 'contact_data', 'order'
    ]

    model = Contact
    extra = 1

    ordering = ['order','contact_type', 'contact_label']




class Admin_Office(Admin_Trans_BaseModelViewPage):

    fieldsets = [
        (None, {
            'classes': ['wide'],
            'fields': (
                # 'main_office',
                'city',
                'address',
                'picture',
                (
                    'work_time_from', 'work_time_to',
                    'workTime',
                ),
                # ('legal_name', 'unp', 'legal_address', 'urInfo'),
                # 'fullContactInfo',
                # 'footer_info',
                # 'gps_longitude',
                # 'gps_latitude',
                # 'map_code'
            )
        }),
    ]



    save_on_top = True
    # prepopulated_fields = {"url": ("name",)}
    list_display = [
        'image_thumb', 'id', 'enable', 'order', 'city', 'address', 'work_time_from', 'work_time_to',
        # 'main_office',
        'modifiedDT', 'createDT'
    ]
    list_editable = [
        # 'work_time_from', 'work_time_to',
        # 'main_office',
        'enable',
        'order'
    ]
    list_display_links = ['id', 'city', 'address']

    search_fields = ['city', 'address', 'id']
    list_filter = [
        'enable',
        # 'main_office',
        'modifiedDT', 'createDT'
    ]

    inlines = [AdminTabular_ContactModel]

    # def has_delete_permission(self, request, obj=None):
    #     return not Office.objects.all().count() < 2

admin.site.register(Office, Admin_Office)
