from django.db import models
from grapple.helpers import register_singular_query_field
from grapple.models import (
    GraphQLImage,
    GraphQLRichText,
    GraphQLStreamfield,
    GraphQLString,
)
from wagtail.admin.panels import FieldPanel
from wagtail.fields import RichTextField
from wagtail.models import Page
from wagtail.search import index
from wagtail_wordpress_import.models import WPImportedPageMixin

from dnscms.fields import CommonStreamField


@register_singular_query_field("associationIndex")
class AssociationIndex(Page):
    max_count = 1
    subpage_types = ["associations.AssociationPage"]

    lead = RichTextField(features=["italic", "link"])

    body = CommonStreamField

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

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

    search_fields = Page.search_fields


class AssociationPage(WPImportedPageMixin, Page):
    subpage_types = []
    parent_page_types = ["associations.AssociationIndex"]
    show_in_menus = False

    class AssociationType(models.TextChoices):
        FORENING = "forening", "Forening"
        UTVALG = "utvalg", "Utvalg"

    excerpt = models.TextField(max_length=512, blank=False)
    lead = RichTextField(features=["italic", "link"], blank=True)
    body = CommonStreamField
    association_type = models.CharField(
        max_length=64, choices=AssociationType, default=AssociationType.FORENING
    )
    logo = models.ForeignKey(
        "images.CustomImage",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
    )
    website_url = models.URLField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel(
            "excerpt",
            heading="Utdrag",
            help_text="En veldig kort oppsummering av innholdet nedenfor. Brukes i listevisninger.",
        ),
        FieldPanel("lead", heading="Ingress"),
        FieldPanel("body", heading="Innhold"),
        FieldPanel("logo"),
        FieldPanel("association_type", heading="Type"),
        FieldPanel("website_url", heading="Nettside"),
    ]

    graphql_fields = [
        GraphQLString("excerpt"),
        GraphQLRichText("lead"),
        GraphQLStreamfield("body"),
        GraphQLImage("logo"),
        GraphQLString("website_url"),
        GraphQLString("association_type"),
    ]

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

    settings_panels = Page.settings_panels + WPImportedPageMixin.wordpress_panels

    def import_wordpress_data(self, data):
        import html

        # Wagtail page model fields
        self.title = html.unescape(data["title"])
        self.slug = data["slug"]
        self.first_published_at = data["first_published_at"]
        self.last_published_at = data["last_published_at"]
        self.latest_revision_created_at = data["latest_revision_created_at"]
        self.search_description = data["search_description"]

        # debug fields
        self.wp_post_id = data["wp_post_id"]
        self.wp_post_type = data["wp_post_type"]
        self.wp_link = data["wp_link"]
        self.wp_raw_content = data["wp_raw_content"]
        self.wp_block_json = data["wp_block_json"]
        self.wp_processed_content = data["wp_processed_content"]
        self.wp_normalized_styles = data["wp_normalized_styles"]
        self.wp_post_meta = data["wp_post_meta"]

        # own model fields
        self.body = data["body"] or ""

        meta = data["wp_post_meta"]
        self.association_type = meta.get("neuf_associations_type").lower()
        self.website_url = meta.get("neuf_associations_homepage") or ""

        self.excerpt = meta.get("excerpt_encoded") or "TODO"
