from django.db import models
from django.utils.translation import gettext_lazy
from wagtail.admin.edit_handlers import (
    FieldPanel,
    HelpPanel,
    MultiFieldPanel,
    StreamFieldPanel,
)
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.documents.blocks import DocumentChooserBlock
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtailmetadata.models import MetadataPageMixin

from shared.mixins import MatomoMixin
from tuning import help
from uniweb import models as uniweb


class PdpContentMixin(models.Model):
    content = StreamField(
        uniweb.UNIWEB_STREAM_CHOICES
        + [
            (
                "jupyter",
                DocumentChooserBlock(label="Jupyter notebook", group="ostatní"),
            ),
        ],
        verbose_name="obsah stránky",
        blank=True,
    )

    class Meta:
        abstract = True


class PdpHomePage(
    Page,
    PdpContentMixin,
    uniweb.UniwebTopmenuMixin,
    MetadataPageMixin,
    MatomoMixin,
):

    ### PANELS

    content_panels = Page.content_panels + [
        StreamFieldPanel("content"),
    ]

    promote_panels = [
        MultiFieldPanel(
            [
                FieldPanel("seo_title"),
                FieldPanel("search_description"),
                ImageChooserPanel("search_image"),
                HelpPanel(help.build(help.IMPORTANT_TITLE)),
            ],
            gettext_lazy("Common page configuration"),
        ),
    ]

    settings_panels = [
        MultiFieldPanel(
            [
                # definovano v MatomoMixin, TODO obstrahovat rozsireni settings_panels do mixinu
                FieldPanel("matomo_id"),
            ],
            "nastavení webu",
        ),
        StreamFieldPanel("top_menu"),  # z uniwebTopmenuMixin
    ]

    subpage_types = [
        "pdp.PdpSubPage",
    ]

    ### OTHERS

    class Meta:
        verbose_name = "Pirátská datová platforma"

    @property
    def root_page(self):
        return self


class PdpSubPage(Page, PdpContentMixin, uniweb.SubpageMixin, MetadataPageMixin):
    ### FIELDS

    ### PANELS
    promote_panels = [
        MultiFieldPanel(
            [
                FieldPanel("slug"),
                FieldPanel("seo_title"),
                FieldPanel("search_description"),
                ImageChooserPanel("search_image"),
                HelpPanel(help.build(help.NO_SEO_TITLE, help.NO_SEARCH_IMAGE)),
            ],
            gettext_lazy("Common page configuration"),
        ),
    ]

    content_panels = Page.content_panels + [
        StreamFieldPanel("content"),
    ]

    settings_panels = []

    ### RELATIONS
    parent_page_types = ["pdp.PdpHomePage"]
    subpage_types = []

    ### OTHERS
    class Meta:
        verbose_name = "Pirátská datová platforma - podstránka"