"""Detailed top-loading water cooler with two hinged paddle actuators."""
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("warm_white_enamel_polymer", (0.91, 0.91, 0.88), roughness=0.29, texture=0.08)
white_trim = v4.plastic("white_trim", (0.97, 0.97, 0.94), roughness=0.31, texture=0.06)
black = v4.plastic("charcoal_recess", (0.045, 0.055, 0.062), roughness=0.36, texture=0.1)
black_rubber = v4.rubber("foot_and_grip_rubber", (0.025, 0.03, 0.032))
steel = v4.metal("satin_stainless", (0.72, 0.75, 0.77), roughness=0.3, brushed=True)
steel_dark = v4.metal("dark_stainless", (0.28, 0.31, 0.33), roughness=0.34, brushed=True)
copper = v4.metal("condenser_copper", (0.65, 0.31, 0.13), roughness=0.34, brushed=False)
blue = v4.plastic("cold_blue", (0.025, 0.24, 0.72), roughness=0.27, texture=0.06)
red = v4.plastic("hot_red", (0.78, 0.045, 0.035), roughness=0.29, texture=0.06)
child_safe = v4.plastic("child_safety_amber", (0.98, 0.72, 0.08), roughness=0.28, texture=0.04)
blue_light = v4.emissive("blue_status", (0.12, 0.52, 1.0), strength=1.3)
label = v4.mat("lettering", (0.82, 0.86, 0.87), roughness=0.4)
water = v4.plastic("cool_blue_pet_bottle", (0.32, 0.68, 0.82), roughness=0.19, texture=0.025)
water_band = v4.plastic("water_tint", (0.08, 0.40, 0.60), roughness=0.18, texture=0.02)
# A restrained amount of transmission gives the PET jug a translucent edge while
# keeping the silhouette and molded ribs legible in the product render.
pet_bsdf = water.node_tree.nodes.get("Principled BSDF")
if pet_bsdf and "Transmission Weight" in pet_bsdf.inputs:
    pet_bsdf.inputs["Transmission Weight"].default_value = 0.16
    pet_bsdf.inputs["IOR"].default_value = 1.46

# Dimensions: front is -Y, floor is z=0. Main cabinet is 0.36 x 0.34 x 0.88 m.
base = v4.body("cabinet")

# Four discreet rubber pads; slight recessed toe plinth.
for sx in (-1, 1):
    for sy in (-1, 1):
        v4.box(f"rubber_foot_{sx}_{sy}", (0.052, 0.048, 0.0165), (sx*0.143, sy*0.133, 0.00825), bevel=0.003, material=black_rubber, body=base)
v4.box("toe_plinth", (0.385, 0.355, 0.075), (0, 0.006, 0.054), bevel=0.018, material=white_trim, body=base)
v4.box("lower_shadow_seam", (0.371, 0.342, 0.008), (0, -0.001, 0.094), bevel=0.003, material=black, body=base)

# Rounded cabinet housing assembled around a real recessed faucet bay. The open
# front center gives the moving paddles room to swing into the cabinet.
for side in (-1, 1):
    v4.box(f"cabinet_side_{side}", (0.034, 0.34, 0.82), (side*0.163, 0, 0.51), bevel=0.018, material=shell, body=base)
v4.box("cabinet_back", (0.292, 0.030, 0.82), (0, 0.155, 0.51), bevel=0.014, material=shell, body=base)
v4.box("cabinet_front_lower", (0.292, 0.014, 0.395), (0, -0.163, 0.300), bevel=0.009, material=shell, body=base)
v4.box("cabinet_front_upper", (0.292, 0.014, 0.105), (0, -0.163, 0.870), bevel=0.009, material=shell, body=base)
v4.box("cabinet_internal_shelf", (0.292, 0.280, 0.018), (0, 0, 0.490), bevel=0.006, material=shell, body=base)
# Recess supports tie the faucet panel back into the side walls.
for side in (-1, 1):
    v4.box(f"faucet_mount_bracket_{side}", (0.030, 0.045, 0.125), (side*0.139, -0.176, 0.685), bevel=0.004, material=black, body=base)
# Thin front access panel seam and maintenance hatch at lower right.
v4.box("front_service_hatch", (0.17, 0.004, 0.19), (0.075, -0.171, 0.285), bevel=0.009, material=white_trim, body=base)
# A narrow dark gasket line makes the lower access hatch read as a door.
for sx in (-1, 1):
    v4.box(f"hatch_seam_side_{sx}", (0.0018, 0.002, 0.174), (0.075+sx*0.085, -0.1734, 0.285), bevel=0.0006, material=black, body=base)
