from grapple.helpers import register_streamfield_block
from grapple.models import GraphQLForeignKey, GraphQLStreamfield, GraphQLString
from wagtail import blocks
from wagtail.snippets.blocks import SnippetChooserBlock


@register_streamfield_block
class ContactEntityBlock(blocks.StructBlock):
    contact_entity = SnippetChooserBlock("contacts.ContactEntity")

    graphql_fields = [
        GraphQLForeignKey("contact_entity", "contacts.ContactEntity"),
    ]


@register_streamfield_block
class ContactListBlock(blocks.ListBlock):
    pass


ContactEntityList = ContactListBlock(ContactEntityBlock(), label="Liste med kontaktpunkter")


@register_streamfield_block
class ContactSubsectionBlock(blocks.StructBlock):
    title = blocks.CharBlock(max_length=64, required=True, label="Tittel")
    text = blocks.RichTextBlock(features=["bold", "italic", "link"])
    blocks = blocks.StreamBlock(
        [
            ("contact_entity_list", ContactEntityList),
        ],
        required=False,
    )

    graphql_fields = [
        GraphQLString("title", required=True),
        GraphQLString("text", required=False),
        GraphQLStreamfield("blocks", required=False),
    ]

    class Meta:
        icon = "folder-open-1"


@register_streamfield_block
class ContactSectionBlock(blocks.StructBlock):
    title = blocks.CharBlock(max_length=64, required=True, label="Tittel")
    text = blocks.RichTextBlock(features=["bold", "italic", "link"])
    blocks = blocks.StreamBlock(
        [
            ("contact_entity_list", ContactEntityList),
            ("contact_subsection", ContactSubsectionBlock(label="Kontaktunderseksjon")),
        ],
        required=False,
    )

    graphql_fields = [
        GraphQLString("title", required=True),
        GraphQLString("text", required=False),
        GraphQLStreamfield("blocks", required=False),
    ]

    class Meta:
        icon = "folder-open-1"
