"""Build a detailed, articulated portable cash box with v4lib."""
import math
import os
import sys

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

v4.reset()

# Finishes
blue = v4.painted("deep_blue_enamel", (0.055, 0.12, 0.24), roughness=0.72, coat=0.06)
blue_light = v4.painted("raised_blue_enamel", (0.075, 0.15, 0.29), roughness=0.68, coat=0.07)
inside = v4.painted("charcoal_interior", (0.105, 0.12, 0.14), roughness=0.58, coat=0.12)
nickel = v4.metal("satin_nickel", (0.72, 0.74, 0.75), roughness=0.28, brushed=True)
steel = v4.metal("darkened_steel", (0.28, 0.31, 0.34), roughness=0.38, brushed=False)
brass = v4.metal("warm_brass", (0.78, 0.53, 0.22), roughness=0.26, brushed=True)
black = v4.mat("keyway_black", (0.018, 0.021, 0.025), roughness=0.38)
rubber = v4.rubber("foot_rubber", (0.045, 0.05, 0.055))
liner = v4.fabric("tray_liner", (0.16, 0.17, 0.17))
white = v4.mat("warm_lettering", (0.86, 0.82, 0.69), roughness=0.42)
copper = v4.metal("coin_copper", (0.72, 0.34, 0.15), roughness=0.29)
gold = v4.metal("coin_brass", (0.82, 0.64, 0.28), roughness=0.27)

# Dimensions: 300 × 220 × 85 mm body, lid closed height 93 mm.
W, D = 0.300, 0.220
wall = 0.0015
floor_z = 0.009
rim_z = 0.084

base = v4.body("base")
lid = v4.body("lid")
key = v4.body("key_barrel")
tray = v4.body("coin_tray")
handle = v4.body("carry_handle")

# Pressed shell: plates meet at the corners and form one rigid shell.
v4.box("base_floor", (W - 2 * wall, D - 2 * wall, 0.002), (0, 0, floor_z), bevel=0.0005,
       material=inside, body=base)
side_h = rim_z - 0.008
side_cz = (rim_z + 0.008) / 2
for sx, label in [(-1, "left"), (1, "right")]:
    v4.box(f"shell_side_{label}", (wall, D, side_h), (sx * (W / 2 - wall / 2), 0, side_cz),
           bevel=0.002, material=blue, body=base)
for sy, label in [(-1, "front"), (1, "rear")]:
    v4.box(f"shell_{label}", (W - 2 * wall, wall, side_h), (0, sy * (D / 2 - wall / 2), side_cz),
           bevel=0.0015, material=blue, body=base)

# Fine pressed beads break up the broad side panels and trace the lower return.
for sx, label in [(-1, "L"), (1, "R")]:
    for k, z in enumerate((0.026, 0.066)):
        v4.box(f"side_bead_{label}_{k}", (0.0015, 0.182, 0.0022),
               (sx * 0.1505, 0, z), bevel=0.001, material=blue_light, body=base)
for sy, label in [(-1, "front"), (1, "back")]:
    v4.box(f"wall_crease_{label}", (0.272, 0.0015, 0.0022), (0, sy * 0.1105, 0.026),
           bevel=0.001, material=blue_light, body=base)

# Folded inner rim rails and two tray support ledges.
for sx, label in [(-1, "L"), (1, "R")]:
    v4.box(f"tray_ledge_{label}", (0.012, 0.154, 0.005), (sx * 0.1425, 0, 0.0295),
           bevel=0.0012, material=steel, body=base)
    # Tall shallow guides keep the lift-out tray centred while it rises.
    v4.box(f"tray_guide_{label}", (0.004, 0.158, 0.060), (sx * 0.1465, 0, 0.053),
           bevel=0.001, material=nickel, body=base)
    v4.box(f"rolled_rim_{label}", (0.004, D - 0.028, 0.004), (sx * 0.1485, 0, 0.0815),
           bevel=0.0015, material=blue_light, body=base)
for sy, label in [(-1, "front"), (1, "back")]:
    v4.box(f"rolled_rim_{label}", (W - 0.032, 0.004, 0.004), (0, sy * 0.108, 0.0815),
           bevel=0.0015, material=blue_light, body=base)

