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

from tuning import help

RICH_TEXT_FEATURES = [
    "h2",
    "h3",
    "h4",
    "h5",
    "bold",
    "italic",
    "ol",
    "ul",
    "hr",
    "link",
    "document-link",
    "image",
]


class MenuItemBlock(blocks.StructBlock):
    name = blocks.CharBlock(label="název")
    page = blocks.PageChooserBlock(
        label="stránka",
        page_type=["green_deal.GreenDealHomePage", "green_deal.GreenDealSubPage"],
    )

    class Meta:
        label = "stránka"


class FaqBlock(blocks.StructBlock):
    question = blocks.TextBlock()
    answer = blocks.RichTextBlock()


class ColumnsTextBlock(blocks.StructBlock):
    left_text = blocks.RichTextBlock(label="levý sloupec", features=RICH_TEXT_FEATURES)
    right_text = blocks.RichTextBlock(
        label="pravý sloupec", features=RICH_TEXT_FEATURES
    )

    class Meta:
        label = "text dva sloupce"
        icon = "doc-full"


class ColumnsFaqBlock(blocks.StructBlock):
    left_text = blocks.RichTextBlock(label="levý sloupec", features=RICH_TEXT_FEATURES)
    faqs = blocks.ListBlock(FaqBlock(), label="faq")


class NewsBlock(blocks.StructBlock):
    header = blocks.CharBlock()
    perex = blocks.RichTextBlock(features=RICH_TEXT_FEATURES)
    img = ImageChooserBlock()
    link = blocks.URLBlock()
    date = blocks.DateBlock(default=timezone.now())


class StudiesBlock(blocks.StructBlock):
    header = blocks.CharBlock()
    text = blocks.RichTextBlock(features=RICH_TEXT_FEATURES)

    class Meta:
        label = "Text 1 sloupec"
        icon = "doc-full"


class GreenDealHomePage(MetadataPageMixin, Page):
    ### FIELDS
    content = StreamField(
        [
            ("text_2_columns", ColumnsTextBlock()),
            ("text_1_column", StudiesBlock()),
            ("FAQ_columns", ColumnsFaqBlock()),
            ("news", blocks.ListBlock(NewsBlock(), label="aktuality")),
        ],
        verbose_name="obsah stránky",
        blank=True,
    )
    # settings
    matomo_id = models.IntegerField(
        "Matomo ID pro sledování návštěvnosti", blank=True, null=True
    )
    top_menu = StreamField(
        [("item", MenuItemBlock())],
        verbose_name="horní menu",
        blank=True,
    )

    ### 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 = [
        FieldPanel("matomo_id"),
        StreamFieldPanel("top_menu"),
    ]

    ### RELATIONS

    subpage_types = [
        "green_deal.GreenDealSubPage",
    ]

    ### OTHERS

    class Meta:
        verbose_name = "Zelená dohoda pro regiony"

    @property
    def root_page(self):
        return self


class GreenDealSubPage(Page, MetadataPageMixin):
    ### FIELDS
    content = StreamField(
        [
            ("text_2_columns", ColumnsTextBlock()),
            ("text_1_column", StudiesBlock()),
            ("FAQ_columns", ColumnsFaqBlock()),
            ("news", blocks.ListBlock(NewsBlock(), label="aktuality")),
        ],
        verbose_name="obsah stránky",
        blank=True,
    )

    ### 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"),
        ),
    ]

    ### RELATIONS

    parent_page_types = [
        "green_deal.GreenDealHomePage",
    ]
    subpage_types = [
        "green_deal.GreenDealSubPage",
    ]

    ### OTHERS

    class Meta:
        verbose_name = "Zelená dohoda pro regiony"

    @property
    def root_page(self):
        if not hasattr(self, "_root_page"):
            self._root_page = (
                self.get_ancestors().type(GreenDealHomePage).specific().last()
            )
        return self._root_page