"""Detailed 10 L utility pail with a retained, pivoting wire bail."""
import math
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Moulded teal polypropylene, charcoal elastomer, zinc plated steel, orange grip.
teal = v4.plastic("deep_teal_polypropylene", (0.035, 0.24, 0.25), roughness=0.34, texture=0.11)
teal_light = v4.plastic("raised_teal_details", (0.055, 0.31, 0.31), roughness=0.35, texture=0.08)
charcoal = v4.rubber("charcoal_base_elastomer", (0.035, 0.045, 0.047), roughness=0.72)
steel = v4.metal("zinc_plated_wire", (0.72, 0.77, 0.79), roughness=0.28, brushed=False)
orange = v4.plastic("safety_orange_grip", (0.94, 0.25, 0.045), roughness=0.38, texture=0.12)
white = v4.mat("raised_cream_markings", (0.88, 0.88, 0.79), roughness=0.48)

bucket = v4.body("bucket")
# Closed cross-section: thick foot, tapered outer wall, rolled rim, and real inner wall/floor.
profile = [
    (0.000, 0.012), (0.122, 0.012), (0.128, 0.014), (0.130, 0.018),
    (0.130, 0.024), (0.128, 0.031), (0.128, 0.038), (0.130, 0.041),
    (0.133, 0.078), (0.135, 0.080), (0.136, 0.083), (0.135, 0.086),
    (0.137, 0.142), (0.139, 0.144), (0.140, 0.147), (0.139, 0.150),
    (0.142, 0.207), (0.144, 0.209), (0.145, 0.212), (0.144, 0.215),
    (0.146, 0.255), (0.148, 0.257), (0.149, 0.260), (0.148, 0.263),
    (0.149, 0.274), (0.151, 0.280), (0.151, 0.286),
    (0.145, 0.290), (0.147, 0.290), (0.147, 0.286), (0.146, 0.278),
    (0.124, 0.044), (0.122, 0.031), (0.119, 0.025), (0.000, 0.025)
]
v4.lathe("moulded_bucket_shell", profile, material=teal, body=bucket, segments=96)
# Shallow circumferential reinforcing beads are integrated into the shell cross-section.
# Foot ring adds a resilient-looking dark contact surface under the pail.
v4.lathe("rubberized_foot_ring", [(0.125, 0.003), (0.132, 0.003), (0.134, 0.009), (0.134, 0.025), (0.130, 0.031), (0.124, 0.031)],
         material=charcoal, body=bucket, segments=96)
# The rolled top bead and narrow inner lip are built into the shell profile.

# Handle ears at opposite ends of the common transverse pivot axis (Y).
# Each ear is a molded saddle that projects from the tapered shell and carries a plated axle.
for side, label in ((-1, "left"), (1, "right")):
    v4.box(f"ear_saddle_{label}", (0.040, 0.035, 0.030), (0, side * 0.1635, 0.224),
           bevel=0.006, material=teal, body=bucket)
    # Outer cheek meets the saddle face; hardware and bail journal share one pivot line.
    v4.cylinder(f"ear_cheek_{label}", 0.017, 0.018, (0, side * 0.190, 0.252),
                rot=(math.pi/2, 0, 0), material=teal_light, body=bucket)
    v4.cylinder(f"pivot_pin_{label}", 0.006, 0.056, (0, side * 0.163, 0.252),
                rot=(math.pi/2, 0, 0), material=steel, body=bucket, bevel=0.0007)
    v4.cylinder(f"retainer_cap_{label}", 0.010, 0.003, (0, side * 0.202, 0.252),
                rot=(math.pi/2, 0, 0), material=steel, body=bucket, bevel=0.001)
    v4.torus(f"pin_cap_rim_{label}", 0.0082, 0.001, loc=(0, side * 0.204, 0.252),
             rot=(math.pi/2, 0, 0), material=steel, body=bucket, segments=32, ring_segments=10)

# Small molded front badge and understated raised capacity marks, mounted on the body.
v4.box("front_badge", (0.075, 0.0025, 0.034), (0, -0.137, 0.105), bevel=0.004, material=teal_light, body=bucket)
v4.text("capacity_mark", "10 L", 0.014, loc=(0, -0.139, 0.105), rot=(math.pi/2, 0, 0),
        extrude_depth=0.00035, material=white, body=bucket)
v4.text("product_mark", "UTILITY", 0.0062, loc=(0, -0.139, 0.092), rot=(math.pi/2, 0, 0),
        extrude_depth=0.00025, material=white, body=bucket)
# A row of molded vertical graduations on the front-right side.
for i in range(4):
    z = 0.14 + i * 0.022
    shell_r = 0.130 + (z - 0.041) * (0.149 - 0.130) / (0.274 - 0.041)
    y = -math.sqrt(max(0.0, shell_r**2 - 0.030**2))
    v4.box(f"graduation_{i+1}", (0.018 if i % 2 == 0 else 0.010, 0.0014, 0.0018),
           (0.030, y - 0.0007, z), bevel=0.0005, material=teal_light, body=bucket)

# One continuous steel bail, including short journals through the two ears.
bail = v4.body("bail")
wire_pts = [
    (0, -0.151, 0.252), (0, -0.169, 0.286), (0, -0.168, 0.355),
    (0, -0.168, 0.454), (0, -0.153, 0.493), (0, -0.121, 0.522),
    (0, -0.080, 0.538), (0, 0.000, 0.543), (0, 0.080, 0.538),
    (0, 0.121, 0.522), (0, 0.153, 0.493), (0, 0.168, 0.454),
    (0, 0.168, 0.355), (0, 0.169, 0.286), (0, 0.151, 0.252)
]
v4.tube("continuous_bent_steel_bail", wire_pts, 0.0042, material=steel, body=bail, resolution=16)
# Molded grip sleeve and shallow tactile ribs around the crown of the wire.
v4.cylinder("orange_handle_grip", 0.010, 0.112, (0, 0, 0.543), rot=(math.pi/2, 0, 0),
            material=orange, body=bail, bevel=0.002, segments=48)
for i in range(9):
    yy = -0.050 + i * 0.0125
    v4.torus(f"grip_tactile_rib_{i+1}", 0.0097, 0.0011, loc=(0, yy, 0.543),
             rot=(math.pi/2, 0, 0), material=orange, body=bail, segments=32, ring_segments=10)

# The two coaxial revolute joints retain both journal ends while allowing the bail to swing.
axis = (0, 1, 0)
limits = (-math.radians(75), math.radians(75))
v4.joint("bail_left_pivot", "revolute", bucket, bail, origin=(0, -0.151, 0.252), axis=axis, limits=limits)
v4.joint("bail_right_pivot", "revolute", bucket, bail, origin=(0, 0.151, 0.252), axis=axis, limits=limits)

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