"""Hospital bassinet: single-source articulated product model."""
import math
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Materials
white = v4.plastic("warm_hospital_white", (0.88, 0.90, 0.88), roughness=0.31)
blue = v4.plastic("medical_blue_trim", (0.19, 0.48, 0.60), roughness=0.28)
steel = v4.metal("brushed_stainless", (0.68, 0.72, 0.74), roughness=0.25)
chrome = v4.metal("column_chrome", (0.78, 0.80, 0.82), roughness=0.18, brushed=False)
rubber = v4.rubber("caster_tire", (0.055, 0.065, 0.067))
bumper_rubber = v4.rubber("cabinet_corner_rubber", (0.12, 0.15, 0.15))
dark = v4.mat("drawer_shadow", (0.12, 0.15, 0.15), roughness=0.65)
acrylic = v4.glass("clear_blue_acrylic", (0.80, 0.92, 0.96), roughness=0.08)
foam = v4.fabric("mattress_cover", (0.82, 0.89, 0.88))
pad_blue = v4.fabric("mattress_edge", (0.30, 0.57, 0.64))
ink = v4.mat("marking_ink", (0.08, 0.20, 0.24), roughness=0.45)

base = v4.body("chassis")

# Four splayed lower legs converge into the welded central frame. Wheel brackets
# mount at the leg tips. Cross-members keep the cart visually and structurally rigid.
wheel_xy = [(-0.255, -0.33), (0.255, -0.33), (-0.255, 0.33), (0.255, 0.33)]
for i, (x, y) in enumerate(wheel_xy):
    v4.rod(f"leg_{i}", (x * 0.60, y * 0.58, 0.43), (x, y, 0.19), 0.018,
           material=white, body=base)
    v4.cylinder(f"leg_socket_{i}", 0.022, 0.070, (x, y, 0.18), material=steel, body=base)
    # Gusset plate at each leg/cabinet mounting point.
    px, py = x*0.60, y*0.58
    v4.box(f"leg_mount_plate_{i}", (0.082, 0.080, 0.014), (px, py, 0.383), bevel=0.004,
           material=steel, body=base)
# Low H-frame tubes, just above the casters
v4.tube("welded_lower_frame", [(-0.192, -0.245, 0.31), (0.192, -0.245, 0.31), (0.192, 0.245, 0.31), (-0.192, 0.245, 0.31)], 0.013,
        material=steel, body=base, cyclic=True)
v4.tube("cart_bumper_ring", [(-0.255, -0.33, 0.205), (0.255, -0.33, 0.205),
                              (0.255, 0.33, 0.205), (-0.255, 0.33, 0.205)], 0.014,
        material=bumper_rubber, body=base, cyclic=True)

# Cabinet shell, with open front and one functional shallow drawer.
cab_w, cab_d = 0.44, 0.38
cab_z0, cab_z1 = 0.39, 0.68
cab_mid = (cab_z0 + cab_z1) / 2
v4.box("cabinet_left", (0.018, cab_d, cab_z1-cab_z0), (-cab_w/2+0.009, 0, cab_mid), bevel=0.006, material=white, body=base)
v4.box("cabinet_right", (0.018, cab_d, cab_z1-cab_z0), (cab_w/2-0.009, 0, cab_mid), bevel=0.006, material=white, body=base)
v4.box("cabinet_back", (cab_w-0.036, 0.018, cab_z1-cab_z0), (0, cab_d/2-0.009, cab_mid), bevel=0.004, material=white, body=base)
v4.box("cabinet_top", (cab_w, cab_d, 0.018), (0, 0, cab_z1-0.009), bevel=0.006, material=white, body=base)
v4.box("cabinet_bottom", (cab_w-0.036, cab_d-0.01, 0.018), (0, 0.005, cab_z0+0.009), bevel=0.003, material=white, body=base)
# Blue bumper edge along the cabinet's lower front edge
v4.box("cabinet_blue_kick", (cab_w-0.03, 0.012, 0.025), (0, -cab_d/2+0.006, cab_z0+0.025), bevel=0.004, material=blue, body=base)
# Four dark elastomer corner guards protect the cabinet at contact height.
for sx in (-1, 1):
    for sy in (-1, 1):
        v4.box(f"cabinet_corner_bumper_{sx}_{sy}", (0.025, 0.025, 0.17),
               (sx*(cab_w/2-0.008), sy*(cab_d/2-0.008), cab_mid), bevel=0.010,
               material=bumper_rubber, body=base)
