"""Detailed articulated spincast fishing reel. Run with Blender's background Python."""
import math
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Materials
shell = v4.plastic("graphite_polymer", color=(0.075, 0.085, 0.095), roughness=0.28)
shell_hi = v4.plastic("raised_graphite", color=(0.13, 0.15, 0.16), roughness=0.25)
shell_dark = v4.plastic("black_seam", color=(0.025, 0.03, 0.034), roughness=0.4)
rubber = v4.rubber("soft_black_rubber", color=(0.035, 0.04, 0.04), roughness=0.75)
aluminum = v4.metal("spool_anodized_aluminum", color=(0.63, 0.67, 0.69), roughness=0.28, brushed=True)
steel = v4.metal("satin_stainless", color=(0.72, 0.75, 0.76), roughness=0.24, brushed=False)
accent = v4.metal("champagne_accent", color=(0.72, 0.48, 0.20), roughness=0.32, brushed=True)
line_mat = v4.mat("monofilament_blue", (0.23, 0.42, 0.48), roughness=0.38)

# Axial-cylinder convenience: local cylinder Z -> world -Y.
AXIAL = (math.pi / 2, 0, 0)
SIDE = (0, math.pi / 2, 0)  # local Z -> +X

# Rigid bodies
housing = v4.body("gear_housing")
spool = v4.body("spool")
thumb = v4.body("thumb_button")
crank = v4.body("crank")

# Main two-piece reel housing, centered at z=.075; shell halves meet at a fine waist seam.
v4.cylinder("housing_core", 0.0455, 0.039, (0, 0.001, 0.075), rot=AXIAL, bevel=0.0022,
            material=shell, body=housing)
# Thin molded waist ring visible around the circumference.
v4.cylinder("housing_mid_seam", 0.0462, 0.0015, (0, 0, 0.075), rot=AXIAL, bevel=0.0004,
            material=shell_dark, body=housing)
# Rear cover boss and thumb guide bezel.
v4.cylinder("rear_cover_boss", 0.029, 0.006, (0, 0.0215, 0.075), rot=AXIAL, bevel=0.001,
            material=shell_hi, body=housing)
v4.cylinder("thumb_guide_ring", 0.016, 0.006, (0, 0.027, 0.075), rot=AXIAL, bevel=0.0008,
            material=shell_dark, body=housing)
v4.cylinder("thumb_guide_lip", 0.013, 0.002, (0, 0.0305, 0.075), rot=AXIAL, bevel=0.0005,
            material=steel, body=housing)

# Closed nose shell: a true hollow turned cap with a small line-exit hole.
# Local +Z points toward the front (-Y). The profile returns along its inner surface.
nose_profile = [
    (0.0062, 0.050), (0.010, 0.050), (0.017, 0.047), (0.026, 0.039),
    (0.034, 0.029), (0.040, 0.017), (0.043, 0.008), (0.043, 0.002),
    (0.040, 0.000), (0.037, 0.004), (0.036, 0.011), (0.032, 0.022),
    (0.024, 0.033), (0.016, 0.042), (0.009, 0.047), (0.0062, 0.047),
    (0.0062, 0.050)
]
v4.lathe("closed_nose_cone", nose_profile, loc=(0, -0.020, 0.075), rot=AXIAL,
         segments=96, material=shell_hi, body=housing)
# Nose-cap retaining lip and thin warm accent band at its base.
v4.torus("nose_retainer_bead", 0.043, 0.0013, loc=(0, -0.025, 0.075), rot=AXIAL,
         material=shell_dark, body=housing)
v4.torus("nose_accent_ring", 0.0423, 0.00075, loc=(0, -0.028, 0.075), rot=AXIAL,
         material=accent, body=housing)
# The eyelet occupies the actual small aperture at the tip.
v4.torus("line_exit_eyelet", 0.0065, 0.0012, loc=(0, -0.071, 0.075), rot=AXIAL,
         material=steel, body=housing)
v4.cylinder("eyelet_bushing", 0.0051, 0.005, (0, -0.0685, 0.075), rot=AXIAL,
            bevel=0.0005, material=shell_dark, body=housing)

