from django.db import models
from django.utils.text import slugify
from wagtail.admin.panels import FieldPanel
from wagtail.fields import RichTextField, StreamField
from wagtail.images.blocks import ImageChooserBlock

from books.blocks import (
    CMSDocumentBlock,
    DocumentBlock,
    GeniallyBlock,
    InfogramBlock,
    InstrumentTextBlock,
    LinkBlock,
    SectionTitleBlock,
    SubtitleBlock,
    ThinglinkBlock,
    VideoBlock,
)
from core.constants import DEFAULT_RICH_TEXT_FEATURES
from core.wagtail_utils import StrictHierarchyPage
from django.utils.translation import gettext_lazy as _

LANGUAGE_COMMUNICATION = "language_communication"
SOCIETY = "society"
INTERDISCIPLINARY = "interdisciplinary"
LANGUAGE_COMMUNICATION_LABEL = "Sprache & Kommunikation"
SOCIETY_LABEL = "Gesellschaft"
INTERDISCIPLINARY_LABEL = "Überfachliche Instrumente"


class InstrumentCategory(models.Model):
    name = models.CharField(max_length=255, unique=True)
    background = models.CharField("background color", max_length=7)
    foreground = models.CharField("foreground color", max_length=7)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = _("instrument categories")
        verbose_name = _("instrument category")


def default_category():
    return InstrumentCategory.objects.first().pk


class InstrumentType(models.Model):
    class Meta:
        verbose_name = _("instrument type")
        verbose_name_plural = _("instrument types")

    CATEGORY_CHOICES = (
        (LANGUAGE_COMMUNICATION, LANGUAGE_COMMUNICATION_LABEL),
        (SOCIETY, SOCIETY_LABEL),
        (INTERDISCIPLINARY, INTERDISCIPLINARY_LABEL),
    )

    name = models.CharField(max_length=255, unique=True)
    category = models.ForeignKey(
        InstrumentCategory,
        on_delete=models.PROTECT,
        null=False,
        default=default_category,
        related_name="instrument_types",
    )

    @property
    def type(self):
        return slugify(self.name.lower())

    def __str__(self):
        return self.type


class BasicKnowledge(StrictHierarchyPage):
    class Meta:
        verbose_name = _("instrument")
        verbose_name_plural = _("instruments")

    parent_page_types = ["books.book"]

    intro = RichTextField(
        features=DEFAULT_RICH_TEXT_FEATURES, default="", blank=True)

    contents = StreamField(
        [
            ("text_block", InstrumentTextBlock()),
            ("image_block", ImageChooserBlock()),
            ("link_block", LinkBlock()),
            ("video_block", VideoBlock()),
            ("document_block", DocumentBlock()),
            ("section_title", SectionTitleBlock()),
            ("infogram_block", InfogramBlock()),
            ("genially_block", GeniallyBlock()),
            ("thinglink_block", ThinglinkBlock()),
            ("subtitle", SubtitleBlock()),
            ("cms_document_block", CMSDocumentBlock()),
        ],
        null=True,
        blank=True,
        use_json_field=True,
    )

    new_type = models.ForeignKey(
        InstrumentType, null=True, on_delete=models.PROTECT, related_name="instruments"
    )

    old_type = models.CharField(
        max_length=100, choices=InstrumentType.CATEGORY_CHOICES, blank=True
    )

    content_panels = [
        FieldPanel("title", classname="full title"),
        FieldPanel("new_type"),
        FieldPanel("intro"),
        FieldPanel("contents"),
    ]
