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

v4.reset()

# Finishes
blue = v4.painted('stove_blue_enamel', (0.075, 0.24, 0.42), roughness=0.27, coat=0.75)
steel = v4.metal('brushed_stainless', (0.72, 0.75, 0.77), roughness=0.27)
darksteel = v4.metal('burner_dark_steel', (0.16, 0.18, 0.19), roughness=0.34)
brass = v4.metal('machined_brass', (0.72, 0.47, 0.19), roughness=0.25)
black = v4.rubber('heatproof_black', (0.025, 0.03, 0.032))
red = v4.plastic('valve_red', (0.72, 0.055, 0.035), roughness=0.31, texture=0.12)
white = v4.mat('marking_white', (0.88, 0.9, 0.88), roughness=0.52)

# Deployed appliance chassis: underside rests above ground on three legs.
base = v4.body('chassis')
# pressed square-ish tray with a broad rolled rim and central heat shield
tray = v4.box('stamped_tray', (0.132, 0.128, 0.018), (0, 0, 0.083), bevel=0.012, material=blue, body=base)
# Recesses on the underside let the leg barrels sit in real stamped hinge pockets.
for nm, theta in [('left', math.radians(210)), ('right', math.radians(330)), ('rear', math.radians(90))]:
    n=(math.cos(theta), math.sin(theta), 0)
    pivot=(0.052*n[0], 0.052*n[1], 0.069)
    cutter=v4.box(nm+'_hinge_recess_cutter', (0.038,0.032,0.011),
                  (pivot[0]+0.010*n[0],pivot[1]+0.010*n[1],0.072), rot=(0,0,theta), bevel=0)
    v4.boolean(tray,cutter)
# Pressed top perimeter bead: a rolled steel edge follows the softened tray corners.
v4.tube('tray_rolled_edge', [(-0.052,-0.060,0.092),(-0.060,-0.052,0.092),(-0.060,0.052,0.092),
                             (-0.052,0.060,0.092),(0.052,0.060,0.092),(0.060,0.052,0.092),
                             (0.060,-0.052,0.092),(0.052,-0.060,0.092)], 0.0012,
        material=steel, body=base, cyclic=True)
# Fine heat-relief slots pressed into the tray around the burner well.
for i in range(12):
    a=2*math.pi*i/12
    x,y=0.049*math.cos(a),0.049*math.sin(a)
    v4.box(f'tray_vent_{i:02}',(0.007,0.0016,0.0008),(x,y,0.0921),rot=(0,0,a),
           bevel=0.0004,material=darksteel,body=base)
v4.cylinder('burner_well', 0.039, 0.012, (0, 0, 0.099), bevel=0.003, material=darksteel, body=base)
v4.cylinder('burner_riser', 0.0125, 0.026, (0, 0, 0.118), bevel=0.002, material=brass, body=base)
# Side-entry venturi mixing tube with a dark open intake and rolled brass collar.
v4.cylinder('venturi_tube',0.0065,0.022,(0.013,0,0.118),rot=(0,math.pi/2,0),
            bevel=0.001,material=brass,body=base)
v4.cylinder('venturi_mouth',0.0047,0.001,(0.0245,0,0.118),rot=(0,math.pi/2,0),
            bevel=0.0003,material=black,body=base)
v4.torus('venturi_rim',0.0054,0.0007,(0.024,0,0.118),rot=(0,math.pi/2,0),material=steel,body=base)
v4.cylinder('burner_mix_chamber', 0.025, 0.012, (0, 0, 0.137), bevel=0.003, material=darksteel, body=base)
v4.cylinder('burner_head_lip', 0.030, 0.006, (0, 0, 0.146), bevel=0.002, material=steel, body=base)
v4.cylinder('burner_cap', 0.025, 0.007, (0, 0, 0.152), bevel=0.002, material=darksteel, body=base)
v4.torus('flame_spreader_ring',0.027,0.0015,(0,0,0.157),material=brass,body=base)
# radial burner ports represented as small dark drilled mouths around the cap perimeter
for i in range(24):
    a = 2*math.pi*i/24
    x, y = 0.0215*math.cos(a), 0.0215*math.sin(a)
    v4.cylinder(f'jet_port_{i:02}', 0.00115, 0.001, (x, y, 0.1557), segments=12, bevel=0,
                material=black, body=base)
