from django.db import models
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
from wagtail.models import Page
from wagtail.search import index

from dnscms.fields import CommonStreamField


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

    lead = RichTextField(features=["bold", "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")]


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

    lead = RichTextField(features=["bold", "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 VenuePage(Page):
    # no children
    subpage_types = []
    parent_page_types = ["venues.VenueIndex"]
    # should not be able to be shown in menus
    show_in_menus = False

    featured_image = models.ForeignKey(
        "images.CustomImage",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        help_text=("Bilde av lokalet"),
    )

    body = CommonStreamField

    show_as_bookable = models.BooleanField(
        default=True, help_text="Skal lokalet dukke i oversikten over lokaler som leies ut?"
    )
    floor = models.CharField(
        blank=True,
        max_length=255,
    )
    preposition = models.CharField(
        blank=True, max_length=255, help_text="Er man i eller på lokalet?"
    )
    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("body"),
        FieldPanel("floor", heading="Etasje"),
        FieldPanel("preposition", heading="Preposisjon"),
        FieldPanel("show_as_bookable", heading="Vis på utleieside"),
        MultiFieldPanel(
            heading="Kapabiliteter",
            children=capability_panels,
        ),
        MultiFieldPanel(
            heading="Kapasitet",
            children=capacity_panels,
        ),
    ]

    graphql_fields = [
        GraphQLImage("featured_image"),
        GraphQLStreamfield("body"),
        GraphQLString("floor"),
        GraphQLString("preposition"),
        GraphQLBoolean("show_as_bookable"),
        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("lead"),
        index.SearchField("body"),
    ]
