from django import forms
from django.utils.translation import gettext_lazy as _

from wagtail.admin.forms import WagtailAdminModelForm

from .utils import get_google_fonts


class ColorPickerWidget(forms.TextInput):
    template_name = "coyote/forms/widgets/colorpicker.html"


class FontSelectWidget(forms.TextInput):
    template_name = "coyote/forms/widgets/fontselect.html"

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        context["fonts"] = get_google_fonts()
        return context


class FontPreviewWidget(forms.TextInput):
    template_name = "coyote/forms/widgets/fontpreview.html"


class StyleSettingsForm(WagtailAdminModelForm):
    primary = forms.CharField(widget=ColorPickerWidget(), label=_("Primary color"))
    secondary = forms.CharField(widget=ColorPickerWidget(), label=_("Secondary color"))
    gray_900 = forms.CharField(widget=ColorPickerWidget(), label=_("Dark accent color"))
    gray_200 = forms.CharField(
        widget=ColorPickerWidget(), label=_("Light accent color")
    )
    body_color = forms.CharField(widget=ColorPickerWidget(), label=_("Body text color"))
    body_background = forms.CharField(
        widget=ColorPickerWidget(), label=_("Body background color")
    )
    footer_background = forms.CharField(
        widget=ColorPickerWidget(), label=_("Footer background color")
    )
    header_background = forms.CharField(
        widget=ColorPickerWidget(), label=_("Header background color")
    )

    header_font_variant = forms.CharField(
        label=_("Variant"),
        widget=forms.Select(
            choices=map(lambda x: (x, x), ["normal", "bold", "italic", "bold italic"])
        ),
    )
    paragraph_font_variant = forms.CharField(
        label=_("Variant"),
        widget=forms.Select(choices=map(lambda x: (x, x), ["normal", "bold"])),
    )

    header_font_family = forms.CharField(
        required=False,
        widget=FontSelectWidget(),
        label=_("Heading font family"),
        help_text=_("Used for headings across the Coyote theme."),
    )
    paragraph_font_family = forms.CharField(
        required=False,
        widget=FontSelectWidget(),
        label=_("Body font family"),
        help_text=_("Used for paragraph text across the Coyote theme."),
    )

    header_font = forms.CharField(
        required=False,
        widget=FontPreviewWidget(
            attrs=dict(preview="""
                    <h1>Lorem ipsum dolor</h1>
                    <h2>Lorem ipsum dolor</h2>
                    <h3>Lorem ipsum dolor</h3>
                    <h4>Lorem ipsum dolor</h4>
                    <h5>Lorem ipsum dolor</h5>
                    <h6>Lorem ipsum dolor</h6>
                """),
        ),
        label=_("Heading font"),
    )
    paragraph_font = forms.CharField(
        required=False,
        widget=FontPreviewWidget(
            attrs=dict(preview="""
                    <p>
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia odio vitae vestibulum vestibulum. 
                        Cras venenatis euismod malesuada. Donec eget lorem non urna volutpat tincidunt at non eros.
                        <br><br>
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia odio vitae vestibulum vestibulum. 
                        Cras venenatis euismod malesuada. Donec eget lorem non urna volutpat tincidunt at non eros.
                    </p>
                """),
        ),
        label=_("Body font"),
    )

    border_radius = forms.DecimalField(
        label=_("Border radius"),
        help_text=_("Applies the shared border radius used by Coyote components."),
    )

    class Media:
        js = (
            "wagtailadmin/coyote/stylesettingscolorpicker.js",
            "external/js/jquery.js",
            "external/js/select2.js",
            "fontadmin/js/font.js",
        )
        css = {
            "all": [
                "wagtailadmin/coyote/stylesettingscolorpicker.css",
                "external/css/select2.css",
                "external/css/select2-bs5-theme.css",
                "fontadmin/css/font.css",
            ],
        }
