
from __future__ import annotations

import sys
from pathlib import Path

import FreeCAD
import Part
from FreeCAD import Vector


def box(length, width, height, center):
    cx, cy, cz = center
    return Part.makeBox(
        float(length),
        float(width),
        float(height),
        Vector(cx - (length / 2.0), cy - (width / 2.0), cz - (height / 2.0)),
    )


def cylinder(diameter, length, center, axis="z"):
    cx, cy, cz = center
    radius = float(diameter) / 2.0
    if axis == "x":
        base = Vector(cx - (length / 2.0), cy, cz)
        direction = Vector(1, 0, 0)
    elif axis == "y":
        base = Vector(cx, cy - (length / 2.0), cz)
        direction = Vector(0, 1, 0)
    else:
        base = Vector(cx, cy, cz - (length / 2.0))
        direction = Vector(0, 0, 1)
    return Part.makeCylinder(radius, float(length), base, direction)


def fuse_all(shapes):
    items = [shape for shape in shapes if shape is not None]
    if not items:
        raise ValueError("No shapes were provided for fusion.")
    result = items[0]
    for other in items[1:]:
        result = result.fuse(other)
    return result


def make_compound(shapes):
    items = [shape for shape in shapes if shape is not None]
    if len(items) == 1:
        return items[0]
    return Part.makeCompound(items)


def cut_box(shape, length, width, height, center):
    return shape.cut(box(length, width, height, center))


def cut_hole(shape, diameter, length, center, axis="z"):
    return shape.cut(cylinder(diameter, length, center, axis=axis))


def add_box(shape, length, width, height, center):
    return shape.fuse(box(length, width, height, center))


def add_cylinder(shape, diameter, length, center, axis="z"):
    return shape.fuse(cylinder(diameter, length, center, axis=axis))


def export_shape(shape, output_path):
    output = Path(output_path).resolve()
    output.parent.mkdir(parents=True, exist_ok=True)
    refined = shape.removeSplitter() if hasattr(shape, "removeSplitter") else shape
    refined.exportStep(str(output))
    print(str(output))


DISPLAY_NAME = 'QuickChange locating adapter plate'
MATERIAL = 'Anodized Al 6082-T6'
REVISION = 'rev_b'
FALLBACK_MODE = False

def build_model():

    base = box(220.0, 140.0, 12.0, center=(0.0, 0.0, 6.0))
    for slot_x in (-70.0, 70.0):
        for slot_y in (-40.0, 40.0):
            base = cut_box(base, 30.0, 10.0, 18.0, center=(slot_x, slot_y, 6.0))
    for hole_x, hole_y in [(-62.0, 40.0), (62.0, 40.0)]:
        base = cut_hole(base, 8.0, 18.0, center=(hole_x, hole_y, 6.0), axis="z")
    base = cut_hole(base, 12.0, 18.0, center=(-92.0, 0.0, 6.0), axis="z")
    base = cut_hole(base, 12.0, 18.0, center=(92.0, 0.0, 6.0), axis="z")
    base = cut_hole(base, 16.0, 20.0, center=(-110.0, 0.0, 6.0), axis="y")
    base = cut_hole(base, 16.0, 20.0, center=(110.0, 0.0, 6.0), axis="y")
    base = cut_box(base, 52.0, 18.0, 1.2, center=(0.0, 55.0, 11.4))
    base = add_box(base, 18.0, 18.0, 1.0, center=(-58.0, 52.0, 12.5))
    base = add_box(base, 18.0, 18.0, 1.0, center=(58.0, 52.0, 12.5))
    shape = base
    return shape

def main():
    if len(sys.argv) != 2:
        raise SystemExit("Usage: freecad_script.py <output.step>")
    shape = build_model()
    export_shape(shape, sys.argv[1])

if __name__ == "__main__":
    main()