for sz in (-1, 1):
    v4.box(f"hatch_seam_edge_{sz}", (0.166, 0.002, 0.0018), (0.075, -0.1734, 0.285+sz*0.095), bevel=0.0006, material=black, body=base)
for sx in (-1, 1):
    for z in (0.215, 0.355):
        v4.cylinder(f"hatch_screw_{sx}_{z}", 0.0032, 0.0015, (0.075+sx*0.068, -0.174, z), rot=(math.pi/2, 0, 0), material=steel_dark, body=base, bevel=0.0003)
v4.box("hatch_pull_recess", (0.026, 0.0015, 0.004), (0.075, -0.174, 0.285), bevel=0.0015, material=black, body=base)

# Side ventilation banks on both cabinet flanks, all attached to the shell.
for side in (-1, 1):
    x = side*0.1812
    for row in range(8):
        z = 0.31 + row*0.031
        v4.box(f"side_louver_{side}_{row}", (0.0018, 0.205, 0.009), (x, 0.028, z), bevel=0.0035, material=black, body=base)
        v4.box(f"louver_lip_{side}_{row}", (0.0025, 0.208, 0.0025), (x+side*0.001, 0.028, z+0.004), bevel=0.001, material=white_trim, body=base)

# Rear condenser panel, exposed serpentine coil, electrical cord and hot-tank switch.
v4.box("rear_coil_panel", (0.248, 0.004, 0.194), (0, 0.1715, 0.390), bevel=0.006, material=black, body=base)
for sx in (-1, 1):
    v4.box(f"rear_grille_side_{sx}", (0.004, 0.003, 0.178), (sx*0.118, 0.175, 0.390), bevel=0.0012, material=steel_dark, body=base)
for z in (0.302, 0.478):
    v4.box(f"rear_grille_rail_{z}", (0.238, 0.003, 0.004), (0, 0.175, z), bevel=0.0012, material=steel_dark, body=base)
coil_points = []
for row in range(7):
    z = 0.315 + row*0.0235
    a, b = (-0.105, 0.105) if row % 2 == 0 else (0.105, -0.105)
    coil_points.extend([(a,0.177,z), (b,0.177,z)])
    if row < 6:
        coil_points.append((b,0.177,z+0.0235))
v4.tube("rear_condenser_coil", coil_points, 0.0022, material=copper, body=base, resolution=8)
for sx in (-1, 1):
    for z in (0.345, 0.425):
        v4.rod(f"coil_standoff_{sx}_{z}", (sx*0.103,0.1728,z), (sx*0.103,0.1755,z), 0.0032, material=steel_dark, body=base)
# Back upper switch/identification plate with raised indicator and compact molded marks.
v4.box("hot_tank_switch_plate", (0.112, 0.004, 0.072), (0.084, 0.1715, 0.820), bevel=0.004, material=steel_dark, body=base)
v4.box("hot_tank_switch_face", (0.060, 0.002, 0.034), (0.073, 0.1742, 0.820), bevel=0.002, material=black, body=base)
v4.box("hot_tank_toggle", (0.018, 0.008, 0.022), (0.073, 0.179, 0.820), bevel=0.003, material=red, body=base)
for i in range(4):
    v4.box(f"hot_tank_label_bar_{i}", (0.012-0.001*i, 0.001, 0.002), (0.119, 0.174, 0.807+i*0.004), bevel=0.0005, material=label, body=base)
for sx in (-1, 1):
    for z in (0.795, 0.845):
        v4.cylinder(f"switch_plate_screw_{sx}_{z}", 0.0023, 0.0015, (0.084+sx*0.047,0.174,z), rot=(math.pi/2,0,0), material=steel, body=base, bevel=0.0003, segments=20)
# Power lead leaves a rear grommet and loops down to a molded plug.
v4.torus("power_cord_grommet", 0.009, 0.0025, loc=(0.122,0.171,0.205), rot=(math.pi/2,0,0), material=black_rubber, body=base)
v4.tube("power_cord", [(0.122,0.171,0.205),(0.122,0.181,0.198),(0.139,0.192,0.172),
                         (0.163,0.198,0.133),(0.188,0.198,0.103),(0.205,0.198,0.094)],
        0.0042, material=black_rubber, body=base, resolution=10)
v4.box("power_plug", (0.036,0.031,0.044), (0.221,0.198,0.094), bevel=0.006, material=black_rubber, body=base)
for z in (0.084,0.104):
    v4.rod(f"plug_prong_{z}", (0.237,0.198,z), (0.248,0.198,z), 0.0025, material=steel, body=base)

