"""Detailed articulated wooden spring snap trap, authored 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()

# Materials: warm hardwood, bright zinc hardware, darker spring wire, and a
# stamped steel trigger plate.
wood = v4.wood("sealed_hardwood", light=(0.53, 0.34, 0.19), dark=(0.25, 0.13, 0.06),
               grain_scale=5.0, roughness=0.58, coat=0.08)
steel = v4.metal("zinc_plated_steel", color=(0.70, 0.73, 0.75), roughness=0.32, brushed=True)
wire = v4.metal("tempered_spring_steel", color=(0.23, 0.26, 0.29), roughness=0.27, brushed=False)
dark_steel = v4.metal("darkened_trigger_steel", color=(0.36, 0.39, 0.41), roughness=0.38, brushed=False)
ink = v4.mat("printed_ink", (0.08, 0.055, 0.035), roughness=0.72)
food = v4.mat("bait_residue", (0.42, 0.20, 0.07), roughness=0.82)

# Proportions: 95 x 48 x 9 mm hardwood board. Rear spring is +Y; bait is -Y.
L, W, BASE_T = 0.095, 0.048, 0.009
BASE_TOP = BASE_T
PIVOT_Y, PIVOT_Z = 0.037, 0.018

# Four bodies: one fixed base and three independent moving links.
base = v4.body("base")
bail = v4.body("bail")
hold = v4.body("hold_down_bar")
treadle = v4.body("treadle")

# ---- Hardwood base and fixed spring / hinge mount ----------------------------------
board = v4.box("oiled_hardwood_board", (W, L, BASE_T), (0, 0, BASE_T / 2), bevel=0.0018,
               material=wood, body=base)

# Two stamped saddle tabs carry the shared rear hinge line. Their feet sit directly
# on the board, with small screws securing each foot.
for side in (-1, 1):
    x = side * 0.0165
    v4.box(f"rear_saddle_foot_{side}", (0.008, 0.010, 0.0010), (x, 0.0345, BASE_TOP + 0.00035),
           bevel=0.00035, material=steel, body=base)
    v4.box(f"rear_saddle_ear_{side}", (0.0030, 0.0040, 0.0090), (x, PIVOT_Y, 0.0140),
           bevel=0.0006, material=steel, body=base)
    # Mounting screws sit on the outer edge of each saddle foot.
    v4.cylinder(f"saddle_screw_{side}", 0.0015, 0.00065,
                (x + side * 0.0024, 0.0324, BASE_TOP + 0.00075),
                segments=20, bevel=0.00012, material=wire, body=base)
    v4.box(f"saddle_screw_slot_{side}", (0.00165, 0.00025, 0.00012),
           (x + side * 0.0024, 0.0324, BASE_TOP + 0.00110), bevel=0.00005,
           material=ink, body=base)

# The staple axle passes through the two ears and under the spring coil.
v4.rod("rear_staple_pivot_pin", (-0.021, PIVOT_Y, PIVOT_Z), (0.021, PIVOT_Y, PIVOT_Z),
       0.0010, material=steel, body=base)
for side in (-1, 1):
    v4.cylinder(f"pivot_pin_end_{side}", 0.00155, 0.0011,
                (side * 0.0209, PIVOT_Y, PIVOT_Z), rot=(0, math.pi / 2, 0),
                segments=24, bevel=0.00018, material=steel, body=base)

# A formed torsion coil around the staple, with its two anchor legs represented as
# fixed wire ends. The moving spring response is visual; flexible spring deformation
# is not a rigid-body degree of freedom in this authoring format.
spring_center = (0.0, PIVOT_Y, PIVOT_Z + 0.0002)
coil_points = []
turns, steps, coil_r, coil_width = 3.35, 144, 0.0032, 0.013
for i in range(steps + 1):
    t = i / steps
    a = 2.0 * math.pi * turns * t
    coil_points.append((-coil_width / 2 + coil_width * t,
                       spring_center[1] + coil_r * math.cos(a),
                       spring_center[2] + coil_r * math.sin(a)))
v4.tube("torsion_spring_coil", coil_points, 0.00062, material=wire, body=base)
v4.tube("torsion_spring_anchor_leg", [
    coil_points[0], (-0.009, 0.0405, 0.0160), (-0.013, 0.0405, 0.0105),
    (-0.013, 0.0385, BASE_TOP + 0.0008)], 0.00062, material=wire, body=base)
v4.tube("torsion_spring_pressure_leg", [
    coil_points[-1], (0.009, 0.0340, 0.0200), (0.015, 0.0340, 0.0170),
    (0.020, PIVOT_Y, PIVOT_Z)], 0.00062, material=wire, body=base)

# Tiny stamped maker mark on an open strip of wood beside the catch bar.
v4.text("board_maker_mark", "FIELD MOUSE TRAP", 0.0030, loc=(-0.004, 0.006, BASE_TOP + 0.0002),
        material=ink, body=base, on=board, height=0.00012)

# ---- The one-piece tempered-wire striking bail --------------------------------------
# Cocked rest pose: side arms rise from the rear hinge and the broad front crossbar
# hangs above the bait area. The hinge sweep then carries it down across the board.
left_arm = [(-0.0205, PIVOT_Y, PIVOT_Z), (-0.0205, 0.030, 0.025),
            (-0.0205, 0.012, 0.043), (-0.0205, -0.010, 0.061),
            (-0.0205, -0.026, 0.075)]
right_arm = [(0.0205, -0.026, 0.075), (0.0205, -0.010, 0.061),
             (0.0205, 0.012, 0.043), (0.0205, 0.030, 0.025),
             (0.0205, PIVOT_Y, PIVOT_Z)]
bail_path = left_arm + [(0.0, -0.029, 0.076)] + right_arm
v4.tube("formed_u_striker_bail", bail_path, 0.00115, material=steel, body=bail)
# A short, dark impact sleeve on the center of the striking crossbar.
v4.tube("striker_impact_sleeve", [(-0.0055, -0.029, 0.076), (0.0055, -0.029, 0.076)],
        0.00175, material=wire, body=bail)
# Hinge eyes formed at the bail ends sit around the staple line.
for side in (-1, 1):
    v4.torus(f"bail_hinge_eye_{side}", 0.0021, 0.00072,
             loc=(side * 0.0205, PIVOT_Y, PIVOT_Z), rot=(0, math.pi / 2, 0),
             segments=48, ring_segments=12, material=steel, body=bail)

# ---- The long catch / hold-down wire ------------------------------------------------
# This wire pivots on the rear staple. It runs low over the board, then rises into a
# small open hook around the striker crossbar; its heel rests in the treadle's forked
# catch notch. Lifting the bar clears the striker and the notch.
hold_path = [
    (0.0, PIVOT_Y, PIVOT_Z),
    (0.0, 0.031, 0.0155), (0.0, 0.017, 0.0120),
    (0.0, -0.002, 0.0118), (0.0, -0.018, 0.0120),
    (0.0, -0.020, 0.0139), (0.0, -0.021, 0.0160),
    (0.0, -0.021, 0.034),
    (0.0, -0.021, 0.056), (0.0, -0.021, 0.068),
    (0.0, -0.024, 0.073), (0.0, -0.029, 0.073),
    (0.0, -0.032, 0.070), (0.0, -0.032, 0.066),
    (0.0, -0.029, 0.064),
]
v4.tube("hold_down_catch_wire", hold_path, 0.00082, material=steel, body=hold)
# A flattened contact heel sits just inside the paddle notch.
v4.rod("catch_heel", (0.0, -0.020, 0.0139), (0.0, -0.0155, 0.0139),
       0.00070, material=wire, body=hold)

# ---- Hinged stamped bait treadle -----------------------------------------------------
# Small pivot brackets and cross-pin are fixed to the board. The paddle has two eyes
# around this pin and a forked rear notch that seats the hold-down heel.
TREADLE_Y, TREADLE_Z = -0.0355, 0.0145
for side in (-1, 1):
    x = side * 0.0100
    v4.box(f"treadle_hinge_saddle_{side}", (0.0048, 0.0040, 0.0058),
           (x, TREADLE_Y, 0.0118), bevel=0.0006, material=steel, body=base)
    v4.cylinder(f"treadle_saddle_screw_{side}", 0.0012, 0.0006,
                (x, TREADLE_Y - 0.0022, BASE_TOP + 0.0007),
                segments=20, bevel=0.0001, material=wire, body=base)
v4.rod("treadle_hinge_pin", (-0.013, TREADLE_Y, TREADLE_Z),
       (0.013, TREADLE_Y, TREADLE_Z), 0.00085, material=steel, body=base)

# Paddle outline, with an open central fork at its back edge for the catch heel.
# Coordinates are CCW in the XY plane; extrusion begins above the board surface.
paddle_outline = [
    (-0.0090, -0.0380), (0.0090, -0.0380),
    (0.0130, -0.0340), (0.0130, -0.0180), (0.0110, -0.0120),
    (0.0080, -0.0090), (0.0040, -0.0090), (0.0030, -0.0140),
    (0.0016, -0.0150), (-0.0016, -0.0150), (-0.0030, -0.0140),
    (-0.0040, -0.0090), (-0.0080, -0.0090), (-0.0110, -0.0120),
    (-0.0130, -0.0180), (-0.0130, -0.0340),
]
plate = v4.extrude("stamped_bait_treadle", paddle_outline, 0.00145,
                    loc=(0, 0, 0.01175), bevel=0.00045, material=steel, body=treadle)

# Two small barrel eyes meet the paddle at the hinge line.
for side in (-1, 1):
    v4.cylinder(f"treadle_hinge_eye_{side}", 0.0018, 0.0040,
                (side * 0.0096, TREADLE_Y, TREADLE_Z), rot=(0, math.pi / 2, 0),
                segments=28, bevel=0.00025, material=steel, body=treadle)

# Shallow stamped bait dish: dark floor, bright rolled lip and a modest bait dab.
cup_x, cup_y = -0.0045, -0.0260
v4.cylinder("bait_cup_floor", 0.0048, 0.00035, (cup_x, cup_y, 0.01332),
            segments=48, bevel=0.00018, material=dark_steel, body=treadle)
v4.torus("bait_cup_rolled_lip", 0.0053, 0.00048, loc=(cup_x, cup_y, 0.01353),
         segments=48, ring_segments=12, material=steel, body=treadle)
v4.sphere("small_bait_dab", 0.0017, (cup_x + 0.0005, cup_y, 0.01370),
          segments=24, material=food, body=treadle)

# A pair of stamped side ribs stiffen the trigger plate without blocking its catch fork.
for side in (-1, 1):
    v4.tube(f"treadle_stiffening_rib_{side}", [
        (side * 0.0108, -0.0320, 0.01336),
        (side * 0.0110, -0.0260, 0.0135),
        (side * 0.0107, -0.0170, 0.01336)],
        0.00034, material=dark_steel, body=treadle)

# ---- Articulation -------------------------------------------------------------------
v4.joint("bail_hinge", "revolute", base, bail,
         origin=(0.0, PIVOT_Y, PIVOT_Z), axis=(1, 0, 0),
         limits=(0.0, 0.78))
v4.joint("hold_down_hinge", "revolute", base, hold,
         origin=(0.0, PIVOT_Y, PIVOT_Z), axis=(1, 0, 0),
         limits=(-0.25, 0.0))
v4.joint("treadle_hinge", "revolute", base, treadle,
         origin=(0.0, TREADLE_Y, TREADLE_Z), axis=(1, 0, 0),
         limits=(-0.10, 0.0))

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