"""
The Stop Bar: the Tapper moved onto a longer bar that stands on two feet,
with a stop button in a yellow box at one end and the electronics in a small
project box. The board, the relay and the fuse are inside the project box, so
only the box shows, with the test button on its lid.

Run from the cad/ folder:

    uv run stop-bar/model.py

It writes, for the site:
    site/public/files/stop-bar/stop-bar.step    the whole assembly, for editing
    site/public/files/stop-bar/stop-bar.glb     the 3D model shown on the page
    site/public/files/stop-bar/stop-bar-cad.py  this script, and console.py beside it
    site/src/content/instruments/stop-bar/drawings/stop-bar-render.svg

It builds the Tapper with the Tapper's own script (cad/tapper/model.py). Every
part is bought, so there is nothing to print. The parts are drawn only well
enough to show where they go. Coordinates in millimeters: X along the bar, Y
across it, Z up from the bench.
"""

from pathlib import Path
import importlib.util
import sys

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parent))
import console as c  # noqa: E402

spec = importlib.util.spec_from_file_location("tapper", HERE.parent / "tapper" / "model.py")
tapper = importlib.util.module_from_spec(spec)
spec.loader.exec_module(tapper)

BAR_L = 500.0
FEET = (30.0, 470.0)  # the middle of each foot, along the bar
FOOT_L = 120.0  # each foot, across the bar
TOP = 40.0  # the bar's top: it stands 20 mm up on the feet
TAPPER_X = 300.0  # the upright's left face
BOX = (120.0, 220.0, -20.0, 40.0, 35.0)  # the project box: x from, x to, y from, y to, height
STOP = (410.0, 480.0, -25.0, 45.0, 60.0)  # the stop button's box, the same way

YELLOW = (0.9, 0.75, 0.15)
RED = (0.8, 0.15, 0.12)


def lidded(x0, x1, y0, y1, z0, h):
    """A box with its lid a little proud all round, so the lid line shows."""
    return c.box(x0, x1, y0, y1, z0, z0 + h - 4) + c.box(x0 - 1, x1 + 1, y0 - 1, y1 + 1, z0 + h - 4, z0 + h)


def main():
    bought = [("bar", c.vslot(0, BAR_L, 0, 20, 20, TOP), tapper.ALUMINUM)]
    for name, fx in zip(("foot-left", "foot-right"), FEET):
        bought.append((name, c.vslot(fx - 10, fx + 10, 10 - FOOT_L / 2, 10 + FOOT_L / 2, 0, 20), tapper.ALUMINUM))
    brackets = [tapper.corner(fx, y, 20, turn) for fx in FEET for y, turn in ((0, -90), (20, 90))]
    bought += [(f"feet-bracket-{i}", b, tapper.CAST) for i, b in enumerate(brackets, start=1)]
    bought += tapper.tapper(TAPPER_X, TOP)

    x0, x1, y0, y1, h = BOX
    bought.append(("electronics-box", lidded(x0, x1, y0, y1, TOP, h), (0.25, 0.26, 0.28)))
    bought.append(("test-button", c.Pos(x1 - 25, (y0 + y1) / 2, TOP + h) * c.push_button(), tapper.BLACK))
    x0, x1, y0, y1, h = STOP
    bought.append(("stop-box", lidded(x0, x1, y0, y1, TOP, h), YELLOW))
    bought.append(("stop-button", c.Pos((x0 + x1) / 2, (y0 + y1) / 2, TOP + h) * c.stop_button(), RED))

    look = (BAR_L / 2, 10, 70)
    eye = tuple(a + 1000 * d for a, d in zip(look, tapper.VIEW))
    caption = "THE TAPPER ON THE BAR, THE ELECTRONICS IN THEIR BOX AND THE STOP BUTTON AT THE END"
    c.export("stop-bar", __file__, [], bought, [(None, eye, look, caption)], "The Stop Bar")
    out = c.REPO / "site/public/files/stop-bar"  # nothing is printed, so no STLs to offer
    (out / "stop-bar-stl.zip").unlink()
    (out / "stl").rmdir()


if __name__ == "__main__":
    main()