# Side axle boss on the right side of the reel. The crank pivot shares its centerline.
v4.cylinder("crank_axle_boss", 0.0135, 0.009, (0.048, 0.004, 0.075), rot=SIDE,
            bevel=0.001, material=shell_dark, body=housing)
v4.cylinder("crank_bearing_cap", 0.009, 0.003, (0.0535, 0.004, 0.075), rot=SIDE,
            bevel=0.0006, material=steel, body=housing)

# Reel foot: beveled, tapered mounting plate fitted directly to the housing underside.
foot_outline = [(-0.010, -0.023), (0.010, -0.023), (0.013, -0.016), (0.010, 0.021),
                (0.007, 0.026), (-0.007, 0.026), (-0.010, 0.021), (-0.013, -0.016)]
v4.extrude("reel_foot", foot_outline, 0.009, loc=(0, 0, 0.018), bevel=0.002,
           material=steel, body=housing)
# Two dark inset mounting-slot marks (on the top face of the foot).
for i, y in enumerate((-0.013, 0.015)):
    v4.box(f"foot_slot_inset_{i}", (0.006, 0.012, 0.0007), (0, y, 0.0273),
           bevel=0.0025, material=shell_dark, body=housing)
# Foot-to-body saddle collar at the interface.
v4.box("foot_saddle", (0.026, 0.035, 0.004), (0, 0.002, 0.0295), bevel=0.003,
       material=shell, body=housing)

# Housing radial mold ribs on the forward annular face (terminate clear of the nose shell).
for i in range(12):
    a = 2 * math.pi * i / 12
    # Short raised ribs aligned tangent to the face's circumferential direction.
    r = 0.039
    x, z = r * math.cos(a), 0.075 + r * math.sin(a)
    rib = v4.box(f"front_vent_rib_{i:02d}", (0.006, 0.0014, 0.0015),
                 (x, -0.0202, z), rot=(0, -a, 0), bevel=0.0005,
                 material=shell, body=housing)
# Six recessed-looking fastener heads on the housing outer face, spaced around the nose seat.
for i in range(6):
    a = 2 * math.pi * i / 6
    x, z = 0.040 * math.cos(a), 0.075 + 0.040 * math.sin(a)
    v4.cylinder(f"housing_screw_{i}", 0.0023, 0.0015, (x, -0.0202, z), rot=AXIAL,
                segments=24, bevel=0.0004, material=steel, body=housing)
    v4.box(f"screw_slot_{i}", (0.0024, 0.0005, 0.00045), (x, -0.0211, z),
           rot=(0, -a, 0), bevel=0.00015, material=shell_dark, body=housing)

# Small product badge on the visible forward upper quadrant.
v4.cylinder("badge_base", 0.007, 0.0012, (0.028, -0.0211, 0.101), rot=AXIAL,
            bevel=0.0004, material=accent, body=housing)
v4.text("badge_mark", "S", 0.007, loc=(0.0257, -0.0220, 0.0985),
        rot=(math.pi / 2, 0, 0), extrude_depth=0.0003, material=shell_dark, body=housing)

# Internal spool: stays inside the hollow closed nose cap with clearance to its wall.
# Its front-to-back axis is Y; profiled flanges and grooved line barrel are turned components.
spool_center_y = -0.037
v4.cylinder("spool_barrel", 0.021, 0.024, (0, spool_center_y, 0.075), rot=AXIAL,
            bevel=0.0006, material=aluminum, body=spool)
v4.lathe("spool_rear_flange", [(0.008, 0), (0.027, 0), (0.031, 0.002),
          (0.032, 0.004), (0.030, 0.006), (0.010, 0.006), (0.008, 0)],
         loc=(0, -0.0225, 0.075), rot=AXIAL, segments=64, material=aluminum, body=spool)
# The front flange is tapered to follow the narrowing inside wall of the nose shell.
v4.lathe("spool_front_flange", [(0.006, 0), (0.020, 0), (0.023, 0.002),
          (0.024, 0.004), (0.022, 0.006), (0.008, 0.006), (0.006, 0)],
         loc=(0, -0.0445, 0.075), rot=AXIAL, segments=64, material=aluminum, body=spool)
