from grapple.helpers import register_streamfield_block
from grapple.models import (
    GraphQLImage,
    GraphQLPage,
    GraphQLRichText,
    GraphQLStreamfield,
    GraphQLString,
)
from wagtail import blocks
from wagtail.embeds.blocks import EmbedBlock
from wagtail.images.blocks import ImageChooserBlock

BACKGROUND_COLOR_CHOICES = (
    ("betongGray", "Betonggrå"),
    ("deepBrick", "Dyp tegl"),
    ("neufPink", "Griserosa"),
    ("goldenOrange", "Gyllen oransje"),
    ("goldenBeige", "Gyllen beige"),
    ("chateauBlue", "Slottsblå"),
)


@register_streamfield_block
class ImageWithTextBlock(blocks.StructBlock):
    image = ImageChooserBlock(
        label="Bilde",
    )
    image_format = blocks.ChoiceBlock(
        choices=[
            ("fullwidth", "Fullbredde"),
            ("bleed", "Utfallende"),
            ("original", "Uendret størrelse"),
        ],
        default="fullwidth",
        icon="cup",
        label="Bildeformat",
    )
    text = blocks.CharBlock(
        label="Bildetekst",
        required=False,
        max_length=512,
    )

    graphql_fields = [
        GraphQLImage("image", required=True),
        GraphQLString("image_format", required=True),
        GraphQLString("text"),
    ]

    class Meta:
        icon = "image"
        label = "Bilde"


@register_streamfield_block
class ImageSliderItemBlock(blocks.StructBlock):
    image = ImageChooserBlock(
        label="Bilde",
    )
    text = blocks.CharBlock(
        label="Tekst",
        required=False,
        max_length=512,
    )

    graphql_fields = [GraphQLImage("image", required=True), GraphQLString("text")]

    class Meta:
        icon = "image"
        # label = "Bilde"


@register_streamfield_block
class ImageSliderBlock(blocks.StructBlock):
    images = blocks.ListBlock(
        ImageSliderItemBlock(),
        min_num=1,
        label="Bilder",
    )

    graphql_fields = [GraphQLStreamfield("images", required=True)]

    class Meta:
        icon = "image"
        label = "Bildegalleri"


@register_streamfield_block
class HorizontalRuleBlock(blocks.StructBlock):
    COLOR_CHOICES = (
        ("deepBrick", "Dyp tegl"),
        ("neufPink", "Griserosa"),
        ("goldenOrange", "Gyllen oransje"),
        ("goldenBeige", "Gyllen beige"),
        ("chateauBlue", "Slottsblå"),
    )

    color = blocks.ChoiceBlock(
        label="Farge",
        required=False,
        choices=COLOR_CHOICES,
    )

    graphql_fields = [GraphQLString("color", required=False)]

    class Meta:
        icon = "minus"
        label = "Skillelinje"


@register_streamfield_block
class FeaturedBlock(blocks.StructBlock):
    IMAGE_POSITION_CHOICES = (
        ("left", "Venstre"),
        ("right", "Høyre"),
    )

    title = blocks.CharBlock(max_length=64, required=True, label="Tittel")
    text = blocks.RichTextBlock(features=["bold", "italic", "link"], required=True, label="Tekst")
    featured_page = blocks.PageChooserBlock(header="Fremhevet side", required=True)
    link_text = blocks.CharBlock(
        max_length=64,
        label="Lenketekst",
        default="Les mer",
        required=True,
        help_text='Lenketeksten som tar deg videre til siden. Tips: Ikke start med "Trykk her"',
    )
    background_color = blocks.ChoiceBlock(
        label="Bakgrunnsfarge",
        required=True,
        default="betongGray",
        choices=BACKGROUND_COLOR_CHOICES,
    )
    image_position = blocks.ChoiceBlock(
        required=True,
        choices=IMAGE_POSITION_CHOICES,
        label="Bildeplassering",
    )

    featured_image_override = ImageChooserBlock(
        header="Overstyr bilde",
        help_text=(
            "Bildet som er tilknyttet undersiden du vil fremheve, vil automatisk brukes. "
            "Om det mangler eller du vil overstyre hvilket bilde som et brukes, "
            "kan du velge et her."
        ),
        required=False,
    )

    graphql_fields = [
        GraphQLString("title", required=True),
        GraphQLString("text", required=True),
        GraphQLString("link_text", required=True),
        GraphQLString("image_position", required=True),
        GraphQLImage("featured_image_override"),
        GraphQLString("background_color", required=False),
        GraphQLPage("featured_page", required=True),
    ]

    class Meta:
        icon = "arrow-right-full"
        label = "Fremhevet underside"


class PageSectionNavigationBlock(blocks.StaticBlock):
    class Meta:
        icon = "order-down"
        label = "Seksjonsnavigasjon"
        admin_text = "Lager automatisk ankerlenker til alle seksjoner"


class NeufAddressSectionBlock(blocks.StaticBlock):
    class Meta:
        icon = "site"
        label = "Chateau Neuf: Adresse og kart"
        admin_text = "Viser adressen og Google-kart."