# Top deck with a centered receiving socket for the inverted bottle neck.
v4.box("top_deck", (0.375, 0.35, 0.027), (0, 0, 0.927), bevel=0.012, material=white_trim, body=base)
v4.cylinder("bottle_seat_dark_well", 0.071, 0.007, (0, 0, 0.943), material=black, body=base, bevel=0.001)
v4.torus("bottle_cradle_rim", 0.054, 0.006, loc=(0, 0, 0.954), material=steel_dark, body=base)
v4.cylinder("neck_support_boss", 0.037, 0.024, (0, 0, 0.958), material=black, body=base, bevel=0.002)

# Inverted 5-gallon PET jug: broad 240 mm barrel, molded shoulder, short neck,
# three circumferential strengthening ribs, and a recessed handling band.
v4.lathe("inverted_bottle", [
    (0,0.958),(0.030,0.958),(0.034,0.961),(0.036,0.966),(0.036,0.974),
    (0.032,0.978),(0.032,0.991),(0.031,1.002),(0.043,1.014),(0.075,1.039),
    (0.105,1.065),(0.119,1.087),(0.123,1.105),
    (0.123,1.118),(0.119,1.122),(0.123,1.127),(0.123,1.163),
    (0.119,1.167),(0.123,1.172),(0.123,1.207),
    (0.119,1.211),(0.123,1.216),(0.123,1.236),
    (0.119,1.241),(0.115,1.247),(0.115,1.273),(0.119,1.279),(0.123,1.284),
    (0.123,1.346),(0.120,1.371),(0.111,1.391),(0.091,1.410),
    (0.061,1.425),(0.029,1.434),(0,1.437)], material=water, body=base)
# Cap collar, neck threads and molded shoulder/rib highlights.
for z, major, minor in ((0.965,0.035,0.0020),(0.974,0.035,0.0014),(0.985,0.032,0.0012),
                        (1.020,0.049,0.0013),(1.070,0.110,0.0012),
                        (1.125,0.122,0.0010),(1.170,0.122,0.0010),(1.214,0.122,0.0010),
                        (1.283,0.121,0.0012),(1.375,0.118,0.0011)):
    v4.torus(f"bottle_mold_ring_{z}", major, minor, loc=(0,0,z), material=water_band, body=base)
# A shallow band at the grip recess and a faint waterline in the upper barrel.
v4.torus("grip_band_lower_edge", 0.1157, 0.0011, loc=(0,0,1.249), material=water_band, body=base)
v4.torus("grip_band_upper_edge", 0.1157, 0.0011, loc=(0,0,1.271), material=water_band, body=base)
v4.torus("waterline_band", 0.1231, 0.0011, loc=(0,0,1.314), material=water_band, body=base)

# Faucet fascia sits slightly proud of the front shell. Rounded black insert and two chrome outlets.
# Hidden rear brace anchors the fixed outlet feeds behind the complete lever sweep.
v4.box("faucet_internal_crossbrace", (0.300, 0.020, 0.018), (0, -0.122, 0.651), bevel=0.003, material=black, body=base)
def fascia_frame(prefix, width, depth, height, y, z, side_w, rail_h, material, bevel, split_bottom=False):
    # Open center leaves a clear sweep for the cup levers while retaining a
    # continuous raised border around the valve field.
    side_x = width/2-side_w/2
    rail_w = width-2*side_w
    rail_z = height/2-rail_h/2
    for sx in (-1,1):
        v4.box(f"{prefix}_side_{sx}", (side_w,depth,height), (sx*side_x,y,z), bevel=bevel, material=material, body=base)
    for sz in (-1,1):
        rz = z+sz*rail_z
        if split_bottom and sz < 0:
            for k, (seg_w, seg_x) in enumerate(((0.027,-0.1235),(0.027,0.1235))):
                v4.box(f"{prefix}_lower_rail_{k}", (seg_w,depth,rail_h), (seg_x,y,rz), bevel=bevel, material=material, body=base)
        else:
            v4.box(f"{prefix}_rail_{sz}", (rail_w,depth,rail_h), (0,y,rz), bevel=bevel, material=material, body=base)

