from django.utils.html import format_html

from apps.core.video import VIDEO_SOURCE_UPLOAD, VIDEO_SOURCE_YOUTUBE


def video_admin_preview(obj):
    if not obj.has_video:
        return "No video configured yet."
    if obj.video_source == VIDEO_SOURCE_UPLOAD and obj.video_file:
        if obj.video_poster:
            return format_html(
                '<video controls playsinline preload="metadata" style="max-width:100%;" poster="{}">'
                '<source src="{}"></video>',
                obj.video_poster.url,
                obj.video_file.url,
            )
        return format_html(
            '<video controls playsinline preload="metadata" style="max-width:100%;">'
            '<source src="{}"></video>',
            obj.video_file.url,
        )
    if obj.video_source == VIDEO_SOURCE_YOUTUBE and obj.youtube_embed_url:
        return format_html(
            '<iframe src="{}" title="Preview" width="480" height="270" '
            'style="max-width:100%;border:0;" allowfullscreen loading="lazy"></iframe>',
            obj.youtube_embed_url,
        )
    return "No video configured yet."

video_admin_preview.short_description = "Preview"
