from django.utils.translation import gettext as _

from wagtail import blocks

from ocyan.main.loading import get_ocyan_class

from ocyan.plugin.coyote.custom_blocks import ColorChoiceBlock, LinkChooserBlock

COYOTE_COLOR_CHOICES = get_ocyan_class("coyote.variables", "COYOTE_COLOR_CHOICES")


class CoyoteAnchorBlock(blocks.StaticBlock):
    "The anchor block adds a unique anchor id to each block it is added to"

    def value_from_datadict(self, data, files, prefix):
        # the prefix is a unique value
        return prefix

    class Meta:
        admin_text = ""
        label = " "


class CoyoteStructBlock(blocks.StructBlock):
    coyote_anchor = CoyoteAnchorBlock()
    coyote_bg = ColorChoiceBlock()

    class Meta:
        icon = "site"


class CoyoteColorChoiceBlock(blocks.ChoiceBlock):
    choices = COYOTE_COLOR_CHOICES


class CoyoteButtonBlock(blocks.StructBlock):
    label = blocks.CharBlock(required=False, label=_("Label"))
    link = LinkChooserBlock(required=False, label=_("Page URL"))
    new_tab = blocks.BooleanBlock(
        required=False, default=False, label=_("Open in new tab")
    )
    button_style = CoyoteColorChoiceBlock(label=_("Button style"), required=False)

    class Meta:
        preview_template = "preview/bootstrap/button.html"
        label = _("Button")
        group = "Bootstrap"
        icon = "error"


class CoyoteInlineBlock(blocks.StructBlock):
    class Meta:
        # form_template = "coyote/formtemplates/inline.html"
        form_classname = "coyote_inline"