# Four low rubber feet, each with a thin nickel fixing washer.
for ix, sx in enumerate((-1, 1)):
    for iy, sy in enumerate((-1, 1)):
        x, y = sx * 0.138, sy * 0.098
        v4.cylinder(f"rubber_foot_{ix}_{iy}", 0.014, 0.010, (x, y, 0.005),
                    material=rubber, body=base)

# Hinge leaves and three knuckles on each side of the centre pin.
hinge_y, hinge_z = 0.106, 0.084
for sx, label in [(-1, "L"), (1, "R")]:
    x = sx * 0.092
    v4.box(f"hinge_leaf_base_{label}", (0.055, 0.002, 0.020), (x, 0.102, 0.073),
           bevel=0.001, material=nickel, body=base)
    v4.cylinder(f"hinge_barrel_base_{label}", 0.004, 0.064, (x, hinge_y, hinge_z),
                rot=(0, math.pi / 2, 0), material=nickel, body=base)
    v4.box(f"hinge_leaf_lid_{label}", (0.055, 0.002, 0.002), (x, 0.097, 0.083),
           bevel=0.001, material=nickel, body=lid)
    v4.cylinder(f"hinge_barrel_lid_{label}", 0.004, 0.022, (x, hinge_y, hinge_z),
                rot=(0, math.pi / 2, 0), material=brass, body=lid)
    # Visible leaf screws are mounted to their respective leaves.
    for j, dz in enumerate((-0.0004, 0.0004)):
        v4.cylinder(f"hinge_screw_base_{label}_{j}", 0.0023, 0.0015,
                    (x, 0.1005, 0.073 + dz), rot=(math.pi / 2, 0, 0),
                    material=brass, body=base)
        v4.cylinder(f"hinge_screw_lid_{label}_{j}", 0.0023, 0.0015,
                    (x, 0.096, 0.083 + dz), rot=(math.pi / 2, 0, 0),
                    material=brass, body=lid)

# Lid skin and rolled lip. Closed lid rests just above the body rim.
v4.box("lid_outer_panel", (W + 0.008, D + 0.008, 0.002), (0, 0, 0.085),
       bevel=0.0025, material=blue, body=lid)
# Narrow front and side skirts create the characteristic overhanging lid seam.
for sx, label in [(-1, "L"), (1, "R")]:
    v4.box(f"lid_skirt_side_{label}", (0.002, D - 0.020, 0.006),
           (sx * 0.1515, 0, 0.081), bevel=0.0015, material=blue, body=lid)
v4.box("lid_skirt_front", (W - 0.006, 0.002, 0.006), (0, -0.1115, 0.081),
       bevel=0.0015, material=blue, body=lid)
v4.box("lid_stamped_panel", (0.238, 0.155, 0.0012), (0, -0.004, 0.0866),
       bevel=0.007, material=blue_light, body=lid)
for x in (-0.115, 0.115):
    v4.box(f"lid_reinforcement_side_{x}", (0.004, 0.176, 0.004), (x, 0, 0.082),
           bevel=0.001, material=steel, body=lid)
for y in (-0.084, 0.084):
    v4.box(f"lid_reinforcement_end_{y}", (0.226, 0.004, 0.004), (0, y, 0.082),
           bevel=0.001, material=steel, body=lid)
v4.box("maker_badge", (0.072, 0.019, 0.0012), (0, 0.052, 0.0875),
       bevel=0.002, material=brass, body=lid)
for sx in (-1, 1):
    for sy in (-1, 1):
        v4.cylinder(f"lid_rivet_{sx}_{sy}", 0.0022, 0.0012,
                    (sx * 0.126, sy * 0.090, 0.0874), material=nickel, body=lid)

# Folding bail handle, stored flat on the lid with its two hinge brackets visible.
# The U-shaped wire rotates about one continuous pin line along the lid's X axis.
handle_y, handle_z = 0.018, 0.094
for sx, label in [(-1, "L"), (1, "R")]:
    v4.box(f"handle_bracket_foot_{label}", (0.024, 0.022, 0.004),
           (sx * 0.070, handle_y, 0.0895), bevel=0.002, material=steel, body=lid)
    v4.box(f"handle_bracket_ear_{label}", (0.010, 0.006, 0.010),
           (sx * 0.070, handle_y, 0.096), bevel=0.002, material=nickel, body=lid)
    v4.cylinder(f"handle_pivot_pin_{label}", 0.0045, 0.014,
                (sx * 0.070, handle_y, handle_z), rot=(0, math.pi / 2, 0),
                material=brass, body=handle)
