from django.conf import settings
from django.db import models
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _

# pylint: disable=E0401,E0611
from oxyan.themes.definitions import theme

from wagtail.admin.panels import (
    FieldPanel,
    FieldRowPanel,
    MultiFieldPanel,
)
from wagtail.contrib.settings.models import BaseSiteSetting, register_setting
from wagtail.search import index

from ocyan.core.fender import config
from ocyan.main.loading import get_ocyan_class, get_ocyan_classes

from .fields import CoyoteStreamField
from .forms import StyleSettingsForm

# pylint: disable=unbalanced-tuple-unpacking
(
    HomePageHeaderStreamBlock,
    HomePageContentStreamBlock,
    HomePageFooterStreamBlock,
    ContactPageHeaderStreamBlock,
    ContactPageContentStreamBlock,
    ContactPageFooterStreamBlock,
) = get_ocyan_classes(
    "coyote.stream_blocks",
    (
        "HomePageHeaderStreamBlock",
        "HomePageContentStreamBlock",
        "HomePageFooterStreamBlock",
        "ContactPageHeaderStreamBlock",
        "ContactPageContentStreamBlock",
        "ContactPageFooterStreamBlock",
    ),
)

AbstractBasePage = get_ocyan_class("ocyan_plugin_wagtail.models", "AbstractBasePage")


@register_setting
class StyleSettings(BaseSiteSetting):
    primary = models.CharField(max_length=7, default=theme["primary"])
    secondary = models.CharField(max_length=7, default=theme["secondary"])
    gray_900 = models.CharField(max_length=7, default=theme["gray-900"])
    gray_200 = models.CharField(max_length=7, default=theme["gray-200"])
    body_color = models.CharField(max_length=7, default=theme["body-color"])
    body_background = models.CharField(max_length=7, default=theme["body-background"])
    footer_background = models.CharField(
        max_length=7, default=theme["footer-background"]
    )
    header_background = models.CharField(
        max_length=7, default=theme["header-background"]
    )

    header_font = models.CharField(max_length=255, blank=True, null=True)
    paragraph_font = models.CharField(max_length=255, blank=True, null=True)
    header_font_family = models.CharField(max_length=255, blank=True, null=True)
    paragraph_font_family = models.CharField(max_length=255, blank=True, null=True)
    header_font_variant = models.CharField(max_length=255, default="")
    paragraph_font_variant = models.CharField(max_length=255, default="")

    border_radius = models.DecimalField(
        max_digits=6, decimal_places=2, default=theme["border-radius"]
    )

    header = models.CharField(
        max_length=255,
        null=True,
        choices=settings.OXYAN_HEADER_OPTIONS,
        default=theme.get("header", "basic"),
    )

    panels = [
        MultiFieldPanel(
            (
                FieldRowPanel(
                    (
                        FieldPanel("primary"),
                        FieldPanel("secondary"),
                    )
                ),
                FieldRowPanel(
                    (
                        FieldPanel("body_color"),
                        FieldPanel("gray_900"),
                        FieldPanel("gray_200"),
                    )
                ),
                FieldRowPanel(
                    (
                        FieldPanel("body_background"),
                        FieldPanel("footer_background"),
                        FieldPanel("header_background"),
                    )
                ),
            ),
            heading=_("Colors"),
            classname="collapsed",
        ),
        MultiFieldPanel(
            (
                FieldRowPanel(
                    (
                        FieldPanel("header_font_family"),
                        FieldPanel("header_font_variant"),
                        FieldPanel("paragraph_font_family"),
                        FieldPanel("paragraph_font_variant"),
                    ),
                    classname="fontpanel",
                ),
                FieldRowPanel(
                    (
                        FieldPanel("header_font"),
                        FieldPanel("paragraph_font"),
                    )
                ),
            ),
            heading=_("Typography"),
            classname="collapsed",
        ),
        MultiFieldPanel(
            (
                FieldPanel("border_radius"),
                # Do not display the header options if they are not available
                *([FieldPanel("header")] if settings.OXYAN_HEADER_OPTIONS else []),
            ),
            heading=_("General"),
            classname="collapsed",
        ),
    ]

    @cached_property
    def definitions(self):
        basket_counter_background = config.get(
            "themes", "basket-counter-background", self.primary
        )
        header_font_family = (
            [f'"{self.header_font_family}"', "sans-serif"]
            if self.header_font_family
            else None
        )
        paragraph_font_family = (
            [f'"{self.paragraph_font_family}"', "sans-serif"]
            if self.paragraph_font_family
            else None
        )

        return {
            # colors
            "primary": self.primary,
            "secondary": self.secondary,
            "gray-900": self.gray_900,
            "gray-200": self.gray_200,
            "body-color": self.body_color,
            "body-background": self.body_background,
            "footer-background": self.footer_background,
            "header-background": self.header_background,
            "basket-counter-background": basket_counter_background,
            # font
            "header-font-family": header_font_family,
            "font-family-sans-serif": paragraph_font_family,
            "font-weight-headers": (
                "700" if "bold" in self.header_font_variant else "400"
            ),
            "font-weight-normal": (
                "700" if "bold" in self.paragraph_font_variant else "400"
            ),
            "headings-font-style": (
                "italic" if "italic" in self.header_font_variant else "normal"
            ),
            "border-radius": self.border_radius,
            "header": self.header,
        }

    class Meta:
        verbose_name = "Template page settings"

    base_form_class = StyleSettingsForm