# Three fixed pot-support arms with turned pot-contact nibs, arranged 120 degrees apart.
for i in range(3):
    a = math.radians(90 + i*120)
    ux, uy = math.cos(a), math.sin(a)
    p0 = (ux*0.023, uy*0.023, 0.146)
    p1 = (ux*0.078, uy*0.078, 0.158)
    p2 = (ux*0.086, uy*0.086, 0.153)
    v4.rod(f'pot_arm_{i}', p0, p1, 0.0035, material=steel, body=base)
    v4.rod(f'pot_lip_{i}', p1, p2, 0.0035, material=steel, body=base)
    v4.cylinder(f'pot_contact_{i}', 0.0048, 0.003, (p1[0], p1[1], 0.160), bevel=0.001,
                material=black, body=base)
    # Three shallow transverse serrations on each pot landing improve grip.
    tx,ty=-uy,ux
    for j in range(3):
        along=0.0025*(j-1)
        cx=p1[0]+along*ux
        cy=p1[1]+along*uy
        v4.rod(f'pot_tooth_{i}_{j}',(cx-0.003*tx,cy-0.003*ty,0.1615),
               (cx+0.003*tx,cy+0.003*ty,0.1615),0.00065,material=darksteel,body=base)
    # Root rivet on tray
    v4.cylinder(f'arm_rivet_{i}', 0.003, 0.002, (ux*0.036, uy*0.036, 0.093),
                bevel=0.0005, material=brass, body=base)
# Front-facing fuel valve, coupling points toward +X.
v4.cylinder('valve_body', 0.008, 0.020, (0.055, -0.020, 0.091), rot=(0, math.pi/2, 0),
            bevel=0.002, material=brass, body=base)
v4.cylinder('fuel_socket', 0.0065, 0.015, (0.071, -0.020, 0.091), rot=(0, math.pi/2, 0),
            bevel=0.001, material=steel, body=base)
v4.cylinder('coupling_nut', 0.009, 0.008, (0.079, -0.020, 0.091), rot=(0, math.pi/2, 0),
            bevel=0.0015, material=brass, body=base)
# Flexible fuel feed links the inlet to the mixing tube.
v4.tube('fuel_feed_line',[(0.083,-0.020,0.091),(0.063,-0.018,0.092),(0.046,-0.008,0.098),
                          (0.031,0.004,0.106),(0.014,0.006,0.112)],0.0022,
        material=brass,body=base,resolution=24)
for i, x in enumerate((0.0762, 0.0780, 0.0798, 0.0816)):
    v4.torus(f'coupling_grip_ring_{i}', 0.0091, 0.00045, (x, -0.020, 0.091),
             rot=(0, math.pi/2, 0), material=brass, body=base)
# Red ribbed control knob at the forward edge; shaft and pointer are all chassis-mounted.
v4.cylinder('control_shaft', 0.0035, 0.017, (0.050, -0.043, 0.090), rot=(math.pi/2,0,0),
            bevel=0.001, material=brass, body=base)
v4.cylinder('control_knob', 0.012, 0.012, (0.050, -0.054, 0.090), rot=(math.pi/2,0,0),
            bevel=0.003, material=red, body=base)
for i in range(10):
    a=2*math.pi*i/10
    v4.rod(f'knob_grip_{i}', (0.050+0.011*math.cos(a), -0.055, 0.090+0.011*math.sin(a)),
           (0.050+0.011*math.cos(a), -0.061, 0.090+0.011*math.sin(a)), 0.0012,
           material=red, body=base)
v4.box('knob_pointer', (0.002,0.001,0.007), (0.050,-0.0615,0.097), bevel=0.0004, material=white, body=base)
# Screws in the tray flange.
for i, (x,y) in enumerate([(-.051,-.048),(.051,-.048),(-.051,.048),(.051,.048)]):
    v4.cylinder(f'tray_fastener_{i}', 0.0027, 0.0015, (x,y,0.0925), bevel=0.0005, material=steel, body=base)
# unobtrusive product mark on front edge
v4.text('stove_mark', 'TRAIL  |  2.8 kW', 0.0065, loc=(0,-0.0642,0.085),
        rot=(math.pi/2,0,0), extrude_depth=0.0002, material=white, body=base)

