#!/usr/bin/python3
import math
import xml.etree.ElementTree as ET


def create_scale_svg(output_filename="winkelmesser_1deg_fixed.svg"):
    # Arbeitsbereich & Zentren (in mm)
    view_w, view_h = 360.0, 230.0
    cx_main, cy_main = 100.0, 130.0
    cx_nonius, cy_nonius = 260.0, 130.0

    # Radien Hauptskala
    r_main_outer = 70.0
    r_main_ticks = 60.0
    r_main_text = 52.0  # Zahlen sauber im Innenkreis platziert

    # Radien Nonius
    r_nonius_outer = 59.5  # 0.5 mm Luftspalt zur Hauptskala
    r_nonius_ticks = 49.5
    r_nonius_text = 42.0

    # SVG Root Element
    svg = ET.Element(
        "svg",
        {
            "xmlns": "http://www.w3.org/2000/svg",
            "width": f"{view_w}mm",
            "height": f"{view_h}mm",
            "viewBox": f"0 0 {view_w} {view_h}",
            "style": "background-color: #ffffff;",
        },
    )

    # Style-Header mit angepassten Schriftgrößen & Ausrichtungen
    style = ET.SubElement(svg, "style")
    style.text = """
        .cut-line { stroke: #000000; stroke-width: 0.3mm; fill: none; }
        .tick-line { stroke: #000000; stroke-width: 0.25mm; stroke-linecap: round; }
        .scale-text { font-family: Arial, sans-serif; font-size: 3.0mm; text-anchor: middle; dominant-baseline: central; fill: #000000; }
        .label-title { font-family: Arial, sans-serif; font-size: 4.5mm; font-weight: bold; text-anchor: middle; fill: #000000; }
    """

    # --- 1. HAUPTSKALA (0° bis 360° in 10°-Schritten) ---
    g_main = ET.SubElement(svg, "g", {"id": "Hauptskala"})

    # Titel oberhalb der Skala
    ET.SubElement(
        g_main,
        "text",
        {"x": str(cx_main), "y": "22.0", "class": "label-title"},
    ).text = "Hauptskala (10°-Teilung)"

    # Zentrierbohrung & Außenkreis
    ET.SubElement(
        g_main,
        "circle",
        {"cx": str(cx_main), "cy": str(cy_main), "r": "3.0", "class": "cut-line"},
    )
    ET.SubElement(
        g_main,
        "circle",
        {
            "cx": str(cx_main),
            "cy": str(cy_main),
            "r": str(r_main_outer),
            "class": "cut-line",
        },
    )

    for deg in range(0, 360, 10):
        rad = math.radians(deg)

        # Teilstriche
        x1 = cx_main + r_main_outer * math.cos(rad)
        y1 = cy_main - r_main_outer * math.sin(rad)
        x2 = cx_main + r_main_ticks * math.cos(rad)
        y2 = cy_main - r_main_ticks * math.sin(rad)

        ET.SubElement(
            g_main,
            "line",
            {
                "x1": f"{x1:.3f}",
                "y1": f"{y1:.3f}",
                "x2": f"{x2:.3f}",
                "y2": f"{y2:.3f}",
                "class": "tick-line",
            },
        )

        # Zahlenposition
        xt = cx_main + r_main_text * math.cos(rad)
        yt = cy_main - r_main_text * math.sin(rad)

        # Radiale Text-Rotation
        rot = -deg
        # 180°-Drehung im linken Halbkreis für durchgehend aufrechte Lesbarkeit
        if 90 < deg < 270:
            rot += 180

        ET.SubElement(
            g_main,
            "text",
            {
                "x": f"{xt:.3f}",
                "y": f"{yt:.3f}",
                "class": "scale-text",
                "transform": f"rotate({rot:.1f}, {xt:.3f}, {yt:.3f})",
            },
        ).text = str(deg)

    # --- 2. NONIUS-SKALA (90°-Bogen, 10 Abschnitte à 9°) ---
    g_nonius = ET.SubElement(svg, "g", {"id": "Nonius"})

    # Titel oberhalb der Skala
    ET.SubElement(
        g_nonius,
        "text",
        {"x": str(cx_nonius), "y": "22.0", "class": "label-title"},
    ).text = "Nonius (1°-Auflösung)"

    # Zentrierbohrung & Außenkreis
    ET.SubElement(
        g_nonius,
        "circle",
        {
            "cx": str(cx_nonius),
            "cy": str(cy_nonius),
            "r": "3.0",
            "class": "cut-line",
        },
    )
    ET.SubElement(
        g_nonius,
        "circle",
        {
            "cx": str(cx_nonius),
            "cy": str(cy_nonius),
            "r": str(r_nonius_outer),
            "class": "cut-line",
        },
    )

    for i in range(11):
        deg = i * 9.0  # 0°, 9°, 18°, ..., 90°
        rad = math.radians(deg)

        x1 = cx_nonius + r_nonius_outer * math.cos(rad)
        y1 = cy_nonius - r_nonius_outer * math.sin(rad)
        x2 = cx_nonius + r_nonius_ticks * math.cos(rad)
        y2 = cy_nonius - r_nonius_ticks * math.sin(rad)

        ET.SubElement(
            g_nonius,
            "line",
            {
                "x1": f"{x1:.3f}",
                "y1": f"{y1:.3f}",
                "x2": f"{x2:.3f}",
                "y2": f"{y2:.3f}",
                "class": "tick-line",
            },
        )

        xt = cx_nonius + r_nonius_text * math.cos(rad)
        yt = cy_nonius - r_nonius_text * math.sin(rad)

        rot = -deg

        ET.SubElement(
            g_nonius,
            "text",
            {
                "x": f"{xt:.3f}",
                "y": f"{yt:.3f}",
                "class": "scale-text",
                "transform": f"rotate({rot:.1f}, {xt:.3f}, {yt:.3f})",
            },
        ).text = str(i)

    # XML-Datei schreiben
    tree = ET.ElementTree(svg)
    ET.indent(tree, space="  ", level=0)
    tree.write(output_filename, encoding="utf-8", xml_declaration=True)
    print(f"Korrekturen angewendet. SVG gespeichert als: {output_filename}")


if __name__ == "__main__":
    create_scale_svg()

