# Copyright (C) 2020 by Animath
# SPDX-License-Identifier: GPL-3.0-or-later

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

from .models import Draw, Pool, Round, TeamDraw


@admin.register(Draw)
class DrawAdmin(admin.ModelAdmin):
    list_display = ('tournament', 'teams', 'current_round', 'get_state',)
    list_filter = ('tournament', 'current_round',)
    search_fields = ('tournament__name', 'tournament__participation__team__trigram',)

    @admin.display(description=_("teams"))
    def teams(self, record: Draw):
        return ', '.join(p.team.trigram for p in record.tournament.participations.filter(valid=True).all())


@admin.register(Round)
class RoundAdmin(admin.ModelAdmin):
    list_display = ('draw', 'number', 'teams',)
    list_filter = ('draw__tournament', 'number',)
    search_fields = ('draw__tournament__name', 'pool__teamdraw__participation__team__trigram')
    ordering = ('draw__tournament__name', 'number')

    @admin.display(description=_("teams"))
    def teams(self, record: Round):
        return ', '.join(td.participation.team.trigram for td in record.team_draws)


@admin.register(Pool)
class PoolAdmin(admin.ModelAdmin):
    list_display = ('tournament', 'round', 'letter', 'teams')
    list_filter = ('round__draw__tournament', 'round__number', 'letter')
    ordering = ('round__draw__tournament__name', 'round', 'letter')
    search_fields = ('round__draw__tournament__name', 'teamdraw__participation__team__trigram',)

    @admin.display(ordering='round__draw__tournament__name', description=_("tournament"))
    def tournament(self, record):
        return record.round.draw.tournament

    @admin.display(description=_("teams"))
    def teams(self, record: Round):
        return ', '.join(td.participation.team.trigram for td in record.team_draws)


@admin.register(TeamDraw)
class TeamDrawAdmin(admin.ModelAdmin):
    list_display = ('participation', 'tournament', 'view_round', 'pool', 'accepted', 'rejected',
                    'passage_index', 'choose_index', 'passage_dice', 'choice_dice',)
    list_filter = ('round__draw__tournament', 'round__number', 'pool__letter',)
    search_fields = ('round__draw__tournament__name', 'participation__team__trigram',)

    @admin.display(ordering='round__draw__tournament__name', description=_("tournament"))
    def tournament(self, record):
        return record.round.draw.tournament

    @admin.display(ordering='round__number', description=_('round'))
    def view_round(self, record):
        return record.round.get_number_display()
