""" Adds management of the mahong ranking system to the admin interface. """

from django.contrib import admin
from django.utils.translation import ugettext as _

from . import models, set_dirty


def recalculate(modeladmin, request, queryset):  # Ignore PyLintBear (W0613)
    """ An admin action to force recalculation of the selected items. """
    if isinstance(modeladmin, HanchanAdmin):
        for hanchan in queryset:
            hanchan.save()
    elif isinstance(modeladmin, EventRankingAdmin):
        for event_ranking in queryset:
            set_dirty(event=event_ranking.event_id, user=event_ranking.user_id)
    elif isinstance(modeladmin, KyuDanAdmin):
        for kyu_dan_ranking in queryset:
            set_dirty(user=kyu_dan_ranking.user_id)
    elif isinstance(modeladmin, SeasonRankingAdmin):
        for ladder_ranking in queryset:
            set_dirty(user=ladder_ranking.user_id,
                      season=ladder_ranking.season)
recalculate.short_description = _("Recalculate")


def confirm(modeladmin, request, queryset):  # Ignore PyLintBear (W0613)
    """An admin action to quickly set selected hanchans to confirmed. """
    for hanchan in queryset:
        hanchan.confirmed = True
        hanchan.save()


confirm.short_description = _("Confirm")


def unconfirm(modeladmin, request, queryset):  # Ignore PyLintBear (W0613)
    """An admin action to quickly set selected hanchans to unconfirmed. """
    for hanchan in queryset:
        hanchan.confirmed = False
        hanchan.save()


unconfirm.short_description = _('Set unconfirmed')


class EventRankingAdmin(admin.ModelAdmin):
    """ Lists the Event Rankings and allows to recalculate them. """
    list_filter = ['event']
    list_display = ('placement', 'user', 'event', 'avg_placement', 'avg_score',
                    'hanchan_count', 'good_hanchans', 'won_hanchans')
    list_display_links = ('user',)
    actions = [recalculate]


class HanchanAdmin(admin.ModelAdmin):
    """ To administrate the stored Hanchans. """
    actions = [recalculate, confirm, unconfirm]
    date_hierarchy = 'start'
    list_filter = ('season', 'event', 'confirmed')
    search_fields = ('player_names',)
    list_display = ('event', 'start', 'player_names', 'comment', 'confirmed')
    readonly_fields = ('season', 'player1_comment', 'player2_comment',
                       'player3_comment', 'player4_comment')
    fieldsets = (
        ('Hanchan', {
            'fields': (
                ('event', 'season'),
                'start',
                ('player1', 'player1_input_score', 'player1_comment'),
                ('player2', 'player2_input_score', 'player2_comment'),
                ('player3', 'player3_input_score', 'player3_comment'),
                ('player4', 'player4_input_score', 'player4_comment'),
                'comment', 'confirmed'
            ),
            'classes': ('grp-collapse grp-open',),
        }),
    )


class KyuDanAdmin(admin.ModelAdmin):
    """ Lists the Kyu/dan Rankings and allows to recalculate them. """
    actions = [recalculate]
    list_display = ('user', 'kyu', 'kyu_points', 'dan', 'dan_points',
                    'hanchan_count')
    readonly_fields = ('user', 'kyu', 'kyu_points', 'dan', 'dan_points')
    fieldsets = (
        ('Aktueller Dan/Kyū', {
            'fields': ('user', ('kyu', 'kyu_points'), ('dan', 'dan_points')),
            'classes': ('grp-collapse grp-open',),
        }),
        ('Frühere Aufzeichnungen', {
            'fields': ('legacy_date', 'legacy_hanchan_count',
                       ('legacy_kyu_points', 'legacy_dan_points')),
            'classes': ('grp-collapse grp-closed',),
        }),
    )


class SeasonRankingAdmin(admin.ModelAdmin):
    """ Lists the Season Rankings and allows to recalculate them. """
    actions = [recalculate]
    list_display = ('placement', 'season', 'user', 'avg_placement',
                    'avg_score', 'hanchan_count', 'good_hanchans',
                    'won_hanchans')
    list_display_links = ('user',)
    list_filter = ('season',)


class LadderSeasonAdmin(admin.ModelAdmin):
    """ Seasons will be generated automaticly, but you can examine them here."""
    actions = [recalculate]
    list_display = ('name', 'start', 'end')


admin.site.register(models.Hanchan, HanchanAdmin)
admin.site.register(models.KyuDanRanking, KyuDanAdmin)
admin.site.register(models.SeasonRanking, SeasonRankingAdmin)
admin.site.register(models.EventRanking, EventRankingAdmin)
