import logging

from django.utils.translation import gettext_lazy as _
from rr.blocks import StylingBlock
from rr.search import get_searchable_content_for_fields

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

logger = logging.getLogger()

HEIGHT_CHOICES = [
    ("px-small", _("Small (Pixel height)")),
    ("px-medium", _("Medium (Pixel height)")),
    ("px-large", _("Large (Pixel height)")),
    ("vh-small", _("Small (Screen height)")),
    ("vh-medium", _("Medium (Screen height)")),
    ("vh-large", _("Large (Screen height)")),
    ("vh-full", _("Full (Screen height)")),
]


class EmptyBlock(blocks.StaticBlock):
    class Meta:
        admin_text = _("The empty block renders nothing on the frontend")
        group = "HTML"
        icon = "wagtail-inverse"
        label = _("Empty")
        template = "roadrunner_bs5/blocks/emptyblock.html"


class ParallaxBlock(blocks.StructBlock):
    image = ImageChooserBlock(label=_("Background"), required=False)
    body = blocks.RichTextBlock(
        label=_("Content"),
        help_text=_("This will be placed on top of the background"),
        required=False,
    )
    height = blocks.ChoiceBlock(
        label=_("Height"),
        help_text=_("The height of the block"),
        choices=HEIGHT_CHOICES,
        default="h-medium",
        required=False,
    )
    horizontal_center = blocks.BooleanBlock(
        label=_("Center horizontally"),
        help_text=_("This will center the text horizontally"),
        required=False,
    )
    overlay = blocks.BooleanBlock(
        label=_("Darken background"),
        help_text=_(
            "This will darken the background behind the text, which can help make text more readable on some images."
        ),
        default=True,
        required=False,
    )
    lazy = blocks.BooleanBlock(
        required=False,
        default=False,
        label=_("Below the fold"),
        help_text=_(
            "If the user needs to scroll before the content becomes visible, it is said to be 'below the fold'. Correctly identifying content as 'Below the fold' will speed up page loading time."
        ),
    )
    styling = StylingBlock()

    def get_searchable_content(self, value):
        return get_searchable_content_for_fields(
            value, self.child_blocks, ["image", "body"]
        )

    class Meta:
        template = "roadrunner_bs5/blocks/parallaxblock.html"
        form_template = "formtemplate/parallax.html"
        preview_template = "preview/bootstrap/parallax.html"
        icon = "image"
        label = _("parallax")
        group = "Bootstrap"


class GoogleMapsBlock(blocks.StructBlock):
    name = blocks.CharBlock(label=_("Name"), help_text=_("Company name"))
    address = blocks.CharBlock(
        label=_("Address"),
        required=False,
        help_text=_("The address to display on the map"),
    )
    aspect_ratio = blocks.ChoiceBlock(
        label=_("Aspect ratio"),
        help_text=_("The shape of the map"),
        choices=[
            ("21by9", "Rectangular"),
            ("1by1", "Square"),
        ],
        default="21by9",
    )
    view = blocks.ChoiceBlock(
        label=_("Display"),
        help_text=_("The type of map detail to display"),
        choices=[
            ("m", _("Map")),
            ("k", _("Satelite")),
            ("h", _("Hybrid")),
            ("p", _("Terrain")),
        ],
        default="m",
    )
    zoom = blocks.ChoiceBlock(
        label=_("Zoom"),
        default="17",
        help_text=_("The map's zoom level"),
        choices=[
            ("0", _("max. zoom out")),
            ("1", _("4.000 km")),
            ("2", _("2.000 km (world)")),
            ("3", _("1.000 km")),
            ("4", _("400 km (continent)")),
            ("5", _("200 km")),
            ("6", _("100 km (country)")),
            ("7", _("50 km")),
            ("8", _("30 km")),
            ("9", _("15 km (area)")),
            ("10", _("8 km")),
            ("11", _("4 km")),
            ("12", _("2 km (city)")),
            ("13", _("1 km")),
            ("14", _("400 m (district)")),
            ("15", _("200 m")),
            ("16", _("100 m")),
            ("17", _("50 m (street)")),
            ("18", _("20 m")),
            ("19", _("10 m")),
            ("20", _("5 m  (house)")),
            ("21", _("2,5 m")),
        ],
    )

    def get_searchable_content(self, value):
        return get_searchable_content_for_fields(
            value, self.child_blocks, ["name", "address"]
        )

    class Meta:
        template = "roadrunner_bs5/blocks/googlemapsblock.html"
        preview_template = "preview/bootstrap/googlemaps.html"
        form_template = "formtemplate/googlemaps.html"
        icon = "ggmaps"
        label = _("maps")
        group = "Extra"


