import os

from django.contrib import admin
from django.contrib.admin import AdminSite
from django.contrib.admin.sites import NotRegistered
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from service.layer_service import LayerService
from service.models import AO
from service.models import PlacementPoint
from service.models import Rayon
from service.models import PrePlacementPoint, Post_and_pvz, Post_and_pvzCategory, Post_and_pvzGroup, OtherObjects, \
    OtherObjectsGroup, \
    OtherObjectsCategory, PrePlacementPointPVZDistance, TempFiles, RaschetGroups
from service.models import PlacementPointPVZDistance, TaskStatus
from postamates.settings import DEBUG
from django.core.cache import cache
from service.utils import run_psql_command


class MyAdminSite(AdminSite):
    def index(self, request, extra_context=None):
        if extra_context is None:
            extra_context = {}
        extra_context['is_superadmin'] = request.user.is_superuser
        return super(MyAdminSite, self).index(request, extra_context)


my_admin_site = MyAdminSite(name='POSTNET')
if DEBUG:
    my_admin_site.register(AO)
    my_admin_site.register(Rayon)
    my_admin_site.register(TempFiles)
    my_admin_site.register(PlacementPointPVZDistance)


class Post_and_PVZAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        LayerService().count_post_pvz_for_placementpoint(obj)
        RaschetGroups.objects.create(obj_id=obj.group.id)


my_admin_site.register(Post_and_pvz, Post_and_PVZAdmin)
my_admin_site.register(OtherObjects)
my_admin_site.register(PrePlacementPoint)
my_admin_site.register(PrePlacementPointPVZDistance)


class TaskStatusAdmin(admin.ModelAdmin):
    list_display = ('task_name', 'status')


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'visible')

    def save_model(self, request, obj, form, change):
        obj.save()
        cache.clear()

    def delete_model(self, request, obj):
        obj.delete()
        cache.clear()


class PostPvzCategoryAdmin(CategoryAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        if 'include_in_ml' in form.changed_data or 'visible' in form.changed_data:
            LayerService.update_categories(obj)
        cache.clear()

    def delete_model(self, request, obj):
        if obj.id in (1, 2):
            pass
        else:
            super().delete_model(request, obj)


class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'category', 'visible')

    def save_model(self, request, obj, form, change):
        obj.save()
        cache.clear()

    def delete_model(self, request, obj):
        obj.delete()
        cache.clear()


class PostPvzGroupAdmin(GroupAdmin):
    def save_model(self, request, obj, form, change):
        if not obj.pk:
            run_psql_command()
        obj.save()
        if 'include_in_ml' in form.changed_data or 'visible' in form.changed_data:
            LayerService.update_groups(obj)
        cache.clear()


class OtherObjectsGroupAdmin(GroupAdmin):
    def save_model(self, request, obj, form, change):
        if obj.id in (4, 5) or obj.id in list(range(22, 43)):
            pass
        else:
            super().save_model(request, obj, form, change)

    def delete_model(self, request, obj):
        if obj.id in (4, 5) or obj.id in list(range(22, 43)):
            pass
        else:
            super().delete_model(request, obj)


class OtherObjectsCategoryAdmin(CategoryAdmin):

    def save_model(self, request, obj, form, change):
        if obj.id in (4, 5) or obj.id in list(range(22, 43)):
            pass
        else:
            super().save_model(request, obj, form, change)

    def delete_model(self, request, obj):
        if obj.id in (4, 5) or obj.id in list(range(22, 43)):
            pass
        else:
            super().delete_model(request, obj)


class PlacementPointAdmin(admin.ModelAdmin):
    pass


my_admin_site.register(TaskStatus, TaskStatusAdmin)
my_admin_site.register(Post_and_pvzGroup, PostPvzGroupAdmin)
my_admin_site.register(OtherObjectsGroup, OtherObjectsGroupAdmin)
my_admin_site.register(Post_and_pvzCategory, PostPvzCategoryAdmin)
my_admin_site.register(OtherObjectsCategory, OtherObjectsCategoryAdmin)
my_admin_site.register(PlacementPoint, PlacementPointAdmin)


class UserAdmin(UserAdmin):
    add_fieldsets = (
        (
            None, {
                'classes': ('wide',),
                'fields': ('username', 'password1', 'password2', 'email'),
            },
        ),
    )


try:
    my_admin_site.unregister(User)
except NotRegistered:
    pass

my_admin_site.register(User, UserAdmin)
