"""Wrist depth gadget mount v1.1, parametric.

Axes: X runs along the forearm (+X towards the hand, where the sensor looks), Y across the wrist, Z up (away from skin).
Parts:
  - base plate with a curved underside that sits on the wrist
  - two strap slots for a 22 mm watch strap
  - an electronics tray on top for an ESP32-C3 SuperMini sized board (22.5 x 18 mm)
  - a sensor tower at the front holding a generic VL53L5CX breakout (~25 x 15 mm) upright, behind a window
    with a short hood, tilted 10 degrees down
  - a coin vibration motor pocket underneath, touching the skin, with a wire hole up into the tray
  - a cable groove from the tower to the tray
Run:  python wrist_mount.py  -> wrist_depth_mount_v1_1.stl, .3mf and preview PNGs
"""
import math
import numpy as np
import trimesh
from manifold3d import Manifold, Mesh

# ---- parameters (mm) ----
BASE_L, BASE_W, BASE_H = 56.0, 34.0, 7.0
WRIST_R = 40.0          # radius of the curved underside
UNDER_SAG = 2.0         # how much the centre of the underside is hollowed
STRAP_W, STRAP_T = 23.0, 3.2   # slot for a 22 mm strap, 3 mm thick
SLOT_X = (2.5, 52.5)    # slot start positions along X

BOARD_W, BOARD_H, BOARD_T = 25.6, 15.6, 4.0   # sensor board pocket (width, height, thickness incl. parts)
TOWER_X0, TOWER_L = 37.0, 10.0
TOWER_WALL = 2.0
WINDOW_W, WINDOW_H = 20.0, 10.0
HOOD_DEPTH = 5.0
TILT_DEG = 10.0

TRAY_IN_L, TRAY_IN_W, TRAY_DEPTH, TRAY_WALL = 24.0, 20.0, 5.0, 1.6
TRAY_X0 = 8.0

MOTOR_D, MOTOR_DEPTH, MOTOR_X = 10.6, 3.0, 20.0
WIRE_D = 2.6
GROOVE_W, GROOVE_D = 4.0, 1.6


def box(x0, y0, z0, lx, ly, lz):
    return Manifold.cube([lx, ly, lz]).translate([x0, y0, z0])


def cyl_z(cx, cy, z0, d, h, n=64):
    return Manifold.cylinder(h, d / 2, d / 2, n).translate([cx, cy, z0])


def build():
    y0 = -BASE_W / 2
    base = box(0, y0, 0, BASE_L, BASE_W, BASE_H)
    # curved underside: cylinder along X, top of the cylinder sits UNDER_SAG above z=0 at the centre line
    under = Manifold.cylinder(BASE_L + 2, WRIST_R, WRIST_R, 128).rotate([0, 90, 0]).translate([-1, 0, UNDER_SAG - WRIST_R])
    base = base - under

    # electronics tray (raised walls on top of the base)
    tray_out = box(TRAY_X0 - TRAY_WALL, -(TRAY_IN_W / 2 + TRAY_WALL), BASE_H,
                   TRAY_IN_L + 2 * TRAY_WALL, TRAY_IN_W + 2 * TRAY_WALL, TRAY_DEPTH)
    tray_in = box(TRAY_X0, -TRAY_IN_W / 2, BASE_H - 1.0, TRAY_IN_L, TRAY_IN_W, TRAY_DEPTH + 2)
    # usb-c opening at the back of the tray
    usb = box(TRAY_X0 - TRAY_WALL - 1, -5, BASE_H + 0.5, TRAY_WALL + 2, 10, 4)
    body = base + tray_out

    # sensor tower, built upright at the origin then tilted and placed
    tw = BOARD_W + 2 * TOWER_WALL
    th = BOARD_H + TOWER_WALL + 1.0
    tower = box(0, -tw / 2, 0, TOWER_L, tw, th)
    slot = box(TOWER_L - TOWER_WALL - BOARD_T, -BOARD_W / 2, TOWER_WALL, BOARD_T, BOARD_W, th)  # board drops in from the top
    window = box(TOWER_L - TOWER_WALL - 0.5, -WINDOW_W / 2, TOWER_WALL + (BOARD_H - WINDOW_H) / 2, TOWER_WALL + 1, WINDOW_W, WINDOW_H)
    hood_out = box(TOWER_L, -(WINDOW_W / 2 + 1.6), TOWER_WALL + (BOARD_H - WINDOW_H) / 2 - 1.6, HOOD_DEPTH, WINDOW_W + 3.2, WINDOW_H + 3.2)
    hood_in = box(TOWER_L - 0.5, -WINDOW_W / 2, TOWER_WALL + (BOARD_H - WINDOW_H) / 2, HOOD_DEPTH + 1, WINDOW_W, WINDOW_H)
    wire_back = box(-0.5, -4, TOWER_WALL + 1, TOWER_L - TOWER_WALL - BOARD_T + 1, 8, 4)  # cable exit at the back
    tower = (tower + hood_out) - slot - window - hood_in - wire_back
    # tilt: +10 deg about Y points +X face downward; pivot at the tower's front-bottom edge
    tower = tower.translate([-TOWER_L, 0, 0]).rotate([0, TILT_DEG, 0]).translate([TOWER_L, 0, 0])
    # rotation drops the back bottom edge below z=0; lift so the lowest point sits inside the base top
    lift = -tower.bounding_box()[2] if hasattr(tower, 'bounding_box') else 0
    tower = tower.translate([TOWER_X0, 0, BASE_H - 1.0 + max(lift, 0)])
    # fill under the tilted tower so it is solid down to the base
    fill = box(TOWER_X0, -tw / 2, BASE_H - 1.0, TOWER_L, tw, max(lift, 0) + 1.5)
    body = body + tower + fill

    # cuts that go through everything
    cuts = Manifold()
    for sx in SLOT_X:
        cuts = cuts + box(sx, -STRAP_W / 2, -5, STRAP_T, STRAP_W, 30)
    cuts = cuts + tray_in + usb
    cuts = cuts + cyl_z(MOTOR_X, 0, -5, MOTOR_D, 5 + UNDER_SAG + MOTOR_DEPTH)   # motor pocket from below
    cuts = cuts + cyl_z(MOTOR_X, 0, 0, WIRE_D, 20)                                # wire hole up into the tray
    cuts = cuts + box(TRAY_X0 + TRAY_IN_L - 1, -GROOVE_W / 2, BASE_H - GROOVE_D, TOWER_X0 - TRAY_X0 - TRAY_IN_L + 4, GROOVE_W, GROOVE_D + 3)
    return body - cuts


def to_trimesh(m):
    mesh = m.to_mesh()
    return trimesh.Trimesh(vertices=np.asarray(mesh.vert_properties)[:, :3], faces=np.asarray(mesh.tri_verts), process=True)


if __name__ == '__main__':
    import sys, os
    out = sys.argv[1] if len(sys.argv) > 1 else '.'
    tm = to_trimesh(build())
    tm.export(os.path.join(out, 'wrist_depth_mount_v1_1.stl'))
    tm.export(os.path.join(out, 'wrist_depth_mount_v1_1.3mf'))
    ext = tm.bounds[1] - tm.bounds[0]
    print('watertight', tm.is_watertight, 'volume_cm3', round(tm.volume / 1000, 2), 'size_mm', [round(v, 1) for v in ext], 'faces', len(tm.faces))
