# retoor <retoor@molodetz.nl>
from __future__ import annotations

from html import escape

GRADE_COLORS: dict[str, str] = {
    "A": "#34a853",
    "B": "#7cb342",
    "C": "#fbbc04",
    "D": "#fb8c00",
    "F": "#ea4335",
}
LABEL_COLOR: str = "#555555"
PENDING_COLOR: str = "#9e9e9e"
CHAR_WIDTH: float = 6.3
HORIZONTAL_PADDING: float = 10.0
BADGE_HEIGHT: int = 20
LABEL_TEXT: str = "authenticity · human"


def _segment_width(text: str) -> float:
    return len(text) * CHAR_WIDTH + HORIZONTAL_PADDING


def render_badge(human_percent: float | str | None, grade: str | None, report_url: str) -> str:
    percent: float | None = None
    if human_percent is not None:
        try:
            percent = float(human_percent)
        except (TypeError, ValueError):
            percent = None
    if percent is None or not grade:
        value_text = "analyzing"
        value_color = PENDING_COLOR
    else:
        value_text = f"{percent:.0f}% · {grade}"
        value_color = GRADE_COLORS.get(grade, PENDING_COLOR)
    label_width = _segment_width(LABEL_TEXT)
    value_width = _segment_width(value_text)
    total_width = label_width + value_width
    label_center = label_width / 2.0
    value_center = label_width + value_width / 2.0
    safe_url = escape(report_url, quote=True)
    safe_value = escape(value_text)
    safe_label = escape(LABEL_TEXT)
    return (
        f'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" '
        f'width="{total_width:.0f}" height="{BADGE_HEIGHT}" role="img" aria-label="{safe_label}: {safe_value}">'
        f'<title>{safe_label}: {safe_value}</title>'
        f'<a xlink:href="{safe_url}" target="_blank">'
        f'<linearGradient id="s" x2="0" y2="100%">'
        f'<stop offset="0" stop-color="#bbbbbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/>'
        f'</linearGradient>'
        f'<clipPath id="r"><rect width="{total_width:.0f}" height="{BADGE_HEIGHT}" rx="3" fill="#ffffff"/></clipPath>'
        f'<g clip-path="url(#r)">'
        f'<rect width="{label_width:.0f}" height="{BADGE_HEIGHT}" fill="{LABEL_COLOR}"/>'
        f'<rect x="{label_width:.0f}" width="{value_width:.0f}" height="{BADGE_HEIGHT}" fill="{value_color}"/>'
        f'<rect width="{total_width:.0f}" height="{BADGE_HEIGHT}" fill="url(#s)"/>'
        f'</g>'
        f'<g fill="#ffffff" text-anchor="middle" '
        f'font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">'
        f'<text aria-hidden="true" x="{label_center * 10:.0f}" y="150" fill="#010101" fill-opacity=".3" '
        f'transform="scale(.1)" textLength="{(label_width - HORIZONTAL_PADDING) * 10:.0f}">{safe_label}</text>'
        f'<text x="{label_center * 10:.0f}" y="140" transform="scale(.1)" '
        f'textLength="{(label_width - HORIZONTAL_PADDING) * 10:.0f}">{safe_label}</text>'
        f'<text aria-hidden="true" x="{value_center * 10:.0f}" y="150" fill="#010101" fill-opacity=".3" '
        f'transform="scale(.1)" textLength="{(value_width - HORIZONTAL_PADDING) * 10:.0f}">{safe_value}</text>'
        f'<text x="{value_center * 10:.0f}" y="140" transform="scale(.1)" '
        f'textLength="{(value_width - HORIZONTAL_PADDING) * 10:.0f}">{safe_value}</text>'
        f'</g></a></svg>'
    )


def badge_markdown(badge_url: str, report_url: str) -> str:
    return f"[![authenticity human score]({badge_url})]({report_url})"


def badge_html(badge_url: str, report_url: str) -> str:
    return (
        f'<a href="{escape(report_url, quote=True)}">'
        f'<img src="{escape(badge_url, quote=True)}" alt="authenticity human score"/></a>'
    )
