"""Detailed exposed thermostatic shower mixer with rotary controls and diverter."""
import math
import os
import sys

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

v4.reset()

# Materials: polished chrome with dark machined details and familiar temperature cues.
chrome = v4.metal("polished_chrome", color=(0.78, 0.81, 0.83), roughness=0.2, brushed=False)
brushed = v4.metal("satin_chrome", color=(0.62, 0.66, 0.68), roughness=0.31, brushed=True)
dark_chrome = v4.metal("dark_chrome", color=(0.20, 0.23, 0.24), roughness=0.32, brushed=True)
black = v4.rubber("black_elastomer", (0.055, 0.060, 0.062))
white = v4.mat("warm_white_enamel", (0.91, 0.91, 0.87), roughness=0.35)
red = v4.mat("hot_red_enamel", (0.78, 0.075, 0.045), roughness=0.36)
blue = v4.mat("cold_blue_enamel", (0.035, 0.23, 0.62), roughness=0.36)

# Main bar axis is X. The face points toward -Y; Z=1.12 m is a typical installed height.
Z = 1.12
root = v4.body("mixer_body")

# Barrel and end shoulders. Each turned part is oriented along the bar axis.
v4.cylinder("thermostatic_barrel", 0.0215, 0.222, (0, 0.002, Z), rot=(0, math.pi/2, 0),
            material=brushed, body=root)
for side, x in (("left", -0.119), ("right", 0.119)):
    v4.lathe(f"{side}_barrel_shoulder", [(0, 0), (0.023, 0), (0.026, 0.003),
             (0.026, 0.008), (0.022, 0.011), (0, 0.011)],
             loc=(x, 0.002, Z), rot=(0, math.pi/2 if x < 0 else -math.pi/2, 0), material=chrome, body=root)
    v4.torus(f"{side}_shoulder_seam", 0.0232, 0.00065, loc=(x + (-0.008 if x < 0 else 0.008), 0.002, Z),
             rot=(0, math.pi/2, 0), material=dark_chrome, body=root)

# Wall fixing escutcheons, eccentric shanks and unions. The two flanges sit against the wall.
for side, x in (("left", -0.135), ("right", 0.135)):
    v4.lathe(f"{side}_mounting_flange", [(0, 0), (0.039, 0), (0.043, 0.002),
             (0.043, 0.007), (0.039, 0.009), (0, 0.009)],
             loc=(x, 0.035, Z), rot=(math.pi/2, 0, 0), material=chrome, body=root)
    v4.torus(f"{side}_flange_rim", 0.040, 0.001, loc=(x, 0.029, Z), rot=(math.pi/2, 0, 0),
             material=brushed, body=root)
    # Short eccentric connector from the flange to the mixer union, with a hexagonal nut.
    v4.rod(f"{side}_eccentric_shank", (x, 0.039, Z), (x, -0.001, Z), 0.012,
           material=brushed, body=root)
    v4.cylinder(f"{side}_union_nut", 0.018, 0.014, (x, 0.004, Z), rot=(math.pi/2, 0, 0),
                segments=6, material=brushed, body=root)
    v4.cylinder(f"{side}_union_washer", 0.015, 0.003, (x, -0.0045, Z), rot=(math.pi/2, 0, 0),
                material=brushed, body=root)
    # A narrow dark shadow line separates the escutcheon from the S-union shoulder.
    v4.torus(f"{side}_union_shadow_seam", 0.013, 0.0008, loc=(x, 0.0387, Z),
             rot=(math.pi/2, 0, 0), material=dark_chrome, body=root)

# Slightly flattened front land gives the long barrel a machined, less generic profile.
v4.box("barrel_front_flat", (0.181, 0.006, 0.026), (0, -0.0175, Z),
       bevel=0.004, material=brushed, body=root)

# Cartridge bezels sit behind each dial; a fine dark ring reads as a recessed joint line.
for side, x in (("temperature", -0.103), ("flow", 0.103)):
    v4.cylinder(f"{side}_cartridge_bezel", 0.030, 0.005, (x, -0.017, Z),
                rot=(math.pi/2, 0, 0), material=chrome, body=root)
    v4.torus(f"{side}_cartridge_recess", 0.0275, 0.00075, loc=(x, -0.0202, Z),
             rot=(math.pi/2, 0, 0), material=dark_chrome, body=root)