fascia_frame("faucet_panel", 0.305, 0.018, 0.240, -0.181, 0.685, 0.022, 0.014, black, 0.003)
fascia_frame("fascia_inner_bezel", 0.274, 0.006, 0.201, -0.1925, 0.682, 0.022, 0.012, steel_dark, 0.002, split_bottom=True)
fascia_frame("fascia_inset", 0.262, 0.004, 0.190, -0.1945, 0.682, 0.020, 0.008, black, 0.0015, split_bottom=True)

# Two recessed nozzle wells, nozzles extend forward/down into cup zone.
for i, (x, color, temp) in enumerate(((-0.083, blue, "COLD"), (0.083, red, "HOT"))):
    # Faucet valve panel boss around pivot
    v4.torus(f"valve_mount_bezel_{temp}", 0.021, 0.001, loc=(x,-0.207,0.688), rot=(0,math.pi/2,0), material=steel, body=base)
    sx = -1 if x < 0 else 1
    v4.rod(f"valve_mount_support_{temp}", (x,-0.207,0.709), (x+sx*0.039,-0.1925,0.738), 0.0025, material=steel_dark, body=base)
    # Small discharge tube is anchored to fascia; aligned downward.
    nozzle_x = -0.047 if x < 0 else 0.047
    v4.cylinder(f"spout_root_{temp}", 0.010, 0.022, (nozzle_x, -0.209, 0.651), rot=(math.pi/2,0,0), material=steel, body=base, bevel=0.001)
    v4.tube(f"spout_feed_support_{temp}", [(nozzle_x,-0.201,0.651),(nozzle_x,-0.168,0.651),(nozzle_x,-0.132,0.651)],
            0.0035, material=steel_dark, body=base, resolution=8)
    v4.cylinder(f"spout_drop_{temp}", 0.0075, 0.035, (nozzle_x, -0.215, 0.6275), material=steel, bevel=0.001, body=base)
    v4.cylinder(f"spout_nozzle_{temp}", 0.0075, 0.015, (nozzle_x, -0.215, 0.605), radius_top=0.006, material=steel, body=base, bevel=0.0007)
    v4.cylinder(f"aerator_{temp}", 0.007, 0.008, (nozzle_x, -0.215, 0.596), material=steel_dark, body=base, bevel=0.0008)
    v4.cylinder(f"spout_tip_{temp}", 0.0045, 0.008, (nozzle_x, -0.215, 0.5955), material=black, body=base, bevel=0.0005)
    # Temperature legend at upper edge and a fine divider line.
    # Three molded bars form a compact temperature legend without floating lettering.
    for n in range(3):
        v4.box(f"legend_bar_{temp}_{n}", (0.018-0.003*n, 0.004, 0.0022), (x, -0.1945, 0.773+n*0.0035), bevel=0.0005, material=color, body=base)

# Cup recess/back wall and wide drip tray mounted under the faucets.
v4.box("cup_splash_wall", (0.258, 0.012, 0.075), (0, -0.181, 0.555), bevel=0.007, material=black, body=base)
# Small embossed water-drop mark.
v4.box("drop_icon_bar", (0.018, 0.001, 0.002), (0, -0.188, 0.548), bevel=0.0005, material=label, body=base)
# Brushed tray pan and bright rolled rim.
v4.box("drip_tray_pan", (0.294, 0.119, 0.014), (0, -0.231, 0.493), bevel=0.006, material=steel_dark, body=base)
v4.box("drip_tray_insert", (0.268, 0.093, 0.004), (0, -0.232, 0.502), bevel=0.004, material=black, body=base)
# Tray rim is made as four clean edge bars that meet without overlap.
v4.box("tray_rim_left", (0.008, 0.119, 0.012), (-0.143, -0.231, 0.505), bevel=0.003, material=steel, body=base)
v4.box("tray_rim_right", (0.008, 0.119, 0.012), (0.143, -0.231, 0.505), bevel=0.003, material=steel, body=base)
v4.box("tray_rim_front", (0.278, 0.008, 0.012), (0, -0.2865, 0.505), bevel=0.003, material=steel, body=base)
v4.box("tray_rim_back", (0.278, 0.008, 0.012), (0, -0.1755, 0.505), bevel=0.003, material=steel, body=base)
# Short hidden-in-shadow supports and front grab lips make the tray look serviceable.
for sx in (-1, 1):
    v4.box(f"tray_support_rail_{sx}", (0.014, 0.086, 0.010), (sx*0.119, -0.218, 0.481), bevel=0.002, material=steel_dark, body=base)
    v4.box(f"tray_grab_lip_{sx}", (0.012, 0.006, 0.004), (sx*0.129, -0.292, 0.509), bevel=0.0015, material=steel, body=base)
