"""Detailed, articulated wooden spring snap trap. Run with Blender in background."""
import math
import os
import sys

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

v4.reset()

# Materials: a sealed pine board, galvanized mounting hardware, and dark tempered wire.
pine = v4.wood("sealed_honey_pine", light=(0.60, 0.39, 0.23), dark=(0.32, 0.19, 0.105), grain_scale=7.0, roughness=0.38)
endwood = v4.wood("pine_endgrain", light=(0.48, 0.29, 0.16), dark=(0.25, 0.14, 0.075), grain_scale=11.0, roughness=0.48)
zinc = v4.metal("satin_zinc_plating", color=(0.72, 0.76, 0.79), roughness=0.29, brushed=True)
wire = v4.metal("tempered_spring_steel", color=(0.34, 0.38, 0.41), roughness=0.24, brushed=True)
dark_steel = v4.metal("dark_latch_steel", color=(0.22, 0.25, 0.27), roughness=0.3, brushed=True)
red = v4.painted("oxide_red_ink", (0.57, 0.045, 0.025), roughness=0.42)
black = v4.painted("black_enamel", (0.065, 0.075, 0.078), roughness=0.3)
bait_mat = v4.mat("dried_bait", (0.40, 0.29, 0.13), roughness=0.76)
rubber = v4.rubber("grip_rubber", (0.12, 0.13, 0.12))

# Overall envelope: 160 x 72 x 20 mm. Front is -Y, board top is z=12 mm.
L, W, T = 0.160, 0.072, 0.012
base = v4.body("base")
v4.box("single_piece_pine_base", (W, L, T), (0, 0, T/2), bevel=0.0022, material=pine, body=base)

# Dark end-grain caps are thin inlays that sit flush at each end of the board.
for s, nm in [(-1, "front"), (1, "rear")]:
    v4.box(f"{nm}_endgrain_inlay", (W-0.004, 0.0015, 0.008), (0, s*(L/2-0.00075), 0.006),
           bevel=0.00035, material=endwood, body=base)

# Four small underside anti-skid pads touch the board underside.
for x in (-0.027, 0.027):
    for y in (-0.062, 0.062):
        v4.box(f"underside_grip_{x}_{y}", (0.012, 0.018, 0.0015), (x, y, -0.00075),
               bevel=0.0006, material=rubber, body=base)

# Stamped maker plate and compact raised brand mark on the board's clear center.
plate = v4.box("maker_badge", (0.033, 0.018, 0.001), (0, -0.004, 0.0125), bevel=0.001,
               material=black, body=base)
v4.text("maker_wordmark", "FIELDCRAFT", 0.0043, loc=(0, -0.004, 0.0131),
        material=zinc, body=base, on=plate, height=0.00035)
v4.text("maker_model", "MOUSE 160", 0.0027, loc=(0, -0.010, 0.0131),
        material=red, body=base, on=plate, height=0.00025)

# Four small top-side board nails, with a shallow slot suggested by dark fine bars.
for ix, x in enumerate((-0.030, 0.030)):
    for iy, y in enumerate((-0.066, 0.066)):
        v4.cylinder(f"board_nail_{ix}_{iy}", 0.0026, 0.00065, (x, y, 0.01225), segments=24,
                    bevel=0.00018, material=zinc, body=base)
        v4.box(f"nail_slot_{ix}_{iy}", (0.0026, 0.00032, 0.00012), (x, y, 0.01262), bevel=0,
               material=dark_steel, body=base)

