"""Detailed articulated portable evaporative cooler; run with Blender Python."""
import math, os, sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Palette
shell = v4.plastic("warm_ivory_molded_plastic", (0.78, 0.79, 0.75), roughness=0.31, texture=0.10)
shell_dark = v4.plastic("cooler_grey_trim", (0.29, 0.33, 0.34), roughness=0.34, texture=0.06)
black = v4.plastic("charcoal_grille", (0.035, 0.047, 0.052), roughness=0.32, texture=0.02)
pad = v4.fabric("blue_grey_honeycomb_pad", (0.24, 0.31, 0.32))
rubber = v4.rubber("caster_tire", (0.045, 0.052, 0.052))
steel = v4.metal("zinc_plated_steel", (0.63, 0.67, 0.67), roughness=0.29, brushed=False)
darksteel = v4.metal("dark_fan_steel", (0.12, 0.16, 0.17), roughness=0.35)
blue = v4.mat("water_level_window", (0.08, 0.25, 0.30), roughness=0.22, metallic=0.12)
display = v4.emissive("green_led", (0.22, 0.92, 0.56), strength=2.0)
white = v4.mat("white_print", (0.91, 0.91, 0.84), roughness=0.62)
accent = v4.plastic("knob_graphite", (0.095, 0.11, 0.115), roughness=0.28, texture=0.06)

# Envelope: cabinet 480 x 360 x 780 mm, from z=.09 to .87; total incl. casters=.91m.
W, D = 0.48, 0.36
cab = v4.body("cabinet")

# Main molded shell, with broad rounded corners and a stepped base / shoulder.
v4.box("main_shell", (W, D, 0.70), (0, 0.006, 0.50), bevel=0.035, material=shell, body=cab)
v4.box("upper_cap", (W+0.012, D+0.008, 0.075), (0, 0.003, 0.888), bevel=0.022, material=shell, body=cab)
v4.box("lower_skirt", (W+0.014, D+0.012, 0.095), (0, 0.004, 0.145), bevel=0.018, material=shell_dark, body=cab)
v4.box("top_recess", (0.30, 0.20, 0.003), (0, 0.01, 0.927), bevel=0.014, material=shell_dark, body=cab)
v4.box("top_grate", (0.26, 0.15, 0.003), (0, 0.01, 0.930), bevel=0.009, material=black, body=cab)
for i in range(9):
    x=-0.112 + i*0.028
    v4.box(f"top_vent_{i}", (0.010, 0.13, 0.003), (x, 0.01, 0.933), bevel=0.003, material=shell_dark, body=cab)

# Side cooling-pad cartridges, visible through framed intake openings on both sides.
for s, label in ((-1,"left"),(1,"right")):
    x=s*0.240
    v4.box(f"{label}_pad_inset", (0.006, 0.260, 0.395), (x, 0.015, 0.555), bevel=0.015, material=pad, body=cab)
    # Fine molded bars protect the pad but leave most of the media visible.
    for yidx in range(11):
        y=-0.105+yidx*0.024
        v4.box(f"{label}_pad_flute_{yidx}", (0.008, 0.0035, 0.365), (s*0.245, y, 0.555), bevel=0.0012, material=shell_dark, body=cab)
    for z in (0.345,0.765):
        v4.box(f"{label}_pad_rail_{z}", (0.012,0.285,0.014), (s*0.244,0.015,z), bevel=0.006, material=shell, body=cab)
    # Intake slot bands above and below the media cartridge.
    for band_z in (0.315,0.790):
        for i in range(7):
            yy=-0.09+i*0.028
            v4.box(f"{label}_intake_{band_z}_{i}", (0.006,0.018,0.004), (s*0.241,yy,band_z), bevel=0.0018, material=black, body=cab)

# Rear exhaust/intake pattern and service-panel seam.
v4.box("rear_service_seam", (0.39,0.002,0.43), (0,0.187,0.51), bevel=0.012, material=shell_dark, body=cab)
v4.box("rear_panel", (0.37,0.008,0.41), (0,0.183,0.51), bevel=0.01, material=shell, body=cab)
for i in range(13):
    x=-0.15+i*0.025
    v4.box(f"rear_grille_slot_{i}", (0.012,0.004,0.32), (x,0.178,0.52), bevel=0.005, material=black, body=cab)
for x in (-0.17,0.17):
    for z in (0.32,0.70):
        v4.cylinder(f"rear_screw_{x}_{z}",0.0035,0.002,(x,0.176,z),rot=(math.pi/2,0,0),segments=16,material=steel,body=cab)