v4.tube("bail_wire", [(-0.070, handle_y, handle_z), (-0.070, -0.060, handle_z),
                       (0.070, -0.060, handle_z), (0.070, handle_y, handle_z)],
        radius=0.0035, material=nickel, body=handle, cyclic=False)
v4.cylinder("bail_grip", 0.006, 0.076, (0, -0.060, handle_z),
            rot=(0, math.pi / 2, 0), material=rubber, body=handle)
for sx in (-1, 1):
    v4.cylinder(f"handle_bracket_screw_{sx}", 0.0018, 0.001,
                (sx * 0.078, handle_y, 0.092), material=brass, body=lid)

# Front lock housing fixed to shell; escutcheon and bezel around the rotating barrel.
for nm, size, loc in [
        ("lock_frame_top", (0.064, 0.010, 0.014), (0, -0.113, 0.073)),
        ("lock_frame_bottom", (0.064, 0.010, 0.014), (0, -0.113, 0.031)),
        ("lock_frame_left", (0.012, 0.010, 0.028), (-0.026, -0.113, 0.052)),
        ("lock_frame_right", (0.012, 0.010, 0.028), (0.026, -0.113, 0.052))]:
    v4.box(nm, size, loc, bevel=0.003, material=blue_light, body=base)
escutcheon = v4.cylinder("lock_escutcheon", 0.019, 0.004, (0, -0.1195, 0.055),
                         rot=(math.pi / 2, 0, 0), material=brass, body=base)
cutter = v4.cylinder("escutcheon_bore_cutter", 0.0108, 0.010, (0, -0.1195, 0.055),
                     rot=(math.pi / 2, 0, 0), bevel=0, material=None)
v4.boolean(escutcheon, cutter)
for sx in (-1, 1):
    v4.cylinder(f"lock_mount_screw_{sx}", 0.0023, 0.0014,
                (sx * 0.023, -0.119, 0.055), rot=(math.pi / 2, 0, 0),
                material=brass, body=base)

# Barrel axis runs through the lock face, along +Y. Key slot and short key turn with it.
v4.cylinder("key_barrel_body", 0.0105, 0.013, (0, -0.1245, 0.055),
            rot=(math.pi / 2, 0, 0), material=steel, body=key)
v4.cylinder("key_barrel_face", 0.0085, 0.0015, (0, -0.132, 0.055),
            rot=(math.pi / 2, 0, 0), material=brass, body=key)
v4.box("key_slot", (0.0022, 0.001, 0.008), (0, -0.133, 0.055),
       bevel=0.0007, material=black, body=key)
v4.box("key_stem", (0.003, 0.0018, 0.014), (0, -0.135, 0.044),
       bevel=0.0007, material=brass, body=key)
v4.box("key_bow", (0.014, 0.002, 0.010), (0, -0.136, 0.034),
       bevel=0.004, material=brass, body=key)
v4.cylinder("key_bow_hole", 0.0025, 0.001, (0, -0.1373, 0.034),
            rot=(math.pi / 2, 0, 0), material=black, body=key)

# Lift-out coin tray, resting on paired ledges and guided by side channels.
tray_w, tray_d = 0.264, 0.184
tray_bottom = 0.032
tray_h = 0.045
tray_center_z = tray_bottom + 0.002
v4.box("tray_floor", (tray_w, tray_d, 0.004), (0, 0, tray_center_z),
       bevel=0.001, material=steel, body=tray)
v4.box("tray_liner_panel", (tray_w - 0.006, tray_d - 0.006, 0.001),
       (0, 0, tray_bottom + 0.0045), material=liner, body=tray)
for sx, label in [(-1, "L"), (1, "R")]:
    x = sx * (tray_w / 2 - 0.002)
    v4.box(f"tray_wall_{label}", (0.004, tray_d, tray_h), (x, 0, tray_bottom + tray_h / 2),
           bevel=0.001, material=steel, body=tray)
    # Outer return channel loosely fits over the upright runner at the home position.
    v4.box(f"tray_runner_cap_{label}", (0.006, tray_d - 0.020, 0.004),
           (sx * 0.136, 0, tray_bottom + 0.010), bevel=0.001, material=nickel, body=tray)