# Rear spring saddle: paired stamped ears, a cross staple and an axle bearing line.
hinge_y, bail_z = 0.055, 0.022
for sx in (-1, 1):
    x = sx*0.030
    # Foot and upright lip of each folded zinc strap; all surfaces touch the board / eye.
    v4.box(f"spring_saddle_foot_{sx}", (0.009, 0.018, 0.0012), (x, hinge_y, 0.0126),
           bevel=0.00045, material=zinc, body=base)
    # Twin folded cheeks meet the outside of the eye and rise from the saddle foot.
    for side in (-1, 1):
        v4.rod(f"spring_saddle_cheek_{sx}_{side}", (x+side*0.0035, hinge_y, 0.013),
               (x+side*0.0035, hinge_y, 0.024), 0.0012, material=zinc, body=base)
    v4.torus(f"bail_bearing_eye_{sx}", 0.0035, 0.00105, loc=(x, hinge_y, bail_z),
             rot=(math.pi/2, 0, 0), material=zinc, body=base)
    v4.cylinder(f"axle_rivet_{sx}", 0.002, 0.0022, (x, hinge_y, bail_z), rot=(math.pi/2, 0, 0),
                segments=24, bevel=0.0002, material=dark_steel, body=base)

# Fixed torsion coil: a close-wound 5-turn wire helix about the transverse bail axle.
coil_points = []
turns, steps = 5, 160
for i in range(steps+1):
    u = i/steps
    a = 2*math.pi*turns*u
    coil_points.append((-0.010 + 0.020*u,
                        hinge_y + 0.0048*math.cos(a),
                        bail_z + 0.0048*math.sin(a)))
v4.tube("five_turn_torsion_coil", coil_points, 0.00085, material=wire, body=base, resolution=2)
# Two stationary spring tails bear against the wood and spring saddle.
for sx in (-1, 1):
    v4.tube(f"spring_anchor_leg_{sx}", [
        (sx*0.010, hinge_y, bail_z-0.0045),
        (sx*0.016, hinge_y-0.009, 0.016),
        (sx*0.022, hinge_y-0.018, 0.013),
    ], 0.00085, material=wire, body=base, resolution=2)
    v4.cylinder(f"spring_leg_seat_{sx}", 0.0014, 0.0007, (sx*0.022, hinge_y-0.018, 0.0126),
                segments=16, bevel=0.0001, material=zinc, body=base)

# Bail: a single round-wire U-loop. Cocked tip rises above the base; positive X hinge
# rotation carries it forward/down through a 0.50 rad strike arc.
bail = v4.body("bail")
xspan = 0.031
front_y, front_z = -0.050, 0.075
bail_path = [
    (-xspan, hinge_y, bail_z),
    (-xspan, 0.039, 0.030),
    (-xspan, 0.012, 0.044),
    (-xspan, front_y+0.012, front_z-0.006),
    (-xspan, front_y, front_z),
    (-0.020, front_y-0.002, front_z+0.001),
    (0.000, front_y-0.002, front_z+0.001),
    (0.020, front_y-0.002, front_z+0.001),
    (xspan, front_y, front_z),
    (xspan, front_y+0.012, front_z-0.006),
    (xspan, 0.012, 0.044),
    (xspan, 0.039, 0.030),
    (xspan, hinge_y, bail_z),
]
v4.tube("tempered_wire_striking_bail", bail_path, 0.00165, material=wire, body=bail, resolution=3)
# Short hinge journals finish in the two saddle eyes.
for sx in (-1, 1):
    v4.rod(f"bail_hinge_journal_{sx}", (sx*0.027, hinge_y, bail_z), (sx*0.034, hinge_y, bail_z),
           0.00165, material=wire, body=bail)
v4.joint("bail_hinge", "revolute", base, bail, origin=(0, hinge_y, bail_z), axis=(1, 0, 0),
         limits=(-2.45, 0.50))

# Hold-down bar: pivot eye at rear, straight reach over the raised bail and a formed
# retaining hook which catches the transverse bail wire in the cocked pose.
hold = v4.body("hold_down_bar")
hold_z = 0.040
v4.torus("hold_bar_pivot_eye", 0.0031, 0.0009, loc=(0, 0.053, hold_z),
         rot=(math.pi/2, 0, 0), material=dark_steel, body=hold)
