"""
Set List: a slanted desk with a lit button for each of eight pieces, and the
bridge's stop button, activate button, light and antenna at the right-hand end.
The bridge's board lives inside, on a printed tray.

Run from the cad/ folder:

    uv run set-list/model.py

It writes, for the site:
    site/public/files/set-list/stl/*.stl        cheeks, panel tiles and tray, laid flat
    site/public/files/set-list/set-list-stl.zip
    site/public/files/set-list/set-list.step    the whole desk, for editing
    site/public/files/set-list/set-list.glb     the 3D model shown on the page
    site/public/files/set-list/set-list-cad.py  this script, and console.py beside it
    site/src/content/instruments/set-list/drawings/set-list-render.svg

Coordinates, in millimeters: X across the desk from the left cheek's inside
face, Y from its front edge to the back, Z up from the table. Positions on the
panel are u across and v up the slope from the panel's front edge.

All numbers are a first design, untested in print. Measure your buttons before
printing and change the hole sizes below to match.
"""

from pathlib import Path
import sys

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

# ---------------------------------------------------------------------------
# The design, in one place
# ---------------------------------------------------------------------------

DESK = c.Desk(w=380.0, d=170.0, front=50.0, back=95.0, tiles=[0.0, 182.0, 380.0])

SONGS = 8
BUTTON_HOLE = 28.0  # a 30 mm lit arcade button with a nut; snap-in ones want 30 mm
SONG_U = [30.0 + 34.0 * i for i in range(SONGS)]  # the seam between tiles falls between the fifth and sixth
SONG_V = 72.0
STOP = (330.0, 108.0)
RELEASE = (318.0, 45.0)
LIGHT = (348.0, 45.0)
ANTENNA = (350.0, 155.0)
STOP_D, RELEASE_D, LED_D, SMA_D = 22.4, 16.2, 8.0, 6.5

HOLES = [(u, SONG_V, BUTTON_HOLE) for u in SONG_U] + [
    (*STOP, STOP_D),
    (*RELEASE, RELEASE_D),
    (*LIGHT, LED_D),
    (*ANTENNA, SMA_D),
]

TRAY = (110.0, 270.0)  # across X; it spans the depth on the bottom rails
BOARDS = (190.0, 130.0)  # behind the buttons' switches, under the high back of the panel


def printed_parts():
    d = DESK
    tiles = [(f"panel-{i + 1}", d.tile(x0, x1, holes=HOLES), d.tile_flat()) for i, (x0, x1) in enumerate(zip(d.tiles, d.tiles[1:]))]
    return tiles + [
        ("cheek-left", d.cheek(False), c.Rot(0, 90, 0)),
        ("cheek-right", d.cheek(True), c.Rot(0, 90, 0)),
        ("tray", d.tray(*TRAY), c.Rot(0, 0, 0)),
    ]


def bought_parts():
    d = DESK
    parts = [(f"rail-{i + 1}", r, (0.78, 0.8, 0.83)) for i, r in enumerate(d.rails())]
    buttons = d.place(c.arcade_button(), SONG_U[0], SONG_V)
    for u in SONG_U[1:]:
        buttons += d.place(c.arcade_button(), u, SONG_V)
    parts += [
        ("song-buttons", buttons, (0.92, 0.9, 0.85)),
        ("stop-button", d.place(c.stop_button(), *STOP), (0.8, 0.15, 0.12)),
        ("release-button", d.place(c.push_button(), *RELEASE), (0.15, 0.15, 0.15)),
        ("status-light", d.place(c.light(), *LIGHT), (0.92, 0.92, 0.9)),
        ("antenna", d.place(c.antenna(), *ANTENNA), (0.12, 0.12, 0.12)),
        ("esp32-s3-board", c.Pos(BOARDS[0], BOARDS[1], 23) * c.boards(), (0.1, 0.25, 0.15)),
        ("expander-and-driver", c.box(112, 148, 115, 140, 23, 33) + c.box(232, 268, 115, 140, 23, 33), (0.1, 0.3, 0.5)),
        ("feet", c.Compound(children=d.feet()), (0.1, 0.1, 0.1)),
    ]
    return parts


def main():
    printed = printed_parts()
    bought = bought_parts()
    d = DESK
    c.export(
        "set-list",
        __file__,
        printed,
        bought,
        [(None, (d.w + 260, -420, 420), (d.w / 2, d.d / 2, 40), "THE DESK: EIGHT SONG BUTTONS, THEN ACTIVATE, THE LIGHT AND THE STOP")],
        "Set List: the desk",
    )


if __name__ == "__main__":
    main()
