from django.utils.html import format_html
from django.templatetags.static import static
from wagtail import hooks
from wagtail.snippets.models import register_snippet
from wagtail.snippets.views.snippets import SnippetViewSet
from wagtail.admin.rich_text.converters.html_to_contentstate import BlockElementHandler
from wagtail.admin.rich_text.editors.draftail import features as draftail_features
from draftjs_exporter.dom import DOM






@hooks.register('construct_snippet_listing_buttons')
def add_snippet_listing_buttons(buttons, snippet, user, context=None):
    if hasattr(snippet, 'admin_thumb'):
        buttons.append({
            'url': None,
            'label': '',
            'classname': 'icon icon-image',
            'title': 'Image preview',
            'attrs': {
                'data-preview-image': snippet.admin_thumb.get_rendition('max-100x100').url
                if snippet.admin_thumb else ''
            },
        })



@hooks.register('register_rich_text_features')
def register_bootstrap_features(features):
    """Bootstrap-Klassen für Rich-Text-Elemente registrieren"""
    
    # Bootstrap Paragraph
    feature_name = 'bootstrap_paragraph'
    features.register_editor_plugin(
        'draftail', feature_name,
        draftail_features.BlockFeature({
            'label': 'Bootstrap P',
            'type': feature_name,
            'element': 'p',
            'className': 'mb-4'
        })
    )
    features.register_converter_rule('contentstate', feature_name, {
        'from_database_format': {'p[class="mb-4"]': BlockElementHandler(feature_name)},
        'to_database_format': {'element': 'p', 'props': {'class': 'mb-4'}}
    })
    features.default_features.append(feature_name)

    # Bootstrap Button
    feature_name = 'bootstrap_buttons'
    features.register_editor_plugin(
        'draftail', feature_name,
        draftail_features.InlineStyleFeature({
            'label': 'Button',
            'type': feature_name,
            'style': {'class': 'btn btn-primary'}
        })
    )
    features.register_converter_rule('contentstate', feature_name, {
        'from_database_format': {'a[class="btn btn-primary"]': BlockElementHandler(feature_name)},
        'to_database_format': {'element': 'a', 'props': {'class': 'btn btn-primary'}}
    })
    features.default_features.append(feature_name)

    # Bootstrap Table
    feature_name = 'bootstrap_tables'
    features.register_editor_plugin(
        'draftail', feature_name,
        draftail_features.BlockFeature({
            'label': 'Table',
            'type': feature_name,
            'element': 'table',
            'className': 'table table-bordered'
        })
    )
    features.register_converter_rule('contentstate', feature_name, {
        'from_database_format': {'table[class="table table-bordered"]': BlockElementHandler(feature_name)},
        'to_database_format': {'element': 'table', 'props': {'class': 'table table-bordered'}}
    })
    features.default_features.append(feature_name)

    # Bootstrap Headings
    for i in range(2, 7):
        feature_name = f'bootstrap_h{i}'
        features.register_editor_plugin(
            'draftail', feature_name,
            draftail_features.BlockFeature({
                'label': f'H{i}',
                'type': feature_name,
                'element': f'h{i}',
                'className': 'mt-4 mb-3'
            })
        )
        features.register_converter_rule('contentstate', feature_name, {
            'from_database_format': {f'h{i}[class="mt-4 mb-3"]': BlockElementHandler(feature_name)},
            'to_database_format': {'element': f'h{i}', 'props': {'class': 'mt-4 mb-3'}}
        })
        features.default_features.append(feature_name)

@hooks.register('insert_editor_css')
def editor_css():
    """Custom and Bootstrap CSS for the Wagtail editor."""
    return format_html(
        '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">'
        '<style>'
        '.certification-editor {{ /* Custom Styles */ }}'
        '.Draftail-block--bootstrap_paragraph {{ margin-bottom: 1rem; }}'
        '.Draftail-block--bootstrap_buttons {{ display: inline-block; padding: 0.375rem 0.75rem; '
        'background-color: #007bff; color: white; border-radius: 0.25rem; }}'
        '.Draftail-block--bootstrap_tables {{ width: 100%; border: 1px solid #dee2e6; }}'
        '</style>'
    )