hold_path = [
    (0, 0.053, hold_z),
    (0, 0.044, 0.037),
    (0, 0.025, 0.032),
    (0, -0.012, 0.026),
    (0, -0.030, 0.022),
    (0, -0.039, 0.020),
    (0, -0.042, 0.024),
    (0, -0.039, 0.027),
    (0, -0.035, 0.025),
]
v4.tube("formed_hold_down_latch", hold_path, 0.00135, material=dark_steel, body=hold, resolution=3)
v4.cylinder("hold_bar_pivot_pin", 0.0024, 0.0035, (0, 0.053, hold_z), rot=(math.pi/2, 0, 0),
            segments=24, bevel=0.00018, material=zinc, body=base)
v4.box("hold_bar_staple_foot", (0.010, 0.010, 0.0012), (0, 0.053, 0.0126), bevel=0.0004,
       material=zinc, body=base)
for sx in (-1, 1):
    v4.rod(f"hold_bar_staple_leg_{sx}", (sx*0.004, 0.0555, 0.013), (sx*0.004, 0.0555, 0.040),
           0.001, material=zinc, body=base)
v4.joint("hold_down_pivot", "revolute", base, hold, origin=(0, 0.053, hold_z), axis=(1, 0, 0),
         limits=(0, 0.65))

# Front treadle: a formed galvanized plate with edge flange, pressed ribs, bait cup,
# and a small piece of bait. It rocks on a transverse pin at its rear edge.
treadle = v4.body("treadle")
treadle_pivot_y, treadle_z = -0.043, 0.015
plate_cy = -0.057
v4.box("stamped_treadle_plate", (0.040, 0.034, 0.0022), (0, plate_cy, 0.0158), bevel=0.0024,
       material=zinc, body=treadle)
# Rear lanced retaining tab: the hold-down hook seats over this raised lip.
v4.box("hold_bar_retaining_tab", (0.006, 0.003, 0.010), (0, -0.0395, 0.0217), bevel=0.0008,
       material=dark_steel, body=treadle)
# Three shallow raised traction ribs, laid across the treadle width.
for i, y in enumerate((-0.066, -0.058, -0.050)):
    v4.box(f"treadle_press_rib_{i}", (0.030, 0.0011, 0.00055), (0, y, 0.0171), bevel=0.00045,
           material=wire, body=treadle)
# Slightly dished bait cup, represented by a low ring and a seated bait pellet.
v4.torus("bait_cup_rim", 0.0060, 0.00075, loc=(0, -0.057, 0.0172),
         material=dark_steel, body=treadle)
v4.sphere("bait_pellet", 0.0034, loc=(0, -0.057, 0.0195), segments=24, material=bait_mat, body=treadle)
v4.text("treadle_mark", "BAIT", 0.0030, loc=(0, -0.057, 0.0170), material=dark_steel,
        body=treadle, on="auto", height=0.0002)
# Side hinge ears and a transverse hinge pin; visual hardware fixed to the treadle ears.
for sx in (-1, 1):
    x = sx*0.019
    v4.box(f"treadle_hinge_ear_{sx}", (0.003, 0.006, 0.006), (x, treadle_pivot_y, 0.015),
           bevel=0.0007, material=zinc, body=treadle)
    v4.cylinder(f"treadle_pin_end_{sx}", 0.0022, 0.002, (sx*0.021, treadle_pivot_y, treadle_z),
                rot=(math.pi/2, 0, 0), segments=20, bevel=0.00015, material=dark_steel, body=treadle)
v4.cylinder("treadle_axle", 0.0017, 0.044, (0, treadle_pivot_y, treadle_z),
            rot=(0, math.pi/2, 0), segments=24, bevel=0.0001, material=dark_steel, body=base)
v4.joint("treadle_hinge", "revolute", base, treadle, origin=(0, treadle_pivot_y, treadle_z),
         axis=(1, 0, 0), limits=(-0.22, 0.22))

# Add a thin red printed arrow on the exposed wood ahead of the center badge.
v4.box("trigger_direction_print", (0.002, 0.010, 0.00035), (0, -0.031, 0.01215), bevel=0,
       material=red, body=base)
v4.box("trigger_direction_head", (0.010, 0.002, 0.00035), (0, -0.037, 0.01215), bevel=0,
       material=red, body=base)

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