from django.contrib import admin
from scrobbles.mixins import Genre
from scrobbles.models import (
    AudioScrobblerTSVImport,
    BGStatsImport,
    EBirdCSVImport,
    FavoriteMedia,
    KoReaderImport,
    LastFmImport,
    RetroarchImport,
    ScaleCSVImport,
    Scrobble,
    ShareViewLog,
    TrailGPXImport,
    UDiscCSVImport,
)


class ScrobbleInline(admin.TabularInline):
    model = Scrobble
    extra = 0
    per_page = 15
    ordering = ("-timestamp",)
    show_change_link = True
    fields = ("timestamp", "media_type", "source", "in_progress")
    readonly_fields = fields


class ImportBaseAdmin(admin.ModelAdmin):
    date_hierarchy = "created"
    list_display = (
        "uuid",
        "process_count",
        "processed_finished",
        "processing_started",
        "error_log",
    )
    ordering = ("-created",)


@admin.register(AudioScrobblerTSVImport)
class AudioScrobblerTSVImportAdmin(ImportBaseAdmin): ...


@admin.register(LastFmImport)
class LastFmImportAdmin(ImportBaseAdmin): ...


@admin.register(KoReaderImport)
class KoReaderImportAdmin(ImportBaseAdmin): ...


@admin.register(RetroarchImport)
class RetroarchImportAdmin(ImportBaseAdmin): ...


class RetroarchImportAdmin(ImportBaseAdmin): ...


@admin.register(BGStatsImport)
class BGStatsImportAdmin(ImportBaseAdmin): ...


@admin.register(EBirdCSVImport)
class EBirdCSVImportAdmin(ImportBaseAdmin): ...


@admin.register(ScaleCSVImport)
class ScaleCSVImportAdmin(ImportBaseAdmin): ...


@admin.register(TrailGPXImport)
class TrailGPXImportAdmin(ImportBaseAdmin): ...


@admin.register(UDiscCSVImport)
class UDiscCSVImportAdmin(ImportBaseAdmin): ...


@admin.register(Genre)
class GenreAdmin(admin.ModelAdmin):
    list_display = (
        "name",
        "source",
    )


@admin.register(Scrobble)
class ScrobbleAdmin(admin.ModelAdmin):
    date_hierarchy = "timestamp"
    list_display = (
        "timestamp",
        "media_name",
        "media_type",
        "playback_percent",
        "source",
        "in_progress",
        "is_paused",
        "played_to_completion",
        "visibility",
        "user",
    )
    raw_id_fields = (
        "user",
        "video",
        "channel",
        "podcast_episode",
        "track",
        "sport_event",
        "book",
        "video_game",
        "board_game",
        "geo_location",
        "puzzle",
        "paper",
        "food",
        "task",
        "mood",
        "brick_set",
        "trail",
        "beer",
        "web_page",
        "life_event",
        "birding_location",
        "disc_golf_course",
        "workout_routine",
        "long_play_last_scrobble",
    )
    list_filter = (
        "is_paused",
        "in_progress",
        "media_type",
        "visibility",
        "long_play_complete",
        "source",
        "timezone",
        "user",
    )
    ordering = ("-timestamp",)
    readonly_fields = ("share_token_version", "share_view_count")

    def media_name(self, obj):
        return obj.media_obj

    def playback_percent(self, obj):
        return obj.percent_played

    def get_queryset(self, request):
        qs = super().get_queryset(request).exclude(timestamp__year=None)
        return qs


@admin.register(ShareViewLog)
class ShareViewLogAdmin(admin.ModelAdmin):
    list_display = ("scrobble", "ip_address", "created")
    list_filter = ("created",)
    date_hierarchy = "created"
    raw_id_fields = ("scrobble",)


@admin.register(FavoriteMedia)
class FavoriteMediaAdmin(admin.ModelAdmin):
    list_display = ("user", "media_type", "sent_to_mopidy", "created")
    list_filter = ("media_type", "sent_to_mopidy", "user")
    date_hierarchy = "created"
    raw_id_fields = (
        "user",
        "video",
        "channel",
        "track",
        "podcast_episode",
        "sport_event",
        "book",
        "paper",
        "video_game",
        "board_game",
        "geo_location",
        "puzzle",
        "food",
        "task",
        "mood",
        "brick_set",
        "trail",
        "beer",
        "wine",
        "coffee",
        "web_page",
        "life_event",
        "birding_location",
        "disc_golf_course",
        "workout_routine",
    )
