import uuid

from django.utils.translation import gettext as _

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

from ocyan.plugin.coyote.custom_blocks import (
    IconChoiceBlock,
    LinkChooserBlock,
    VisualChoiceBlock,
)
from ocyan.plugin.coyote.icons import ICONS

from .partials import CoyoteButtonBlock, CoyoteStructBlock


class BootstrapTabBlock(blocks.StructBlock):
    header = blocks.CharBlock(label=_("Title"))
    body = blocks.RichTextBlock(label=_("Body"))
    button = CoyoteButtonBlock(required=False, label=_("Button"))


class CoyoteTabsBlock(CoyoteStructBlock):
    header = blocks.CharBlock(label=_("Title"), required=False)
    image = ImageChooserBlock(label=_("Image"))
    tabs = blocks.ListBlock(
        BootstrapTabBlock(), label=_("Tabbed content"), required=False, collapsed=True
    )

    def get_uuid(self):
        return uuid.uuid4()

    class Meta:
        icon = "table"
        template = "coyote/blocks/content/tabsblock.html"


class CoyoteIconBlock(blocks.StructBlock):
    icon = IconChoiceBlock(choices=ICONS, required=False, label=_("Icon"))
    image = ImageChooserBlock(required=False, label=_("Image"))
    header = blocks.CharBlock(required=False, label=_("Title"))
    body = blocks.TextBlock(label=_("Body"), required=False, max_length=150)
    link = LinkChooserBlock(label=_("Link"), required=False)

    class Meta:
        icon = "tick-inverse"


class CoyoteIconsWithText(CoyoteStructBlock):
    layout = VisualChoiceBlock(
        choices=[
            ("layout1", "coyote/layouts/icon_layout_1.png"),
            ("layout2", "coyote/layouts/icon_layout_2.png"),
            ("layout3", "coyote/layouts/icon_layout_3.png"),
        ],
        default="layout1",
        label=_("Layout"),
    )
    body = blocks.RichTextBlock(label=_("Body"), required=False)
    icons = blocks.ListBlock(
        CoyoteIconBlock(), label=_("Icons"), required=False, collapsed=True
    )
    button = CoyoteButtonBlock(required=False, label=_("Button"))

    class Meta:
        icon = "info-circle"
        template = "coyote/blocks/content/icons_with_text.html"


class CoyoteLogos(CoyoteStructBlock):
    header = blocks.CharBlock(label=_("Title"), required=False)
    logos = blocks.ListBlock(
        ImageChooserBlock(), required=False, collapsed=True, label=_("Logos")
    )

    class Meta:
        icon = "image"
        template = "coyote/blocks/content/logo_slider.html"
        label = "Logo slider"


class CoyoteCenterText(CoyoteStructBlock):
    body = blocks.RichTextBlock(required=False, label=_("Body"))
    image = ImageChooserBlock(required=False, label=_("Image"))
    buttons = blocks.ListBlock(
        CoyoteButtonBlock(required=False), required=False, label=_("Buttons")
    )

    class Meta:
        template = "coyote/blocks/content/centertext.html"


class CounterAttribute(blocks.StructBlock):
    number = blocks.CharBlock(required=False, label=_("Number"), max_length=20)
    label = blocks.CharBlock(required=False, label=_("Label"), max_length=50)

    class Meta:
        icon = "repeat"


class CoyoteImageWithText(CoyoteStructBlock):
    layout = VisualChoiceBlock(
        choices=[
            ("layout1", "coyote/layouts/layout_image_text_2.png"),
            ("layout2", "coyote/layouts/layout_image_text_1.png"),
        ],
        default="layout1",
        label=_("Layout"),
    )
    image = ImageChooserBlock(label=_("Image"))
    image_fill = blocks.BooleanBlock(
        required=False, label=_("Scale image to content size"), default=True
    )
    body = blocks.RichTextBlock(required=False, label=_("Body"))
    buttons = blocks.ListBlock(
        CoyoteButtonBlock(), label=_("Buttons"), required=False, collapsed=True
    )
    counter = blocks.ListBlock(
        CounterAttribute(), label=_("Counter"), required=False, collapsed=True
    )

    class Meta:
        template = "coyote/blocks/content/image_with_text.html"


class CoyoteAccordionBlock(blocks.StructBlock):
    header = blocks.CharBlock(required=False, label=_("Title"))
    body = blocks.RichTextBlock(required=False, label=_("Body"))
    button = CoyoteButtonBlock(required=False, label=_("Button"))

    class Meta:
        icon = "arrow-down"


class CoyoteAccordionAllBlock(CoyoteStructBlock):
    header = blocks.CharBlock(required=False, label=_("Title"))
    body = blocks.RichTextBlock(required=False, label=_("Body"))
    accordion = blocks.ListBlock(
        CoyoteAccordionBlock(), required=True, collapsed=True, label=_("Accordion")
    )

    class Meta:
        template = "coyote/blocks/content/accordion.html"


class CoyotePersonBlock(blocks.StructBlock):
    image = ImageChooserBlock(required=True, label=_("Image"))
    name = blocks.CharBlock(required=True, label=_("Full name"))
    function = blocks.CharBlock(required=False, label=_("Function"))
    instagram = blocks.CharBlock(required=False, label=_("Instagram"))
    facebook = blocks.CharBlock(required=False, label=_("Facebook"))
    linkedin = blocks.CharBlock(required=False, label=_("LinkedIn"))
    twitter = blocks.CharBlock(required=False, label=_("Twitter"))

    class Meta:
        icon = "user"


class CoyotePersonListBlock(CoyoteStructBlock):
    body = blocks.RichTextBlock(required=False, label=_("Body"))
    persons = blocks.ListBlock(CoyotePersonBlock(), required=True, collapsed=True)

    class Meta:
        template = "coyote/blocks/content/personsblock.html"


class CoyoteQuoteBlock(blocks.StructBlock):
    image = ImageChooserBlock(required=False, label=_("Image"))
    name = blocks.CharBlock(required=False, label=_("Full name"))
    function = blocks.CharBlock(required=False, label=_("Function"))
    quote = blocks.TextBlock(required=False, label=_("Quote"))


class CoyoteQuotesBlock(CoyoteStructBlock):
    body = blocks.RichTextBlock(required=False, label=_("Introduction"))
    quotes = blocks.ListBlock(
        CoyoteQuoteBlock(), required=True, collapsed=True, label=_("Quotes")
    )

    class Meta:
        template = "coyote/blocks/content/quotesblock.html"
        icon = "openquote"


class CoyoteTextListBlock(CoyoteStructBlock):
    layout = VisualChoiceBlock(
        choices=[
            ("layout1", "coyote/layouts/textlist_layout_1.png"),
            ("layout2", "coyote/layouts/textlist_layout_2.png"),
            ("layout3", "coyote/layouts/textlist_layout_3.png"),
        ],
        default="layout1",
        label=_("Layout"),
    )
    body = blocks.RichTextBlock(required=False, label=_("Body"))
    buttons = blocks.ListBlock(
        CoyoteButtonBlock(required=False),
        required=False,
        collapsed=True,
        label=_("Buttons"),
    )
    body_list = blocks.ListBlock(
        blocks.RichTextBlock(), required=True, collapsed=True, label=_("Paragraphs")
    )

    class Meta:
        template = "coyote/blocks/content/textlistblock.html"
