from django.utils.translation import gettext as _

from wagtail import blocks
from wagtail.admin.forms.choosers import URLOrAbsolutePathField
from wagtail.images.blocks import ImageChooserBlock


class URLOrAbsolutePathBlock(blocks.FieldBlock):
    def __init__(
        self, required=True, help_text=None, max_length=None, min_length=None, **kwargs
    ):
        self.field = URLOrAbsolutePathField(
            required=required,
            help_text=help_text,
            max_length=max_length,
            min_length=min_length,
        )
        super().__init__(**kwargs)

    class Meta:
        icon = "site"


class CallToActionButtonBlock(blocks.StructBlock):
    link = URLOrAbsolutePathBlock()
    image = ImageChooserBlock()
    title = blocks.CharBlock(blank=True, required=False, default="")


class CallToActionBlock(blocks.StructBlock):
    buttons = blocks.ListBlock(CallToActionButtonBlock())
    paragraph = blocks.RichTextBlock()
    alignment = blocks.ChoiceBlock(
        label=_("Align"),
        required=True,
        default="align-block",
        choices=[
            ("align-left", _("Left")),
            ("align-block", _("Full width")),
            ("align-right", _("Right")),
        ],
        help_text=_("On which side should the image show?"),
    )

    class Meta:
        template = "wagtail_content_page/blocks/call-to-action.html"
        icon = "form"