# Drain holes, dark inserts meet top of tray surface.
for ix in range(7):
    for iy in range(3):
        x = (ix-3)*0.032
        y = -0.254 + iy*0.022
        v4.cylinder(f"tray_drain_{ix}_{iy}", 0.0052, 0.0015, (x,y,0.5048), material=black_rubber, body=base, bevel=0.0003, segments=24)

# Brand badge, tiny status lamp and decorative seam above the lower hatch.
v4.box("brand_badge", (0.093, 0.004, 0.027), (0, -0.173, 0.843), bevel=0.006, material=steel_dark, body=base)
for i in range(4):
    v4.box(f"brand_mark_{i}", (0.006, 0.001, 0.013), ((i-1.5)*0.011, -0.1757, 0.843), bevel=0.0015, material=label, body=base)
v4.sphere("status_lamp", 0.004, (0.073, -0.174, 0.842), material=blue_light, body=base)
v4.box("service_panel_seam", (0.292, 0.0015, 0.003), (0, -0.171, 0.385), bevel=0.001, material=white_trim, body=base)

# Two faucet cartridges pivot on the faucet fascia. Each matching paddle is separately hinged to its valve.
def make_valve(label_name, x, color, lower_angle, upper_angle):
    valve = v4.body(label_name.lower()+"_valve")
    # Cartridge collar, spool and colored index cap are all part of the rotating valve body.
    v4.cylinder(f"{label_name}_valve_hub", 0.019, 0.024, (x, -0.207, 0.688), rot=(0,math.pi/2,0), material=steel, body=valve, bevel=0.002)
    v4.cylinder(f"{label_name}_valve_spool", 0.013, 0.022, (x, -0.237, 0.688), rot=(math.pi/2,0,0), material=black, body=valve, bevel=0.0015)
    v4.cylinder(f"{label_name}_color_cap", 0.011, 0.006, (x, -0.243, 0.688), rot=(math.pi/2,0,0), material=color, body=valve, bevel=0.001)
    v4.torus(f"{label_name}_hub_rim", 0.010, 0.0014, loc=(x,-0.247,0.688), rot=(math.pi/2,0,0), material=steel_dark, body=valve)
    v4.joint(label_name.lower()+"_valve_hinge", "revolute", base, valve,
             origin=(x,-0.207,0.688), axis=(1,0,0), limits=(lower_angle,upper_angle))

    lever = v4.body(label_name.lower()+"_lever")
    # A hinge clevis cheeks and transverse pin straddle the valve's upper actuator ear.
    # The paddle is a compact, forward-projecting molded press tab ending above the tray.
    v4.box(f"{label_name}_paddle", (0.048, 0.020, 0.108), (x, -0.239, 0.628), bevel=0.010, material=color, body=lever)
    v4.box(f"{label_name}_paddle_neck", (0.026, 0.024, 0.032), (x, -0.236, 0.683), bevel=0.008, material=color, body=lever)
    # Soft horizontal ribs on the forward face, set flush into paddle.
    for k in range(4):
        z = 0.602 + k*0.014
        v4.box(f"{label_name}_grip_rib_{k}", (0.034, 0.002, 0.003), (x, -0.250, z), bevel=0.0014, material=white_trim, body=lever)
    if label_name == "HOT":
        # Safety press button is centered on the hot paddle neck for child-resistant use.
        v4.cylinder("hot_child_safety_button", 0.0055, 0.004, (x,-0.247,0.690), rot=(math.pi/2,0,0), material=child_safe, body=lever, bevel=0.0008)
    # Pivot barrel is located at the top of lever and attaches through the valve yoke.
    v4.cylinder(f"{label_name}_lever_pivot_boss", 0.009, 0.052, (x,-0.223,0.707), rot=(0,math.pi/2,0), material=steel_dark, body=lever, bevel=0.001)
    v4.cylinder(f"{label_name}_lever_pin_cap", 0.006, 0.003, (x-0.027,-0.223,0.707), rot=(0,math.pi/2,0), material=steel, body=lever, bevel=0.0005)
    v4.joint(label_name.lower()+"_lever_hinge", "revolute", valve, lever,
             origin=(x,-0.223,0.707), axis=(1,0,0), limits=(-math.radians(18), math.radians(38)))

make_valve("COLD", -0.083, blue, -math.radians(12), math.radians(12))
make_valve("HOT", 0.083, red, -math.radians(12), math.radians(12))

# Save in neutral rest pose.
v4.save(os.path.join(os.getcwd(), "asset.blend"))
print("saved asset.blend")