# Drawer guide rails visible inside the opening
for sx in (-1, 1):
    v4.box(f"drawer_runner_{sx}", (0.024, 0.29, 0.014), (sx*0.190, -0.015, 0.525), bevel=0.002, material=steel, body=base)

drawer = v4.body("drawer")
drawer_z = 0.535
drawer_front_y = -cab_d/2+0.010
v4.box("drawer_front", (cab_w-0.004, 0.020, 0.246), (0, drawer_front_y, drawer_z), bevel=0.006, material=white, body=drawer)
drawer_y = -0.045
v4.box("drawer_floor", (0.355, 0.285, 0.008), (0, drawer_y, drawer_z-0.116), bevel=0.002, material=dark, body=drawer)
for sx in (-1, 1):
    v4.box(f"drawer_side_{sx}", (0.009, 0.285, 0.20), (sx*0.173, drawer_y, drawer_z-0.018), bevel=0.001, material=white, body=drawer)
v4.box("drawer_back", (0.355, 0.009, 0.20), (0, drawer_y+0.138, drawer_z-0.018), bevel=0.001, material=white, body=drawer)
# Recessed-look pull: dark inset with a stainless lip and central grip
v4.box("pull_recess", (0.115, 0.002, 0.025), (0, drawer_front_y-0.0105, drawer_z+0.005), bevel=0.004, material=dark, body=drawer)
v4.rod("pull_bar", (-0.038, drawer_front_y-0.0135, drawer_z+0.005), (0.038, drawer_front_y-0.0135, drawer_z+0.005), 0.004, material=steel, body=drawer)
v4.joint("drawer_slide", "prismatic", base, drawer, origin=(0, -0.20, drawer_z), axis=(0, -1, 0), limits=(0, 0.22))

# Fixed adjustable-height post with locking collar, scale and release knob.
v4.cylinder("column_outer_sleeve", 0.036, 0.28, (0, 0.055, 0.78), material=steel, body=base)
v4.cylinder("column_inner_tube", 0.026, 0.15, (0, 0.055, 0.85), material=chrome, body=base)
v4.cylinder("column_lock_collar", 0.041, 0.035, (0, 0.055, 0.64), material=white, body=base)
v4.cylinder("column_bushing", 0.006, 0.033, (0, 0.055, 0.9205), material=blue, body=base)
# Tilt cradle: a transverse lower shoe, diagonal supports, two upright ears and
# visible pivot bosses. These fixed pieces make the column-to-tub hinge legible.
v4.rod("cradle_cross_shoe", (-0.255, 0.055, 0.895), (0.255, 0.055, 0.895), 0.012, material=steel, body=base)
for sx in (-1, 1):
    v4.rod(f"cradle_diagonal_{sx}", (0, 0.055, 0.875), (sx*0.255, 0.055, 0.895), 0.010, material=steel, body=base)
    v4.rod(f"cradle_ear_{sx}", (sx*0.255, 0.055, 0.895), (sx*0.255, 0.055, 0.932), 0.008, material=steel, body=base)
    v4.box(f"cradle_cheek_plate_{sx}", (0.012, 0.104, 0.050), (sx*0.255, 0.055, 0.910),
           bevel=0.004, material=steel, body=base)
    v4.cylinder(f"cradle_pivot_boss_{sx}", 0.012, 0.018, (sx*0.255, 0.055, 0.94),
                rot=(0, math.pi/2, 0), material=chrome, body=base)
# Ribbed rubber bellows and collars make the telescoping column joint read clearly.
v4.lathe("column_bellows", [(0, -0.045), (0.039, -0.045), (0.044, -0.038), (0.038, -0.030),
                             (0.044, -0.022), (0.038, -0.014), (0.044, -0.006), (0.038, 0.002),
                             (0.044, 0.010), (0.038, 0.018), (0.044, 0.026), (0.038, 0.034),
                             (0.044, 0.041), (0.039, 0.045), (0, 0.045)],
         loc=(0, 0.055, 0.755), material=bumper_rubber, body=base)
v4.cylinder("bellows_upper_collar", 0.042, 0.012, (0, 0.055, 0.804), material=white, body=base)
v4.cylinder("bellows_lower_collar", 0.042, 0.012, (0, 0.055, 0.706), material=white, body=base)
# Side-facing height release lever and oversized blue hand knob.
v4.rod("height_release", (0.030, 0.055, 0.69), (0.105, 0.055, 0.715), 0.008, material=steel, body=base)
v4.sphere("height_release_button", 0.017, (0.112, 0.055, 0.717), material=blue, body=base)
for k in range(5):
    z = 0.79 + k*0.03
    v4.box(f"height_mark_{k}", (0.002, 0.006, 0.002), (0.026, 0.055, z), bevel=0, material=ink, body=base)

