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

def create_scale_svg(output_filename="winkelmesser_1deg_mane.svg"):
    view_w, view_h = 200.0, 200.0
    cx_main, cy_main = 100.0, 100.0
    cx_nonius, cy_nonius = 100.0, 100.0

    r_main_outer = 70.0
    r_main_ticks = 60.0

    r_nonius_outer = 60.0
    r_nonius_ticks = 50.0

    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-line { stroke: #000000; stroke-width: 0.25mm; stroke-linecap: round; }
    """

    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"},
    )
    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)

        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",
            }
        )
        
    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",
        }
    )
    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  
        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",
            }
        )

    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()
