# -*- coding: utf-8 -*-

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _

from mtp.apps.assessments.admin import (
    CertificationProgramUserStatusInline,
    CompetencyTreeUserScoreInline,
    CompetencyUserScoreInline,
    DimensionUserScoreInline,
)
from mtp.apps.common.constants import SYNC_STATUS_FIELDS

from .models import Group, GroupRole, GroupSet, GroupType, MTPUser


class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'type', 'id')
    list_filter = ('type',)
    ordering = ('type', 'name')

    readonly_fields = ('id',)
    fieldsets = (
        (None, {
            'fields': (
                'name',
                'type',
                'color',
                'id',
            )
        }),
        SYNC_STATUS_FIELDS,
    )



class GroupSetAdmin(admin.ModelAdmin):
    list_display = ('name', 'id')
    filter_horizontal = ('mgroups',)

    readonly_fields = ('id',)
    fieldsets = (
        (None, {
            'fields': (
                'name',
                'mgroups',
                'id',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class GroupTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'is_stats_filter', 'is_training_filter', 'id')
    list_editable = ('is_stats_filter','is_training_filter',)
    list_filter = ('is_stats_filter','is_training_filter',)
    ordering = ('is_stats_filter', 'name')

    readonly_fields = ('id',)
    fieldsets = (
        (None, {
            'fields': (
                'name',
                'description',
                'is_stats_filter',
                'is_training_filter',
                'id',
            )
        }),
        SYNC_STATUS_FIELDS,
    )


class GroupRoleInline(admin.TabularInline):
    model = GroupRole


class MTPUserAdmin(UserAdmin):
    list_display = ('username', 'last_update', 'id')

    readonly_fields = ('id', 'date_joined', 'last_login')
    fieldsets = (
        (_("Account data"), {
            'fields': (
                'username',
                'first_name',
                'last_name',
                'email',
                'password',
                'id',
            )
        }),
        (_("MTP Profile data"), {
            'fields': (
                'photo',
                'photos',
                'natural_language',
                'dr',
            )
        }),
        (_("Administrative data"), {
            'fields': (
                'is_active',
                'is_staff',
                'is_superuser',
                'date_joined',
                'last_login',
            )
        }),
        SYNC_STATUS_FIELDS,
    )

    inlines = (
        GroupRoleInline,
        CompetencyTreeUserScoreInline,
        CompetencyUserScoreInline,
        DimensionUserScoreInline,
        CertificationProgramUserStatusInline,
    )


admin.site.register(Group, GroupAdmin)
admin.site.register(GroupSet, GroupSetAdmin)
admin.site.register(GroupType, GroupTypeAdmin)
admin.site.register(MTPUser, MTPUserAdmin)