# Basket pivots around its transverse hinge line. A separate body contains the
# moulded clear tub, mattress and all tub-attached hardware.
basket = v4.body("basket")
bx, by0, by1 = 0.49, -0.34, 0.38
bz = 0.94
wall_h, wall_t = 0.205, 0.006
# Molded bottom has a little depth and radius. Four clear corner posts soften
# the upright corners while the straight panels terminate into them.
floor_t, corner_r = 0.018, 0.018
floor_top = bz + floor_t
side_len = by1-by0-2*corner_r
end_y0, end_y1 = by0+wall_t/2, by1-wall_t/2
v4.box("tub_floor", (bx, by1-by0, floor_t), (0, (by0+by1)/2, bz+floor_t/2), bevel=0.008, material=acrylic, body=basket)
v4.box("tub_side_left", (wall_t, side_len, wall_h), (-bx/2+wall_t/2, (by0+by1)/2, floor_top+wall_h/2), bevel=0.0025, material=acrylic, body=basket)
v4.box("tub_side_right", (wall_t, side_len, wall_h), (bx/2-wall_t/2, (by0+by1)/2, floor_top+wall_h/2), bevel=0.0025, material=acrylic, body=basket)
v4.box("tub_head_wall", (bx-2*corner_r, wall_t, wall_h), (0, end_y0, floor_top+wall_h/2), bevel=0.0025, material=acrylic, body=basket)
v4.box("tub_foot_wall", (bx-2*corner_r, wall_t, wall_h), (0, end_y1, floor_top+wall_h/2), bevel=0.0025, material=acrylic, body=basket)
for sx in (-1, 1):
    for sy in (-1, 1):
        v4.cylinder(f"tub_rounded_corner_{sx}_{sy}", corner_r, wall_h,
                    (sx*(bx/2-corner_r), sy*((by0+by1)/2+side_len/2), floor_top+wall_h/2),
                    material=acrylic, body=basket)
# Continuous padded rim represented by four softly bevelled segments.
rim_z = floor_top+wall_h
v4.box("rim_left", (0.014, by1-by0+0.012, 0.014), (-bx/2, (by0+by1)/2, rim_z), bevel=0.006, material=blue, body=basket)
v4.box("rim_right", (0.014, by1-by0+0.012, 0.014), (bx/2, (by0+by1)/2, rim_z), bevel=0.006, material=blue, body=basket)
v4.box("rim_head", (bx, 0.014, 0.014), (0, by0, rim_z), bevel=0.006, material=blue, body=basket)
v4.box("rim_foot", (bx, 0.014, 0.014), (0, by1, rim_z), bevel=0.006, material=blue, body=basket)
# Mattress with shallow quilt channels and a raised, cushioned perimeter bolster.
mattress_center_z = floor_top+0.026
v4.box("mattress_core", (0.435, 0.695, 0.052), (0, 0.02, mattress_center_z), bevel=0.025, material=foam, body=basket)
for sx in (-1, 1):
    v4.box(f"mattress_raised_side_{sx}", (0.032, 0.65, 0.028), (sx*0.201, 0.02, mattress_center_z+0.040),
           bevel=0.013, material=pad_blue, body=basket)
for sy in (-1, 1):
    v4.box(f"mattress_raised_end_{sy}", (0.37, 0.032, 0.028), (0, 0.02+sy*0.326, mattress_center_z+0.040),
           bevel=0.013, material=pad_blue, body=basket)
for i, y in enumerate((-0.22, -0.08, 0.06, 0.20, 0.30)):
    v4.box(f"mattress_quilt_{i}", (0.30, 0.003, 0.0015), (0, y, mattress_center_z+0.026+0.001), bevel=0.001, material=pad_blue, body=basket)
# Hinge cheeks and transverse pin are on the basket body and align with the chassis pivot.
for sx in (-1, 1):
    v4.box(f"tilt_hinge_leaf_{sx}", (0.048, 0.006, 0.032), (sx*0.19, 0.055, bz-0.012), bevel=0.003, material=steel, body=basket)
v4.cylinder("tilt_hinge_pin", 0.005, 0.42, (0, 0.055, bz), rot=(0, math.pi/2, 0), material=chrome, body=basket)
for sx in (-1, 1):
    v4.cylinder(f"basket_trunnion_{sx}", 0.010, 0.024, (sx*0.249, 0.055, bz),
                rot=(0, math.pi/2, 0), material=steel, body=basket)
    v4.cylinder(f"basket_trunnion_cap_{sx}", 0.013, 0.006, (sx*0.262, 0.055, bz),
                rot=(0, math.pi/2, 0), material=blue, body=basket)
