from django import template
from django.utils.safestring import mark_safe
from django.utils.html import format_html
from ..models.images import ImageLibrary

register = template.Library()


@register.simple_tag
def image_library_img(slug_or_id, css_class="", alt_text="", width=None, height=None):
    """
    Render an image from the image library by slug or ID.

    Usage:
    {% image_library_img "my-image-slug" css_class="img-fluid" %}
    {% image_library_img image_id css_class="logo" width="100" height="100" %}
    """
    try:
        # Try to get by slug first, then by ID
        if isinstance(slug_or_id, str):
            image = ImageLibrary.objects.get(slug=slug_or_id)
        else:
            image = ImageLibrary.objects.get(pk=slug_or_id)

        # Use provided alt_text or fall back to image's alt_text
        final_alt_text = alt_text or image.alt_text

        # Build HTML attributes
        attrs = {
            "src": image.image.url,
            "alt": final_alt_text,
        }

        if css_class:
            attrs["class"] = css_class

        if width:
            attrs["width"] = width

        if height:
            attrs["height"] = height

        # Build the HTML
        attr_string = " ".join(f'{k}="{v}"' for k, v in attrs.items())
        return format_html("<img {}/>", attr_string)

    except ImageLibrary.DoesNotExist:
        # Return empty string or placeholder if image not found
        return format_html(
            '<img src="/static/images/placeholder.png" alt="Image not found" class="{}"/>',
            css_class,
        )


@register.simple_tag
def image_library_url(slug_or_id):
    """
    Get the URL of an image from the image library.

    Usage:
    {% image_library_url "my-image-slug" %}
    {% image_library_url image_id %}
    """
    try:
        if isinstance(slug_or_id, str):
            image = ImageLibrary.objects.get(slug=slug_or_id)
        else:
            image = ImageLibrary.objects.get(pk=slug_or_id)

        return image.image.url

    except ImageLibrary.DoesNotExist:
        return "/static/images/placeholder.png"


@register.simple_tag
def image_library_info(slug_or_id):
    """
    Get information about an image from the image library.

    Usage:
    {% image_library_info "my-image-slug" as img_info %}
    {{ img_info.name }} - {{ img_info.width }}x{{ img_info.height }}
    """
    try:
        if isinstance(slug_or_id, str):
            image = ImageLibrary.objects.get(slug=slug_or_id)
        else:
            image = ImageLibrary.objects.get(pk=slug_or_id)

        return {
            "name": image.name,
            "alt_text": image.alt_text,
            "width": image.width,
            "height": image.height,
            "file_size": image.get_file_size_display(),
            "category": image.get_category_display(),
            "tags": image.get_tags_list(),
            "url": image.image.url,
        }

    except ImageLibrary.DoesNotExist:
        return {
            "name": "Image not found",
            "alt_text": "Image not found",
            "width": None,
            "height": None,
            "file_size": "Unknown",
            "category": "Unknown",
            "tags": [],
            "url": "/static/images/placeholder.png",
        }