# Front outlet: bezel and recessed dark plenum; frame is fixed to cabinet.
v4.box("outlet_bezel_outer", (0.365,0.030,0.342), (0,-0.190,0.600), bevel=0.035, material=shell_dark, body=cab)
v4.box("outlet_recess", (0.326,0.006,0.303), (0,-0.207,0.600), bevel=0.025, material=black, body=cab)
# Deep inner duct and motor mount behind the spinning rotor.
v4.cylinder("fan_shroud",0.137,0.014,(0,-0.101,0.600),rot=(math.pi/2,0,0),material=black,body=cab)
v4.cylinder("motor_mount",0.042,0.050,(0,-0.075,0.600),rot=(math.pi/2,0,0),material=darksteel,body=cab)
for ang in range(0,360,45):
    a=math.radians(ang)
    v4.rod(f"motor_stator_{ang}",(0.036*math.cos(a),-0.11,0.6+0.036*math.sin(a)),(0.115*math.cos(a),-0.11,0.6+0.115*math.sin(a)),0.003,material=darksteel,body=cab)

# Fan rotor; axis is Y and lies inside the duct, set back behind the louvres.
fan=v4.body("fan")
v4.cylinder("fan_hub",0.034,0.034,(0,-0.131,0.600),rot=(math.pi/2,0,0),material=steel,body=fan)
v4.cylinder("fan_hub_cap",0.018,0.010,(0,-0.153,0.600),rot=(math.pi/2,0,0),material=shell_dark,body=fan)
for i in range(5):
    a=2*math.pi*i/5
    # Broad, slightly swept paddles on a compact 280 mm axial fan.
    cx=0.081*math.cos(a); cz=0.600+0.081*math.sin(a)
    v4.box(f"fan_blade_{i}",(0.142,0.012,0.045),(cx,-0.132,cz),rot=(0,-a,0),bevel=0.008,material=darksteel,body=fan)
v4.torus("hub_ring",0.025,0.0025,(0,-0.148,0.600),rot=(math.pi/2,0,0),material=steel,body=fan)
v4.joint("fan_spin","continuous",cab,fan,origin=(0,-0.131,0.600),axis=(0,1,0))

# Eight individual horizontal vanes on the recessed outlet frame.
for i in range(8):
    z=0.476+i*0.0355
    b=v4.body(f"louvre_{i}")
    v4.box(f"louvre_blade_{i}",(0.301,0.014,0.026),(0,-0.216,z),bevel=0.007,material=shell,body=b)
    # Pivot barrels sit at each end of each vane on the same horizontal pin line.
    for s in (-1,1):
        v4.cylinder(f"louvre_pin_{i}_{s}",0.0045,0.014,(s*0.151,-0.216,z),rot=(0,math.pi/2,0),segments=20,material=steel,body=b)
    v4.joint(f"louvre_hinge_{i}","revolute",cab,b,origin=(0,-0.216,z),axis=(1,0,0),limits=(-math.radians(30),math.radians(30)))

# Lower pull-out reservoir. Face sits flush in its opening at rest; tub, grip and level window travel together.
tank=v4.body("water_tank")
tz=0.205
v4.box("tank_front",(0.385,0.026,0.158),(0,-0.190,tz),bevel=0.016,material=shell,body=tank)
v4.box("tank_basin",(0.350,0.224,0.105),(0,-0.065,tz-0.018),bevel=0.012,material=shell_dark,body=tank)
v4.box("tank_water_level_window",(0.034,0.004,0.080),(0.142,-0.205,tz-0.010),bevel=0.006,material=blue,body=tank)
for k in range(4):
    z=tz-0.040+k*0.020
    v4.box(f"tank_level_mark_{k}",(0.014,0.001,0.0015),(0.142,-0.208,z),bevel=0.0005,material=white,body=tank)
# Pull grip recess appears as a dark inset pocket and a rounded bar.
v4.box("tank_grip_pocket",(0.105,0.004,0.034),(0,-0.205,tz+0.045),bevel=0.012,material=shell_dark,body=tank)
v4.box("tank_grip_inner",(0.072,0.010,0.012),(0,-0.211,tz+0.047),bevel=0.006,material=black,body=tank)
v4.cylinder("tank_drain_plug",0.009,0.004,(0.158,-0.18,tz-0.050),rot=(math.pi/2,0,0),material=accent,body=tank)
v4.joint("tank_slide","prismatic",cab,tank,origin=(0,-0.190,tz),axis=(0,-1,0),limits=(0,0.20))
# Visible slide rails fixed to the body, aligned with the tub sides.
for s in (-1,1):
    v4.box(f"tank_runner_{s}",(0.012,0.245,0.012),(s*0.192,-0.065,tz-0.071),bevel=0.003,material=steel,body=cab)