# Card/name holder on the foot end, with simple printed rule marks.
v4.box("name_card_holder", (0.145, 0.010, 0.060), (0, by1+0.002, floor_top+0.112), bevel=0.006, material=steel, body=basket)
v4.box("name_card", (0.125, 0.004, 0.044), (0, by1+0.009, floor_top+0.112), bevel=0.003, material=white, body=basket)
for k, zoff in enumerate((-0.010, 0.0, 0.010)):
    v4.box(f"name_card_rule_{k}", (0.075 if k == 0 else 0.050, 0.002, 0.002),
           (0, by1+0.0105, floor_top+0.112+zoff), bevel=0, material=ink, body=basket)
v4.joint("basket_tilt", "revolute", base, basket, origin=(0, 0.055, bz), axis=(1, 0, 0),
         limits=(-math.radians(18), math.radians(18)))

# Four independently swivelling forks and rolling wheels. The 35 mm trailing
# offset gives the swivel kingpin and rolling axle distinct, realistic centres.
caster_trail = 0.040
for i, (x, y) in enumerate(wheel_xy):
    fork = v4.body(f"caster_fork_{i}")
    wheel = v4.body(f"caster_wheel_{i}")
    wy = y + caster_trail
    # Kingpin stays at the cart corner; the steel yoke sweeps back to the axle.
    v4.cylinder(f"fork_stem_{i}", 0.012, 0.075, (x, y, 0.1075), material=chrome, body=fork)
    v4.cylinder(f"fork_crown_{i}", 0.023, 0.014, (x, y, 0.150), material=white, body=fork)
    wc = (x, wy, 0.073)
    for sy in (-1, 1):
        side_y = wy + sy*0.024
        # Bored flat steel cheeks form an open U around the tyre.
        poly = [(-0.078, -sy*0.023), (0.078, -sy*0.023),
                (0.078, -sy*0.087), (-0.078, -sy*0.087)]
        plate = v4.extrude(f"fork_side_plate_{i}_{sy}", poly, 0.008,
                           loc=(x, side_y-sy*0.004, wc[2]), rot=(-sy*math.pi/2, 0, 0),
                           bevel=0.0015, material=steel, body=fork)
        cutter = v4.cylinder(f"fork_cutout_{i}_{sy}", 0.072, 0.03,
                             loc=wc, rot=(math.pi/2, 0, 0), material=None)
        v4.boolean(plate, cutter)
        v4.rod(f"fork_trailing_arm_{i}_{sy}", (x, y, 0.150), (x, side_y, 0.160),
               0.008, material=steel, body=fork)
    v4.rod(f"fork_tie_{i}", (x, wy-0.024, 0.160), (x, wy+0.024, 0.160),
           0.008, material=steel, body=fork)
    v4.joint(f"swivel_{i}", "revolute", base, fork, origin=(x, y, 0.14), axis=(0, 0, 1),
             limits=(-math.pi, math.pi))
    v4.cylinder(f"tire_{i}", 0.070, 0.040, wc, rot=(math.pi/2, 0, 0), material=rubber, body=wheel)
    for sy in (-1, 1):
        v4.cylinder(f"hub_{i}_{sy}", 0.036, 0.003, (x, wy+sy*0.0215, wc[2]), rot=(math.pi/2, 0, 0), material=white, body=wheel)
        v4.cylinder(f"hub_cap_{i}_{sy}", 0.014, 0.004, (x, wy+sy*0.024, wc[2]), rot=(math.pi/2, 0, 0), material=blue, body=wheel)
    v4.torus(f"tread_bead_{i}", 0.067, 0.003, wc, rot=(math.pi/2, 0, 0), material=dark, body=wheel)
    v4.joint(f"roll_{i}", "continuous", fork, wheel, origin=wc, axis=(0, 1, 0))
    # Broad rubber brake pedals sit above the front tire tread.
    if y < 0:
        v4.rod(f"brake_arm_{i}", (x, wy-0.024, 0.151), (x, wy-0.055, 0.159), 0.006, material=steel, body=fork)
        v4.box(f"brake_pedal_{i}", (0.052, 0.026, 0.014), (x, wy-0.055, 0.164), bevel=0.006, material=blue, body=fork)

v4.save(os.path.join(os.getcwd(), "asset.blend"))
print("saved asset.blend")
