from django.utils.translation import gettext as _

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

from ocyan.main.loading import get_baseclass
from ocyan.plugin.coyote.blocks.partials import CoyoteButtonBlock, CoyoteStructBlock
from ocyan.plugin.coyote.custom_blocks import LinkChooserBlock
from ocyan.plugin.wagtail_oscar_integration.blocks import (
    CategoryChooserBlock,
    ProductChooserBlock,
)

BaseHomePageContentStreamBlock = get_baseclass(
    "coyote.stream_blocks",
    "HomePageContentStreamBlock",
    "ocyan.plugin.carbasa.webshop",
)


class CoyoteProductsBlock(CoyoteStructBlock):
    tagline = blocks.CharBlock(required=False, label=_("Tagline"))
    header = blocks.CharBlock(required=False, label=_("Header"))
    banner_link = blocks.URLBlock(required=False)
    buttons = blocks.ListBlock(
        CoyoteButtonBlock(),
        label=_("Buttons"),
        required=False,
        collapsed=True,
        default=[],
    )
    products = blocks.ListBlock(
        ProductChooserBlock(), required=False, label="Products", default=[]
    )

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


class CarbasaCategorychooser(blocks.StructBlock):
    category = CategoryChooserBlock(required=False)
    image = ImageChooserBlock(
        required=False, help_text=_("default is set to category image")
    )
    label = blocks.CharBlock(
        required=False, help_text=_("Default is set to category label")
    )
    page_link = LinkChooserBlock(
        label=_("Link"),
        required=False,
        help_text=_("This will overwrite the category link"),
    )


class CoyoteCategoryBlock(CoyoteStructBlock):
    tagline = blocks.CharBlock(required=False, label=_("Tagline"))
    header = blocks.CharBlock(required=False, label=_("Header"))
    categories = blocks.ListBlock(
        CarbasaCategorychooser(), required=False, label=_("Categories")
    )

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


class HomePageContentStreamBlock(BaseHomePageContentStreamBlock):
    Products = CoyoteProductsBlock(icon="tag")
    Category = CoyoteCategoryBlock(icon="tag")