# Control fascia on upper-right front, two turned rotary controls and membrane buttons.
v4.box("control_fascia",(0.152,0.012,0.184),(0.137,-0.184,0.800),bevel=0.018,material=black,body=cab)
v4.box("display_bezel",(0.080,0.006,0.032),(0.137,-0.193,0.861),bevel=0.006,material=shell_dark,body=cab)
v4.box("display_lens",(0.064,0.003,0.018),(0.137,-0.198,0.861),bevel=0.003,material=display,body=cab)
# Segmented display and printed function legends represented by connected-looking strokes,
# avoiding free-standing glyph islands on the molded fascia.
for i in range(4):
    x=0.119+i*0.012
    for z in (0.855,0.867):
        v4.box(f"display_segment_h_{i}_{z}",(0.007,0.001,0.0012),(x,-0.200,z),bevel=0.0004,material=black,body=cab)
    for z in (0.858,0.864):
        v4.box(f"display_segment_v_{i}_{z}",(0.0012,0.001,0.005),(x+0.003,-0.200,z),bevel=0.0004,material=black,body=cab)
for i in range(9):
    v4.box(f"control_legend_stroke_{i}",(0.006,0.001,0.0012),(0.105+i*0.008,-0.1905,0.824),bevel=0.0004,material=white,body=cab)
for idx,x in enumerate((0.106,0.169)):
    # Raised printed tick marks around each selector.
    for j in range(5):
        a=math.radians(-60+j*30)
        v4.box(f"knob_tick_{idx}_{j}",(0.002,0.002,0.010),(x+0.026*math.sin(a),-0.1905,0.807+0.026*math.cos(a)),bevel=0.0008,material=white,body=cab)
    knob=v4.body(f"control_knob_{idx}")
    v4.cylinder(f"knob_base_{idx}",0.024,0.009,(x,-0.1945,0.807),rot=(math.pi/2,0,0),material=shell_dark,body=knob)
    v4.cylinder(f"knob_cap_{idx}",0.019,0.018,(x,-0.2085,0.807),rot=(math.pi/2,0,0),material=accent,body=knob)
    v4.torus(f"knob_rim_{idx}",0.017,0.0015,(x,-0.2195,0.807),rot=(math.pi/2,0,0),material=steel,body=knob)
    v4.box(f"knob_pointer_{idx}",(0.003,0.002,0.010),(x,-0.2205,0.817),bevel=0.001,material=white,body=knob)
    v4.joint(f"knob_turn_{idx}","continuous",cab,knob,origin=(x,-0.2035,0.807),axis=(0,1,0))
for i in range(3):
    x=0.103+i*0.034
    v4.box(f"membrane_button_{i}",(0.022,0.006,0.014),(x,-0.193,0.735),bevel=0.004,material=shell,body=cab)
    # Small centered indicator slot rather than individually floating lettering.
    v4.box(f"button_glyph_{i}",(0.004,0.001,0.006),(x,-0.1965,0.735),bevel=0.001,material=black,body=cab)

# Molded front details: vertical edge ribs and mounting screws around the fascia and outlet.
# Fine front panel joint lines are molded into the larger shell silhouette.
for x in (-0.166,0.166):
    for z in (0.454,0.746):
        v4.cylinder(f"outlet_fastener_{x}_{z}",0.0035,0.002,(x,-0.209,z),rot=(math.pi/2,0,0),segments=16,material=steel,body=cab)

# Four compact 45 mm swivel casters. Each fork swivels at a vertical kingpin and each wheel rolls on X.
for ix,sx in enumerate((-1,1)):
    for iy,sy in enumerate((-1,1)):
        n=ix*2+iy
        x=sx*0.188; y=sy*0.132
        fork=v4.body(f"caster_fork_{n}")
        # top swivel plate/stem and two descending fork cheeks
        v4.cylinder(f"caster_stem_{n}",0.012,0.026,(x,y,0.104),material=steel,body=fork)
        v4.box(f"caster_top_plate_{n}",(0.044,0.044,0.008),(x,y,0.088),bevel=0.003,material=steel,body=fork)
        wy=y-sy*0.012; wz=0.041
        for side in (-1,1):
            v4.rod(f"fork_leg_{n}_{side}",(x+side*0.0215,wy,wz+0.045),(x+side*0.0215,wy,wz-0.009),0.003,material=steel,body=fork)
        v4.joint(f"caster_swivel_{n}","continuous",cab,fork,origin=(x,y,0.105),axis=(0,0,1))
        wheel=v4.body(f"caster_wheel_{n}")
        v4.torus(f"caster_tire_{n}",0.026,0.009,(x,wy,wz),rot=(0,math.pi/2,0),material=rubber,body=wheel)
        v4.cylinder(f"wheel_hub_{n}",0.016,0.032,(x,wy,wz),rot=(0,math.pi/2,0),material=steel,body=wheel)
        for side in (-1,1):
            v4.cylinder(f"wheel_cap_{n}_{side}",0.008,0.002,(x+side*0.017,wy,wz),rot=(0,math.pi/2,0),segments=24,material=shell_dark,body=wheel)
        v4.joint(f"wheel_roll_{n}","continuous",fork,wheel,origin=(x,wy,wz),axis=(1,0,0))

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