class CoyoteHomePage(AbstractBasePage):
    template = "coyote/pages/home.html"

    header = CoyoteStreamField(
        HomePageHeaderStreamBlock(),
        blank=True,
        verbose_name="Header",
        max_num=1,
        use_json_field=True,
    )
    content = CoyoteStreamField(
        HomePageContentStreamBlock(),
        blank=True,
        verbose_name=_("Content"),
        collapsed=True,
        use_json_field=True,
    )
    footer = CoyoteStreamField(
        HomePageFooterStreamBlock(),
        blank=True,
        verbose_name="Footer",
        max_num=1,
        use_json_field=True,
    )

    content_panels = AbstractBasePage.content_panels + [
        FieldPanel("header", classname="collapsible"),
        FieldPanel("content", classname="collapsible"),
        FieldPanel("footer", classname="collapsible"),
    ]

    search_fields = AbstractBasePage.search_fields + [
        index.SearchField("header", partial_match=True),
        index.SearchField("content", partial_match=True),
        index.SearchField("footer", partial_match=True),
    ]

    page_description = _(
        "Predefined layout that ensures your content always looks good."
    )

    class Meta:
        verbose_name = "Template page"


class CoyoteContactPage(AbstractBasePage):
    is_creatable = "ocyan.plugin.contact_form" in config.ocyan_plugins
    template = "coyote/pages/contact.html"

    header = CoyoteStreamField(
        ContactPageHeaderStreamBlock(),
        blank=True,
        verbose_name="Header",
        max_num=1,
        use_json_field=True,
    )
    content = CoyoteStreamField(
        ContactPageContentStreamBlock(),
        blank=True,
        verbose_name=_("Content"),
        use_json_field=True,
    )
    footer = CoyoteStreamField(
        ContactPageFooterStreamBlock(),
        blank=True,
        verbose_name="Footer",
        max_num=1,
        use_json_field=True,
    )

    content_panels = AbstractBasePage.content_panels + [
        FieldPanel("header", classname="collapsible"),
        FieldPanel("content", classname="collapsible"),
        FieldPanel("footer", classname="collapsible"),
    ]

    search_fields = AbstractBasePage.search_fields + [
        index.SearchField("header", partial_match=True),
        index.SearchField("content", partial_match=True),
        index.SearchField("footer", partial_match=True),
    ]

    page_description = _(
        "Predefined layout that ensures your contact page always looks good."
    )

    class Meta:
        verbose_name = "Contact template page"
