from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

from wagtail import blocks
from wagtail.images.blocks import ImageChooserBlock

from .localization import resolve_page_for_language


class LinkBlock(blocks.StructBlock):
    link = blocks.PageChooserBlock(required=False, label="Pagina")
    external_link = blocks.CharBlock(required=False, label="Externe link")
    label = blocks.CharBlock(required=False, label="Link label")

    def get_context(self, value, parent_context=None):
        context = super().get_context(value, parent_context=parent_context)
        if context["value"].get("link") is not None:
            context["value"]["link"] = resolve_page_for_language(context["value"]["link"])
        return context


class WrappedCharBlock(blocks.CharBlock):
    """Wraps the output of a CharBlock in a tag of your choice."""

    def render(self, value, context=None):
        html = super().render(value, context)
        tag = self.meta.tag
        return mark_safe(f"<{tag}>{html}</{tag}>")


class FooterBlock(blocks.StructBlock):
    link = blocks.CharBlock(label="Link naar pagina")
    image = ImageChooserBlock(label="Afbeelding")


class SlideBlock(FooterBlock):
    link_text = blocks.CharBlock(label="Tekst op knop", required=False)
    text = blocks.TextBlock(label="Tekst", required=False)


class CategoryLinkBlock(blocks.StructBlock):
    link = blocks.CharBlock()
    image = ImageChooserBlock()
    link_text = blocks.CharBlock(required=False)


class ImageLinkBlock(blocks.StructBlock):
    image = ImageChooserBlock()
    page = blocks.PageChooserBlock(
        required=False, help_text=_("Optional, you can enter a external link too.")
    )
    external_link = blocks.CharBlock(
        required=False, help_text=_("Optional, you can choose a page too.")
    )

    def get_context(self, value, parent_context=None):
        context = super().get_context(value, parent_context=parent_context)
        if context["value"].get("page") is not None:
            context["value"]["page"] = resolve_page_for_language(context["value"]["page"])
        return context

    class Meta:
        template = "ocyan_plugin_wagtail/blocks/image_link_block.html"


# Footer blocks


class HeadingBlock(blocks.StructBlock):
    heading = blocks.CharBlock(
        label=_("Heading"), help_text=_("Heading of the content block."), required=False
    )

    class Meta:
        template = "ocyan_plugin_wagtail/blocks/footer_block.html"


class HeadedRichTextBlock(HeadingBlock):
    content = blocks.RichTextBlock()


class HeadedPagelistBlock(HeadingBlock):
    page = blocks.PageChooserBlock(
        label=_("Page"), help_text=_("List pages below this page")
    )

    def get_context(self, value, parent_context=None):
        context = super().get_context(value, parent_context=parent_context)
        target_page = context["value"]["page"]
        if target_page is not None:
            target_page = resolve_page_for_language(target_page)
            context["value"]["page"] = target_page
            context["children"] = target_page.get_children().live()
        return context

    class Meta:
        template = "ocyan_plugin_wagtail/blocks/pagelist_block.html"


class AboutUsBlock(HeadingBlock):
    content = blocks.RichTextBlock()

    class Meta:
        template = "ocyan_plugin_wagtail/blocks/aboutus_block.html"