# Small color temperature scale on the flattened land between the temperature dial and bar.
for i in range(7):
    x = -0.080 + i*0.005
    mat = blue if i < 2 else red if i > 4 else white
    v4.rod(f"bar_temperature_tick_{i:02d}", (x, -0.022, Z+0.006),
           (x, -0.022, Z+0.011 if i in (0, 3, 6) else Z+0.009), 0.0007,
           material=mat, body=root)

# Outlet selector plumbing: upper overhead riser outlet and lower hand-shower hose outlet.
# Both bosses are fixed to the bar; no swivel spout is present.
for nm, sign in (("overhead_outlet", 1), ("handshower_outlet", -1)):
    v4.cylinder(f"{nm}_boss", 0.014, 0.020, (0, 0.002, Z + sign*0.022),
                rot=(0, 0, 0), material=brushed, body=root)
    v4.lathe(f"{nm}_union", [(0, 0), (0.010, 0), (0.012, 0.002), (0.012, 0.010),
             (0.009, 0.013), (0, 0.013)], loc=(0, 0.002, Z + sign*0.031),
             rot=((0 if sign > 0 else math.pi), 0, 0), material=chrome, body=root)
    v4.torus(f"{nm}_seal_ring", 0.009, 0.0012, loc=(0, 0.002, Z + sign*0.044),
             rot=((0 if sign > 0 else math.pi), 0, 0), material=dark_chrome, body=root)
    # Raised thread crests on the exposed 1/2-inch connector neck.
    for thread_i, distance in enumerate((0.034, 0.037, 0.040)):
        v4.torus(f"{nm}_thread_{thread_i}", 0.0102, 0.00055,
                 loc=(0, 0.002, Z + sign*distance), material=brushed, body=root)

# The hose side has a broad hexagonal swivel nut below its threaded neck.
v4.cylinder("handshower_hose_nut", 0.014, 0.009, (0, 0.002, Z-0.049),
            segments=6, material=chrome, body=root)
v4.torus("handshower_nut_shadow", 0.010, 0.0008, loc=(0, 0.002, Z-0.054),
         material=dark_chrome, body=root)
v4.cylinder("handshower_hose_tail", 0.009, 0.013, (0, 0.002, Z-0.0605),
            material=brushed, body=root)

# Left thermostatic control, on the fixed thermostatic cartridge.
temp = v4.body("temperature_knob")
temp_pivot = (-0.103, -0.020, Z)
v4.lathe("temperature_knob_shell", [(0, 0), (0.021, 0), (0.026, 0.003), (0.028, 0.008),
         (0.028, 0.031), (0.026, 0.036), (0.022, 0.039), (0, 0.039)],
         loc=temp_pivot, rot=(math.pi/2, 0, 0), material=chrome, body=temp)
v4.lathe("temperature_grip_band", [(0, 0), (0.0278, 0), (0.0282, 0.002), (0.0282, 0.012),
         (0.0278, 0.014), (0, 0.014)], loc=(temp_pivot[0], temp_pivot[1]-0.016, Z),
         rot=(math.pi/2, 0, 0), material=black, body=temp)
# Knurling ribs run along the barrel axis, slightly overlapping the dark grip band.
for i in range(32):
    a = 2*math.pi*i/32
    x = temp_pivot[0] + 0.0278*math.cos(a)
    z = Z + 0.0278*math.sin(a)
    v4.rod(f"temperature_knurl_{i:02d}", (x, -0.031, z), (x, -0.043, z), 0.00075,
           material=brushed, body=temp)
v4.cylinder("temperature_end_cap", 0.022, 0.0015, (temp_pivot[0], -0.060, Z),
            rot=(math.pi/2, 0, 0), material=brushed, body=temp)
# A crisp white index line makes rotation easy to read against the satin end cap.
v4.rod("temperature_index", (temp_pivot[0], -0.061, Z+0.012),
       (temp_pivot[0], -0.061, Z+0.020), 0.0012, material=white, body=temp)
# Raised 38-degree safety stop button on the front face, with a chrome bezel and red center.
v4.cylinder("temperature_safety_bezel", 0.0046, 0.0025,
            (temp_pivot[0]+0.014, -0.061, Z-0.008), rot=(math.pi/2, 0, 0),
            material=chrome, body=temp)
