# coding=utf-8
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from BaseModels.admin_utils import *
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _

from AuthApp.models import *

from django.contrib.auth.models import Group
from django.db import models
from django.contrib.admin.models import LogEntry

from django.db.models import F, Value as V
from django.db.models.functions import Concat

from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin
from django.contrib.auth.models import Group
from django.contrib.admin import SimpleListFilter


class LogEntryAdmin(admin.ModelAdmin):
    # pass
    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', 'object_repr']
    date_hierarchy = 'action_time'

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


admin.site.register(LogEntry, LogEntryAdmin)


class Admin_ProfileInline(admin.StackedInline):
    fieldsets = (
        (None, {
            'classes': ['wide'],
            'fields': (
                # ('account_type',),
                ('enable',),
                ('phone',),
                ('country', 'city'),
                ('authMailCode',),
                ('birthdate'),
                'comment', 'creator'
            )
        }),
        (_('Дополнительно'), {
            'classes': ['wide'],
            'fields': (
                ('json_data',)
            )
        }),
    )

    model = UserProfile
    can_delete = False
    extra = 1
    fk_name = 'user'

    # filter_horizontal = ['regions', 'connected_mailings']
    # raw_id_fields = ("company_obj", 'office')
    verbose_name_plural = _(u'Профиль пользователя')

    # list_display = ['company_obj', 'office', 'company_position', 'departament', 'creator']
    readonly_fields = ['creator', ]


class Admin_User(UserAdmin):

    fieldsets = (
        (None, {
            'classes': ['wide'],
            'fields': (
                ('username', 'password'),
                ('first_name', 'last_name', 'email'),
                ('is_active', 'is_staff', 'is_superuser'),
                ('groups', 'user_permissions'),
                ('last_login', 'date_joined'),
                # ('username', 'first_name', 'last_name'),
                # ('password'),
                # ('email', 'is_active'),
                # ('is_staff')
            )
        }),

    )

    save_on_top = True

    list_display = ['id', 'last_name', 'first_name', 'email', 'is_staff',
                    'is_active']
    list_editable = ['is_staff', 'is_active']
    list_display_links = ['first_name', 'last_name', 'email']
    search_fields = ['first_name', 'last_name', 'email']

    inlines = (Admin_ProfileInline,)
    # actions = ['del_all_temp_users', ]

    ordering = ['is_staff', 'last_name', 'first_name']

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, Admin_User)


# class Admin_UserProfile(Admin_BaseIconModel):
#
#     fieldsets = (
#         (None, {
#             'classes': ['wide'],
#             'fields': (
#                 'user', 'enable',
#                 ('account_type',),
#                 ('phone',),
#                 ('country', 'city'),
#                 ('authMailCode',),
#                 ('birthdate'),
#                 'creator'
#             )
#         }),
#         ('1С', {
#             'classes': ['wide'],
#             'fields': (
#                 ('name',),
#             )
#         }),
#     )
#
#     save_on_top = True
#
#     list_display = [
#         'id', 'user', 'enable', 'birthdate', 'modifiedDT', 'createDT'
#     ]
#     list_editable = ['enable', 'birthdate']
#     list_display_links = ['id', ]  # 'user__last_name', 'user__first_name']
#     search_fields = [
#         'id', 'user__last_name', 'user__first_name', 'user__email',
#     ]
#
#     list_filter = ['enable', 'account_type']
#
#     # filter_horizontal = ['connected_mailings']
#     # raw_id_fields = ("favourites",)
#     verbose_name_plural = _(u'Профиль пользователя')
#
#
# admin.site.register(UserProfile, Admin_UserProfile)
