# Copyright: 2017-2023 MoinMoin:RogerHaase
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
Support the use of Font Awesome icons within wiki content.

Usage:
    <<FontAwesome(classes,color,size)>>
    <<FontAwesome(classes)>>
    <<FontAwesome(classes,,size)>>
    <<FontAwesome(classes,color)>>

    <<FontAwesome(regular thumbs-up,red,2)>>

Where:
    classes: One or more Font Awesome classes without the leading "fa-", separated by spaces.
    color: Optional hex color code (#f00 or #ff0000) or an HTML color name.
    size: Optional integer or float size in em units.
"""


from moin.utils.tree import html, moin_page
from moin.macros._base import MacroInlineBase, fail_message
from moin.i18n import _


class Macro(MacroInlineBase):
    def macro(self, content, arguments, page_url, alternative):
        if not arguments:
            err_msg = _("Missing font name, syntax is <<FontAwesome(name,color,size)>>")
            return fail_message(err_msg, alternative)

        args = arguments[0].split(",")
        fonts = args[0].split()
        color = args[1].strip() if len(args) > 1 else ""
        size = args[2].strip() if len(args) > 2 else ""

        if color.startswith("#"):
            try:
                int(color[1:], 16)
                assert len(color) in (4, 7)
            except (ValueError, AssertionError):
                color = ""
        if size:
            try:
                s = float(size)
                assert s > 0.1
                assert s < 99
            except (ValueError, AssertionError):
                size = ""
        classes = []
        for font in fonts:
            f = font if font.startswith("fa-") else "fa-" + font
            classes.append(f)
        if "fa-solid" not in classes and "fa-regular" not in classes and "fa-brands" not in classes:
            classes.insert(0, "fa-solid")
        if "fa-spin-reverse" in classes and "fa-spin" not in classes:
            classes.insert(0, "fa-spin")
        classes = " ".join(classes)

        attrib = {moin_page.class_: classes}
        attrib[html.data_style] = ",".join((color, size))
        return moin_page.span(attrib=attrib)
