from django.db import models
from django.utils.translation import gettext_lazy as _
from grapple.helpers import register_singular_query_field
from grapple.models import (
    GraphQLBoolean,
    GraphQLImage,
    GraphQLRichText,
    GraphQLStreamfield,
    GraphQLString,
)
from wagtail.admin.panels import FieldPanel, FieldRowPanel, MultiFieldPanel
from wagtail.fields import RichTextField, StreamField
from wagtail.models import Page, PageManager
from wagtail.search import index
from wagtail_headless_preview.models import HeadlessMixin

from dnscms.blocks import ImageSliderBlock
from dnscms.fields import CommonStreamField
from dnscms.wordpress.models import DeferWPFieldsManagerMixin, WPImportedPageMixin


class VenuePageManager(DeferWPFieldsManagerMixin, PageManager):
    pass


@register_singular_query_field("venueIndex")
class VenueIndex(HeadlessMixin, Page):
    # there can only be one venue index page
    max_count = 1
    subpage_types = ["venues.VenuePage"]

    lead = RichTextField(features=["italic", "link"], blank=True)
    body = CommonStreamField

    content_panels = Page.content_panels + [
        FieldPanel("lead", heading="Ingress"),
        FieldPanel("body", heading="Innhold"),
    ]

    graphql_fields = [GraphQLRichText("lead"), GraphQLStreamfield("body")]

    class Meta:
        verbose_name = _("venue index")
        verbose_name_plural = _("venue indexes")


@register_singular_query_field("venueRentalIndex")
class VenueRentalIndex(HeadlessMixin, Page):
    # there can only be one venue index page
    max_count = 1
    subpage_types = []

    lead = RichTextField(features=["italic", "link"], blank=True)
    body = CommonStreamField

    content_panels = Page.content_panels + [
        FieldPanel("lead", heading="Ingress"),
        FieldPanel("body", heading="Innhold"),
    ]

    graphql_fields = [GraphQLRichText("lead"), GraphQLStreamfield("body")]

    class Meta:
        verbose_name = _("rentals page")
        verbose_name_plural = _("rentals pages")


class VenuePage(HeadlessMixin, WPImportedPageMixin, Page):
    # no children
    subpage_types = []
    parent_page_types = ["venues.VenueIndex"]
    # should not be able to be shown in menus
    show_in_menus = False

    objects = VenuePageManager()

    featured_image = models.ForeignKey(
        "images.CustomImage",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        help_text=(
            "Bilde av lokalet som brukes i oversiktvisninger. "
            "Blir ikke automatisk med i bildekarusellen."
        ),
    )

    images = StreamField(
        [
            ("image_slider", ImageSliderBlock()),
        ],
        block_counts={"image_slider": {"max_num": 1}},
        blank=True,
        help_text=(
            "Bilder som vises i bildekarusellen når man leser mer om lokalet. "
            "Inkluderer ikke automatisk bildet over."
        ),
    )

    body = CommonStreamField

    show_as_bookable = models.BooleanField(
        default=True, help_text="Skal lokalet vises i oversikten over lokaler som leies ut?"
    )
    show_in_overview = models.BooleanField(
        default=True,
        help_text="Skal lokalet vises i oversikten over lokaler på undersiden /lokaler?",
    )
    floor = models.CharField(
        blank=True,
        max_length=255,
    )
    preposition = models.CharField(
        blank=True, max_length=255, help_text="Er man i eller på lokalet?"
    )
    tech_specs_url = models.URLField(
        blank=True,
        max_length=512,
        help_text="Lenke til tekniske spesifikasjoner for lokalet",
    )
    used_for = models.CharField(blank=True, max_length=255)
    capability_audio = models.CharField(blank=True, max_length=255)
    capability_audio_video = models.CharField(blank=True, max_length=255)
    capability_bar = models.CharField(blank=True, max_length=255)
    capability_lighting = models.CharField(blank=True, max_length=255)
    capacity_legal = models.CharField(blank=True, max_length=255)
    capacity_standing = models.CharField(blank=True, max_length=255)
    capacity_sitting = models.CharField(blank=True, max_length=255)

    # "used_for": "Konsert, foredrag, fest, debatt, teater",

    capability_panels = [
        FieldRowPanel(
            children=[
                FieldPanel("capability_bar", heading="Bar"),
                FieldPanel("capability_audio", heading="Lyd"),
                FieldPanel("capability_lighting", heading="Lys"),
                FieldPanel("capability_audio_video", heading="A/V"),
            ],
        ),
    ]

    capacity_panels = [
        FieldRowPanel(
            children=[
                FieldPanel("capacity_legal", heading="Branntillatelse for"),
                FieldPanel("capacity_sitting", heading="Stående"),
                FieldPanel("capacity_standing", heading="Sittende"),
            ],
        ),
    ]

    content_panels = Page.content_panels + [
        FieldPanel("featured_image"),
        FieldPanel("images", heading="Bilder"),
        FieldPanel("body", heading="Innhold"),
        FieldPanel("floor", heading="Etasje"),
        FieldPanel("preposition", heading="Preposisjon"),
        FieldPanel("show_as_bookable", heading="Vis på utleieside"),
        FieldPanel("show_in_overview", heading="Vis i lokaleoversikt"),
        FieldPanel("used_for", heading="Egnet for"),
        MultiFieldPanel(
            heading="Kapabiliteter",
            children=capability_panels,
        ),
        MultiFieldPanel(
            heading="Kapasitet",
            children=capacity_panels,
        ),
        FieldPanel("tech_specs_url", heading="Tekniske spesifikasjoner"),
    ]

    graphql_fields = [
        GraphQLImage("featured_image"),
        GraphQLStreamfield("images"),
        GraphQLStreamfield("body"),
        GraphQLString("floor"),
        GraphQLString("preposition"),
        GraphQLString("used_for"),
        GraphQLString("tech_specs_url"),
        GraphQLBoolean("show_as_bookable"),
        GraphQLBoolean("show_in_overview"),
        GraphQLString("capability_audio"),
        GraphQLString("capability_audio_video"),
        GraphQLString("capability_bar"),
        GraphQLString("capability_lighting"),
        GraphQLString("capacity_legal"),
        GraphQLString("capacity_standing"),
        GraphQLString("capacity_sitting"),
    ]

    search_fields = Page.search_fields + [
        index.SearchField("body"),
    ]

    class Meta:
        verbose_name = _("venue")
        verbose_name_plural = _("venues")