# Fine, evenly spaced line-wrap grooves and a single muted line layer on the spool barrel.
for k in range(9):
    yy = -0.047 + k * 0.0025
    v4.torus(f"spool_groove_{k:02d}", 0.0211, 0.00028, loc=(0, yy, 0.075), rot=AXIAL,
             material=steel, body=spool)
v4.torus("line_wrap", 0.022, 0.0011, loc=(0, spool_center_y, 0.075), rot=AXIAL,
         material=line_mat, body=spool)
# Stepped bearing and axle sleeve, terminating before the fixed rear housing face.
v4.cylinder("spool_hub", 0.010, 0.028, (0, spool_center_y, 0.075), rot=AXIAL,
            bevel=0.0008, material=steel, body=spool)

# Rear thumb button: sliding stem, shallow scalloped pad and rubberized raised grip surface.
v4.cylinder("thumb_stem", 0.006, 0.034, (0, 0.044, 0.075), rot=AXIAL,
            bevel=0.0006, material=steel, body=thumb)
v4.lathe("thumb_button_shell", [(0, 0), (0.010, 0), (0.013, 0.002), (0.015, 0.006),
          (0.014, 0.011), (0.010, 0.014), (0, 0.014)],
         loc=(0, 0.060, 0.075), rot=AXIAL, segments=64, material=shell_hi, body=thumb)
v4.cylinder("thumb_pad", 0.010, 0.0022, (0, 0.064, 0.075), rot=AXIAL,
            bevel=0.001, material=rubber, body=thumb)
# Shallow concentric grip ridges on the rear-facing pad.
for radius in (0.0032, 0.0060, 0.0083):
    v4.torus(f"thumb_ridge_{radius:.4f}", radius, 0.00055, loc=(0, 0.0652, 0.075),
             rot=AXIAL, material=shell_hi, body=thumb)

# Crank arm and grip, all rotating around the right-side lateral axle.
v4.cylinder("crank_hub", 0.010, 0.014, (0.060, 0.004, 0.075), rot=SIDE,
            bevel=0.001, material=steel, body=crank)
v4.cylinder("crank_hub_cap", 0.0065, 0.002, (0.068, 0.004, 0.075), rot=SIDE,
            bevel=0.0005, material=accent, body=crank)
# Bent offset arm sweeps down and forward in the YZ plane.
v4.rod("crank_arm_inner", (0.064, 0.004, 0.075), (0.071, -0.008, 0.061),
       0.0042, material=steel, body=crank)
v4.rod("crank_arm_outer", (0.071, -0.008, 0.061), (0.077, -0.027, 0.045),
       0.0042, material=steel, body=crank)
v4.sphere("arm_joint_bead", 0.0053, (0.071, -0.008, 0.061), material=steel, body=crank)
# Knob axle and comfortable fluted rubber grip; grip axis follows the crank axle.
v4.cylinder("grip_spindle", 0.0035, 0.023, (0.0885, -0.027, 0.045), rot=SIDE,
            bevel=0.0005, material=steel, body=crank)
v4.lathe("crank_grip", [(0, 0), (0.006, 0), (0.0085, 0.003), (0.009, 0.008),
          (0.008, 0.018), (0.006, 0.022), (0, 0.022)],
         loc=(0.100, -0.027, 0.045), rot=SIDE, segments=48, material=rubber, body=crank)
for j in range(5):
    x = 0.102 + j * 0.0035
    v4.torus(f"grip_flute_{j}", 0.0078, 0.00035, loc=(x, -0.027, 0.045), rot=SIDE,
             material=shell_dark, body=crank)

# Articulations. Each pivot is at the actual shaft / guide centerline.
# Set the spool hub close to the front bearing face while retaining a running clearance.
spool.location.y = 0.001
v4.joint("spool_spin", "continuous", housing, spool,
         origin=(0, -0.036, 0.075), axis=(0, 1, 0))
v4.joint("thumb_travel", "prismatic", housing, thumb,
         origin=(0, 0.034, 0.075), axis=(0, 1, 0), limits=(0.0, 0.012))
v4.joint("crank_spin", "continuous", housing, crank,
         origin=(0.052, 0.004, 0.075), axis=(1, 0, 0))

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