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


def create_nonius_1_20_svg(output_filename="winkelmesser_1_20_deg.svg"):
    # Zeichenbereich (mm)
    view_w, view_h = 380.0, 240.0
    cx_main, cy_main = 110.0, 140.0
    cx_nonius, cy_nonius = 110.0, 140.0

    # Hauptskala Radien
    r_main_outer = 70.0
    r_main_t10 = 65.0  # 10°-Striche
    r_main_t5 = 65.0  # 5°-Striche
    r_main_t1 = 65.0  # 1°-Striche
    r_main_text = 50.0

    # Nonius Radien
    r_nonius_outer = 65.0
    r_nonius_t5 = 60.0  # Hauptstriche (0, 5, 10, 15, 20)
    r_nonius_t1 = 62.5  # Zwischenstriche

    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 = ET.SubElement(svg, "style")
    style.text = """
        .cut-line { stroke: #000000; stroke-width: 0.3mm; fill: none; }
        .tick-major { stroke: #000000; stroke-width: 0.1mm; stroke-linecap: round; }
        .tick-minor { stroke: #000000; stroke-width: 0.1mm; stroke-linecap: round; }
        .scale-text { font-family: 'Helvetica Neue', Arial, sans-serif; font-size: 2.0mm; text-anchor: middle; dominant-baseline: central; fill: #000000; }
    """

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

    ET.SubElement(
        g_main,
        "circle",
        {"cx": str(cx_main), "cy": str(cy_main), "r": "3.0", "class": "cut-line"},
    )

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

        if deg % 10 == 0:
            r_inner = r_main_t10
            line_class = "tick-major"
        elif deg % 5 == 0:
            r_inner = r_main_t5
            line_class = "tick-minor"
        else:
            r_inner = r_main_t1
            line_class = "tick-minor"

        x1 = cx_main + r_main_outer * math.cos(rad)
        y1 = cy_main - r_main_outer * math.sin(rad)
        x2 = cx_main + r_inner * math.cos(rad)
        y2 = cy_main - r_inner * 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": line_class,
            },
        )

        # Beschriftung nur alle 10°
        if deg % 10 == 0:
            xt = cx_main + r_main_text * math.cos(rad)
            yt = cy_main - r_main_text * math.sin(rad)

            rot = -deg
            if 90 < deg < 270:
                rot += 180

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

    # --- 2. NONIUS-SKALA (19°-Bogen, 20 Schritte à 0,95°) ---
    g_nonius = ET.SubElement(svg, "g", {"id": "Nonius"})

    ET.SubElement(
        g_nonius,
        "circle",
        {
            "cx": str(cx_nonius),
            "cy": str(cy_nonius),
            "r": "3.0",
            "class": "cut-line",
        },
    )

    for i in range(21):
        deg = i * 0.95  # 0° bis 19°
        rad = math.radians(deg+45.5)

        r_inner = r_nonius_t5 if (i % 5 == 0) else r_nonius_t1
        line_class = "tick-major" if (i % 5 == 0) else "tick-minor"

        x1 = cx_nonius + r_nonius_outer * math.cos(rad)
        y1 = cy_nonius - r_nonius_outer * math.sin(rad)
        x2 = cx_nonius + r_inner * math.cos(rad)
        y2 = cy_nonius - r_inner * 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": line_class,
            },
        )

    tree = ET.ElementTree(svg)
    ET.indent(tree, space="  ", level=0)
    tree.write(output_filename, encoding="utf-8", xml_declaration=True)
    print(f"Datei erfolgreich generiert: {output_filename}")


if __name__ == "__main__":
    create_nonius_1_20_svg()
