from wagtail.blocks import (
    CharBlock,
    ListBlock,
    ChoiceBlock,
    PageChooserBlock,
    RichTextBlock,
    StreamBlock,
    StructBlock,
)
from wagtail.embeds.blocks import EmbedBlock
from wagtail.images.blocks import ImageChooserBlock

class ImageBlock(StructBlock):
    image = ImageChooserBlock(required=True)
    caption = CharBlock(required=False)
    attribution = CharBlock(required=False)

    class Meta:
        icon = "image"
        template = "base/blocks/image_block.html"


class HeadingBlock(StructBlock):
    heading_text = CharBlock(classname="title", required=True)
    size = ChoiceBlock(
        choices=[
            ("", "Choisissez un niveau de titre"),
            ("h2", "H2"),
            ("h3", "H3"),
            ("h4", "H4"),
        ],
        blank=True,
        required=False,
    )

    class Meta:
        icon = "title"
        template = "base/blocks/heading_block.html"


class AlertBlock(StructBlock):
    alert_type = ChoiceBlock(
        choices=[
            ("alert-success", "Vert"),
            ("alert-link", "Bleu"),
            ("alert-warning", "Jaune"),
            ("alert-danger", "Rouge"),
            ("alert-dark", "Noir"),
            ("alert-primary", "Couleur primaire"),
            ("alert-secondary", "Couleur secondaire"),
            ("alert-tertiary", "Couleur tertiaire"),
            ("alert-quaternary", "Couleur quaternaire"),
        ]
    )
    alert_options = ChoiceBlock(
        choices=[
            ("alert-sm", "Petit"),
            ("alert-lg", "Grand"),
            ("alert-dismissible", "Réductible"),
        ],
        blank=True,
        null=True,
        required=False,
    )
    alert_body = CharBlock(features=["bold", "italic", "link"])

    class Meta:
        template = "base/blocks/alert_block.html"

class CardBlock(StructBlock):
    card = PageChooserBlock(required=True, help_text="Sélectionnez une page pour créer un lien.")

    class Meta:
        # label = 'Page mises en avant'
        template = "base/blocks/card_block.html"

class BaseStreamBlock(StreamBlock):
    heading_block = HeadingBlock()
    paragraph_block = RichTextBlock(icon="pilcrow")
    alert_block = AlertBlock()
    card_block = CardBlock()
    image_block = ImageBlock()
    embed_block = EmbedBlock(
        help_text="Insert a URL to embed. For example, https://www.youtube.com/watch?v=SGJFWirQ3ks",
        icon="media",
    )



class FeaturedPostsBlock(StructBlock):
    heading = CharBlock()  # Titre de la section
    text = RichTextBlock(features=["bold", "italic", "link"], required=False)  # Texte de la section, facultatif
    posts = ListBlock(PageChooserBlock(page_type="applis.ApplisPage"))  # Liste de pages de blog sélectionnées

    class Meta:
        icon = "folder-open-inverse"  # Icône pour représenter ce bloc dans l'interface d'administration
        template = "base/blocks/featured_posts_block.html"  # Template utilisé pour ce bloc


class FeaturedBlock(StructBlock):
    featured_title = CharBlock(
        label='Titre de la section'
    )
    featured_paragraph = RichTextBlock(
        label='Paragraphe de la section'
    )
    featured_posts = ListBlock(PageChooserBlock(page_type="applis.ApplisPage"))  # Liste de pages de blog sélectionnées

    class Meta:
        template = 'base/blocks/featured_block.html'
        icon = 'snippet'
        label = 'Pages mises en avant'

# Définition du bloc de flux de la page standard
class StandardStreamBlock(BaseStreamBlock):
    # Ajoute FeaturedPostsBlock au groupe "Sections"
    featured_posts = FeaturedBlock(group="Sections")