class OpeningHoursSectionBlock(blocks.StaticBlock):
    class Meta:
        icon = "time"
        label = "Chateau Neuf: Åpningstider"
        admin_text = "Viser gjeldende åpningstider."


@register_streamfield_block
class AccordionBlock(blocks.StructBlock):
    heading = blocks.CharBlock(max_length=64, required=True, label="Overskrift")
    body = blocks.StreamBlock(
        [
            ("paragraph", blocks.RichTextBlock(label="Rik tekst")),
            ("image", ImageWithTextBlock(label="Bilde")),
        ]
    )

    graphql_fields = [
        GraphQLString("heading", required=True),
        GraphQLStreamfield("body", required=True),
    ]

    class Meta:
        icon = "folder-open-1"
        label = "Trekkspill"


@register_streamfield_block
class FactBoxBlock(blocks.StructBlock):
    background_color = blocks.ChoiceBlock(
        label="Bakgrunnsfarge",
        required=False,
        choices=BACKGROUND_COLOR_CHOICES,
    )
    body = blocks.RichTextBlock(
        features=["bold", "italic", "link", "ol", "ul", "h2", "h3"],
        label="Innhold",
    )

    graphql_fields = [
        GraphQLRichText("body", required=True),
        GraphQLString("background_color", required=False),
    ]

    class Meta:
        icon = "info-circle"
        label = "Faktaboks"


@register_streamfield_block
class ScheduleItemBlock(blocks.StructBlock):
    time = blocks.CharBlock(
        label="Tidspunkt/dato",
        required=True,
        max_length=64,
        help_text='Fritekst, f.eks. "13:00", "20:00 – 23:00" eller "23.06"',
    )
    title = blocks.RichTextBlock(
        features=["bold", "italic", "link"],
        label="Tittel",
        required=True,
        help_text="Kort tittel på én linje. Du kan legge inn lenker.",
    )
    description = blocks.RichTextBlock(
        features=["bold", "italic", "link"],
        label="Beskrivelse",
        required=False,
        help_text="Valgfri tilleggsinformasjon.",
    )

    graphql_fields = [
        GraphQLString("time", required=True),
        GraphQLRichText("title", required=True),
        GraphQLRichText("description"),
    ]

    class Meta:
        icon = "time"
        label = "Punkt"


@register_streamfield_block
class ScheduleBlock(blocks.StructBlock):
    title = blocks.CharBlock(
        max_length=64,
        required=False,
        label="Tittel",
        help_text="Overskrift for timeplanen (valgfritt)",
    )
    items = blocks.ListBlock(
        ScheduleItemBlock(),
        min_num=1,
        label="Punkter",
    )

    graphql_fields = [
        GraphQLString("title"),
        GraphQLStreamfield("items", required=True),
    ]

    class Meta:
        icon = "time"
        label = "Timeplan"


BASE_BLOCKS = [
    ("paragraph", blocks.RichTextBlock(label="Rik tekst")),
    ("image", ImageWithTextBlock()),
    ("image_slider", ImageSliderBlock()),
    ("horizontal_rule", HorizontalRuleBlock()),
    ("featured", FeaturedBlock()),
    ("page_section_navigation", PageSectionNavigationBlock()),
    ("accordion", AccordionBlock()),
    ("fact_box", FactBoxBlock()),
    ("schedule", ScheduleBlock()),
    ("embed", EmbedBlock()),
    ("raw_html", blocks.RawHTMLBlock()),
]


@register_streamfield_block
class PageSectionBlock(blocks.StructBlock):
    ICON_CHOICES = (
        ("pigHeadLogo", "Grisehodelogo"),
        ("key", "Nøkkel"),
        ("ticket", "Billett"),
        ("shield", "Skjold"),
        ("bottle", "Flaske"),
        ("lostProperty", "Hittegods"),
        ("pigsty", "Grisebinge"),
        ("wheelchair", "Rullestol"),
        ("clock", "Klokke"),
        ("parking", "Parkering"),
        ("coins", "Mynter"),
        ("studioNote", "Note (STUDiO)"),
        ("studioCalendar", "Kalender (STUDiO)"),
        ("studioGuitar", "Gitar (STUDiO)"),
        ("studioDisco", "Discokule (STUDiO)"),
        ("studioMole", "Muldvarpen (STUDiO)"),
    )

    title = blocks.CharBlock(max_length=64, required=True, label="Tittel")
    background_color = blocks.ChoiceBlock(
        label="Bakgrunnsfarge",
        required=False,
        choices=BACKGROUND_COLOR_CHOICES,
    )
    icon = blocks.ChoiceBlock(
        label="Ikon",
        required=False,
        choices=ICON_CHOICES,
    )
    body = blocks.StreamBlock(
        [block for block in BASE_BLOCKS if block[0] != "page_section_navigation"]
        + [
            ("neuf_address", NeufAddressSectionBlock()),
            ("opening_hours", OpeningHoursSectionBlock()),
        ]
    )

    graphql_fields = [
        GraphQLString("title", required=True),
        GraphQLString("background_color", required=False),
        GraphQLString("icon", required=False),
        GraphQLStreamfield("body", required=True),
    ]

    class Meta:
        icon = "folder-open-1"
        label = "Seksjon"