v4.cylinder("temperature_safety_button", 0.0029, 0.002,
            (temp_pivot[0]+0.014, -0.063, Z-0.008), rot=(math.pi/2, 0, 0),
            material=red, body=temp)
# Temperature scale marks sit directly on the exposed outer shoulder of the knob.
for i in range(11):
    a = math.radians(-140 + i*28)
    x = temp_pivot[0] + 0.0279*math.cos(a)
    z = Z + 0.0279*math.sin(a)
    material = blue if i < 4 else red if i > 6 else white
    v4.sphere(f"temperature_scale_{i:02d}", 0.0015, (x, -0.031, z), segments=12,
              material=material, body=temp)
v4.joint("temperature_cartridge", "continuous", root, temp, temp_pivot, axis=(0, -1, 0))

# Right flow control, on the fixed on/off cartridge.
flow = v4.body("flow_knob")
flow_pivot = (0.103, -0.020, Z)
v4.lathe("flow_knob_shell", [(0, 0), (0.021, 0), (0.026, 0.003), (0.028, 0.008),
         (0.028, 0.031), (0.026, 0.036), (0.022, 0.039), (0, 0.039)],
         loc=flow_pivot, rot=(math.pi/2, 0, 0), material=chrome, body=flow)
v4.lathe("flow_grip_band", [(0, 0), (0.0278, 0), (0.0282, 0.002), (0.0282, 0.012),
         (0.0278, 0.014), (0, 0)], loc=(flow_pivot[0], flow_pivot[1]-0.016, Z),
         rot=(math.pi/2, 0, 0), material=black, body=flow)
for i in range(32):
    a = 2*math.pi*i/32
    x = flow_pivot[0] + 0.0278*math.cos(a)
    z = Z + 0.0278*math.sin(a)
    v4.rod(f"flow_knurl_{i:02d}", (x, -0.031, z), (x, -0.043, z), 0.00075,
           material=brushed, body=flow)
v4.cylinder("flow_end_cap", 0.022, 0.0015, (flow_pivot[0], -0.060, Z),
            rot=(math.pi/2, 0, 0), material=brushed, body=flow)
v4.rod("flow_index", (flow_pivot[0], -0.061, Z+0.012), (flow_pivot[0], -0.061, Z+0.020), 0.0012,
       material=white, body=flow)
v4.joint("flow_cartridge", "continuous", root, flow, flow_pivot, axis=(0, -1, 0))

# Central diverter selector with a proper circular rosette and a solid flat paddle.
lever = v4.body("diverter_lever")
lever_pivot = (0, -0.0255, Z)
v4.cylinder("diverter_rosette", 0.016, 0.010, (0, -0.0205, Z),
            rot=(math.pi/2, 0, 0), material=chrome, body=root)
v4.torus("diverter_rosette_rim", 0.0135, 0.001, (0, -0.0255, Z),
         rot=(math.pi/2, 0, 0), material=brushed, body=root)
v4.lathe("diverter_hub", [(0, 0), (0.010, 0), (0.012, 0.002), (0.012, 0.009),
         (0.009, 0.014), (0, 0.014)], loc=lever_pivot, rot=(math.pi/2, 0, 0),
         material=chrome, body=lever)
# Rounded octagonal outline, extruded into a stout paddle that grows directly from the hub.
paddle_outline = [(0.000, -0.006), (0.004, -0.009), (0.033, -0.009), (0.039, -0.005),
                  (0.039, 0.005), (0.033, 0.009), (0.004, 0.009), (0.000, 0.006)]
v4.extrude("diverter_paddle", paddle_outline, 0.005, loc=(0, -0.034, Z),
           rot=(math.pi/2, 0, 0), bevel=0.0025, material=chrome, body=lever)
# Thin satin inlay is seated into the paddle's visible front face.
v4.box("diverter_paddle_inlay", (0.021, 0.0008, 0.0015),
       (0.021, -0.0394, Z), bevel=0.0004, material=brushed, body=lever)
v4.joint("diverter_pivot", "revolute", root, lever, lever_pivot,
         axis=(0, -1, 0), limits=(-math.radians(35), math.radians(35)))

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