from django.template import Context, Template

from wagtail.images.formats import Format, register_image_format

PICTUREIMAGEFORMAT_TEMPLATE = """{% load ocyan_thumbnail %}{% spaceless %}
{% lazy_responsive_image image alt=alt_text %}
{% endspaceless %}"""


class PictureImageFormat(Format):
    def image_to_html(self, image, alt_text, extra_attributes=None):
        tpl = Template(PICTUREIMAGEFORMAT_TEMPLATE)
        ctx = Context({"image": image.file, "alt": alt_text})
        return tpl.render(ctx)


register_image_format(
    PictureImageFormat("picture", "Lazy responsive picture", "lazy", "original")
)