for sy, label in [(-1, "front"), (1, "rear")]:
    v4.box(f"tray_wall_{label}", (tray_w, 0.004, tray_h), (0, sy * (tray_d / 2 - 0.002), tray_bottom + tray_h / 2),
           bevel=0.001, material=steel, body=tray)

# Four transverse dividers and one centre divider make ten useful coin pockets.
inner_w = tray_w - 0.012
cell_w = inner_w / 5
for k in range(1, 5):
    x = -inner_w / 2 + k * cell_w
    v4.box(f"tray_divider_x_{k}", (0.002, 0.120, 0.026),
           (x, -0.022, tray_bottom + 0.017), bevel=0.0008, material=steel, body=tray)
v4.box("coin_note_divider", (tray_w - 0.010, 0.002, 0.026),
       (0, 0.039, tray_bottom + 0.017), bevel=0.0008, material=steel, body=tray)

# Shallow turned coin cups sit in the five front pockets and meet the soft liner.
for k in range(5):
    x = -inner_w / 2 + (k + 0.5) * cell_w
    v4.lathe(f"coin_cup_{k}", [(0, 0), (0.014, 0), (0.014, 0.002),
                                (0.012, 0.003), (0.011, 0.001), (0, 0.001)],
             loc=(x, -0.055, tray_bottom + 0.005), material=nickel, body=tray)

# Rear banknote section with a spring leaf, two raised supports and a base plate.
v4.box("note_clip_base", (0.150, 0.040, 0.0015), (0, 0.064, tray_bottom + 0.0055),
       bevel=0.001, material=nickel, body=tray)
for sx in (-1, 1):
    v4.rod(f"note_clip_spring_arm_{sx}", (sx * 0.043, 0.082, tray_bottom + 0.006),
           (sx * 0.043, 0.052, tray_bottom + 0.018), 0.0015,
           material=brass, body=tray)
v4.box("note_clip_leaf", (0.102, 0.026, 0.002),
       (0, 0.064, tray_bottom + 0.019), rot=(math.radians(-8), 0, 0),
       bevel=0.001, material=steel, body=tray)

# Printed denomination labels on the front row's floor.
# A few realistic coins sit below the divider tops and move with the tray.
coin_specs = [(-0.105, -0.060, 0.007, copper), (-0.052, -0.061, 0.009, nickel),
              (0.000, -0.060, 0.008, gold), (0.052, -0.060, 0.010, nickel),
              (0.105, -0.060, 0.008, copper), (-0.104, 0.050, 0.008, gold),
              (-0.052, 0.050, 0.007, copper), (0.000, 0.050, 0.009, nickel),
              (0.052, 0.050, 0.008, gold), (0.104, 0.050, 0.007, copper)]
for i, (x, y, r, mat) in enumerate(coin_specs):
    for stack in range(2 if i in (1, 3, 7) else 1):
        z = tray_bottom + 0.006 + stack * 0.0017
        v4.cylinder(f"coin_{i}_{stack}", r, 0.0015, (x, y, z),
                    segments=32, material=mat, body=tray)
        if stack == 0:
            v4.cylinder(f"coin_stamp_{i}", r * 0.34, 0.0006, (x, y, z + 0.00105),
                        segments=24, material=brass if i % 2 else nickel, body=tray)

# The tray's two side runners sit on the shell ledges at rest.
v4.box("tray_runner_L", (0.010, 0.150, 0.004), (-0.132, 0, tray_bottom),
       bevel=0.0008, material=nickel, body=tray)
v4.box("tray_runner_R", (0.010, 0.150, 0.004), (0.132, 0, tray_bottom),
       bevel=0.0008, material=nickel, body=tray)

# Articulation. Axis sign is chosen so positive lid motion lifts its front edge.
v4.joint("lid_hinge", "revolute", base, lid, origin=(0, hinge_y, hinge_z),
         axis=(-1, 0, 0), limits=(0, math.radians(106)))
v4.joint("carry_handle_fold", "revolute", lid, handle,
         origin=(0, handle_y, handle_z), axis=(-1, 0, 0), limits=(0, 1.35))
v4.joint("key_turn", "continuous", base, key, origin=(0, -0.118, 0.055), axis=(0, 1, 0))
v4.joint("tray_lift", "prismatic", base, tray, origin=(0, 0, tray_bottom), axis=(0, 0, 1),
         limits=(0, 0.105))

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