class SlideShowChildBlock(blocks.StructBlock):
    image = ImageChooserBlock(required=False)
    body = blocks.RichTextBlock(required=False)
    position = blocks.ChoiceBlock(
        choices=[
            ("top-left", _("Top left")),
            ("top-right", _("Top right")),
            ("bottom-left", _("Bottom left")),
            ("bottom-right", _("Bottom right")),
            ("middle-left", _("Middle left")),
            ("middle", _("Middle")),
            ("middle-right", _("Middle right")),
        ],
        required=False,
        default="middle-left",
    )

    def get_searchable_content(self, value):
        return get_searchable_content_for_fields(
            value, self.child_blocks, ["image", "body"]
        )

    class Meta:
        label = _("Slide")
        icon = "help"
        group = "Extra"
        preview = ["image"]


class SlideShowBlock(blocks.StructBlock):
    slides = blocks.ListBlock(SlideShowChildBlock())

    # options
    items = blocks.IntegerBlock(
        help_text=_("The number of items you want to see on the screen."), default=1
    )
    slideby = blocks.CharBlock(
        help_text=_("Number of slides going on one 'click'."),
        required=False,
        default="1",
    )
    controls = blocks.BooleanBlock(
        help_text=_("Show next/prev buttons."), required=False, default=True
    )
    autoplaytimeout = blocks.CharBlock(
        label=_("Autoplay timeout (in milliseconds)"),
        help_text=_(
            "Autoplay interval timeout. The time between slides. The amount is in milliseconds"
        ),
        required=False,
    )
    loop = blocks.BooleanBlock(
        help_text=_(
            "Infinite loop. Duplicate last and first items to get loop illusion."
        ),
        required=False,
        default=True,
    )
    nav = blocks.BooleanBlock(help_text=_("Show navigation dots."), required=False)
    autoheight = blocks.BooleanBlock(
        help_text=_("Automatically adjust height based on image height."),
        required=False,
        default=True,
    )
    autoplay = blocks.BooleanBlock(
        help_text=_("Automatically play slides"), required=False
    )
    fixed_height = blocks.ChoiceBlock(
        label=_("Fixed height of all images"),
        help_text=_(
            "This will make it possible to set a height for all the images in the slider. You can put this in pixels or viewheight."
        ),
        choices=HEIGHT_CHOICES,
        required=False,
    )
    lazy = blocks.BooleanBlock(
        required=False,
        default=False,
        label=_("Below the fold"),
        help_text=_(
            "If the user needs to scroll before the content becomes visible, it is said to be 'below the fold'. Correctly identifying content as 'Below the fold' will speed up page loading time."
        ),
    )

    styling = StylingBlock()

    def get_searchable_content(self, value):
        return get_searchable_content_for_fields(value, self.child_blocks, ["slides"])

    class Meta:
        label = _("Slideshow")
        icon = "slider"
        group = "Extra"
        template = "roadrunner_bs5/blocks/slideshow.html"
        form_template = "formtemplate/slideshow.html"
        preview = ["slides"]


class SectionFAQItemBlock(blocks.StructBlock):
    question = blocks.CharBlock(label="Vraag")
    answer = blocks.RichTextBlock(label="Antwoord")

    class Meta:
        label = "FAQ Item"


class SectionItemBlock(blocks.StructBlock):
    label = blocks.CharBlock(label="Label", required=False)
    items = blocks.ListBlock(SectionFAQItemBlock(), label="Items")


class SectionedFAQBlock(blocks.StructBlock):
    header = blocks.RichTextBlock(label="Header heading", required=False)
    text = blocks.TextBlock(label="Header tekst", required=False)

    items = blocks.ListBlock(SectionItemBlock(), label="Items")

    footer_heading = blocks.CharBlock(label="Footer heading", required=False)
    footer_text = blocks.TextBlock(label="Footer tekst", required=False)

    class Meta:
        label = "FAQ"
        template = "roadrunner_bs5/blocks/faqblock.html"
        icon = "openquote"
        group = "Extra"