# Three independently hinged tubular legs. At 0 they splay down; positive rotation
# around each tangential axis folds them inward and flat beneath the tray.
leg_defs = [
    ('left', math.radians(210)),
    ('right', math.radians(330)),
    ('rear', math.radians(90)),
]
for name, theta in leg_defs:
    n=(math.cos(theta), math.sin(theta), 0)
    tangent=(-math.sin(theta), math.cos(theta), 0)
    pivot=(0.052*n[0], 0.052*n[1], 0.069)
    leg=v4.body('leg_'+name)
    # Stainless hinge barrel centered at pivot, with a blue clevis strap on the tray body.
    v4.cylinder(name+'_hinge_barrel', 0.006, 0.022, pivot,
                rot=(math.pi/2,0,theta), bevel=0.001, material=steel, body=leg)
    # Leg tip lies on floor in rest pose, with moderate radial splay.
    foot=(pivot[0]+0.038*n[0], pivot[1]+0.038*n[1], 0.005)
    v4.rod(name+'_tubular_leg', pivot, foot, 0.0042, material=steel, body=leg)
    v4.cylinder(name+'_rubber_foot', 0.006, 0.012,
                (foot[0],foot[1],0.006), bevel=0.002, material=black, body=leg)
    # small exposed pin head at outer hinge face
    v4.cylinder(name+'_pivot_cap', 0.0042, 0.002, (pivot[0]+0.0005*tangent[0],pivot[1]+0.0005*tangent[1],pivot[2]),
                rot=(math.pi/2,0,theta), bevel=0.0007, material=brass, body=leg)
    # Under-tray clevis cheeks straddle the hinge barrel and read as a real mount.
    for side in (-1,1):
        v4.box(f'{name}_clevis_{side}',(0.024,0.003,0.020),
               (pivot[0]+side*0.010*tangent[0],pivot[1]+side*0.010*tangent[1],0.064),
               rot=(0,0,theta),bevel=0.001,material=blue,body=base)
        v4.cylinder(f'{name}_clevis_rivet_{side}',0.0022,0.0012,
                    (pivot[0]+side*0.0115*tangent[0],pivot[1]+side*0.0115*tangent[1],0.065),
                    rot=(math.pi/2,0,theta),bevel=0.0003,material=steel,body=base)
    v4.joint(name+'_hinge','revolute',base,leg,origin=pivot,axis=tangent,limits=(0,2.12))

# Rear windscreen with a continuous face; the rear leg now folds inward, away from it.
wind=v4.body('windscreen')
wx0, wx1 = -0.059, 0.059
wy=0.061
zbot, ztop = 0.080, 0.145
v4.box('shield_panel',(wx1-wx0,0.002,ztop-zbot),(0,wy,(zbot+ztop)/2),
       bevel=0.0012,material=steel,body=wind)
# rolled side returns project toward the burner and reduce drafts
for side in (-1,1):
    x=side*0.057
    v4.box(f'shield_return_{side}',(0.004,0.014,ztop-zbot-0.006),(x,wy-0.006,(zbot+ztop)/2),
           bevel=0.0015,material=steel,body=wind)
# Rolled top bead and pressed vertical stiffening ribs.
v4.box('shield_top_roll',(0.116,0.005,0.004),(0,wy,ztop-0.001),
       bevel=0.0018,material=steel,body=wind)
for x in (-0.038,-0.019,0,0.019,0.038):
    v4.box(f'shield_stiffening_crease_{x}',(0.001,0.0008,0.048),(x,wy-0.0015,0.116),
           bevel=0.0003,material=darksteel,body=wind)
# Alternating moving and fixed hinge knuckles around a continuous pin on the lower edge.
for x in (-0.045,0.0,0.045):
    v4.cylinder(f'shield_moving_knuckle_{x}',0.0032,0.018,(x,wy,zbot),rot=(0,math.pi/2,0),
                bevel=0.0005,material=brass,body=wind)
for x in (-0.0225,0.0225):
    v4.cylinder(f'shield_fixed_knuckle_{x}',0.0030,0.018,(x,wy,zbot),rot=(0,math.pi/2,0),
                bevel=0.0005,material=steel,body=base)
    v4.box(f'shield_hinge_ear_{x}',(0.010,0.009,0.004),(x,wy-0.001,0.077),
           bevel=0.0008,material=blue,body=base)
v4.cylinder('windscreen_hinge_pin',0.0018,0.116,(0,wy,zbot),rot=(0,math.pi/2,0),
            bevel=0.0002,material=brass,body=base)
v4.text('shield_warning','HOT',0.009,loc=(0,wy-0.0016,0.119),rot=(math.pi/2,0,0),
        extrude_depth=0.0002,material=white,body=wind)
v4.joint('windscreen_hinge','revolute',base,wind,origin=(0,wy,zbot),axis=(1,0,0),limits=(-1.30,0))

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