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

# Painted tin, lacquered wood-like panels, and plated metal.
red = v4.painted('candy_red_enamel', (0.72, 0.035, 0.045), roughness=0.36)
red_dark = v4.painted('deep_red_trim', (0.34, 0.018, 0.025), roughness=0.3)
yellow = v4.painted('warm_yellow', (0.98, 0.62, 0.075), roughness=0.28)
cream = v4.painted('ivory_inlay', (0.96, 0.83, 0.58), roughness=0.42)
brass = v4.metal('aged_brass', color=(0.76, 0.49, 0.19), roughness=0.28)
gold = v4.metal('polished_brass', color=(0.91, 0.68, 0.31), roughness=0.22)
steel = v4.metal('blued_steel', color=(0.32, 0.39, 0.43), roughness=0.3, brushed=True)
inner = v4.painted('interior_red', (0.46, 0.035, 0.04), roughness=0.42)
black = v4.rubber('grip_rubber', (0.13, 0.025, 0.025), roughness=0.42)
crank_wood = v4.wood('turned_oak_crank', light=(0.55,0.28,0.12), dark=(0.24,0.075,0.028), grain_scale=7)

boxbody = v4.body('box')
# Main shell: 220 mm cube footprint, 205 mm tall above the feet.
W, D, T = 0.22, 0.205, 0.012
zfloor, ztop = 0.025, 0.217
wall_h = ztop-zfloor
v4.box('bottom_panel', (W-2*T, D-2*T, T), (0, 0, zfloor+T/2), bevel=0.001, material=red_dark, body=boxbody)
# Four walls; opposing walls meet the side walls cleanly.
v4.box('wall_left', (T, D, wall_h), (-W/2+T/2, 0, (zfloor+ztop)/2), bevel=0.0015, material=red, body=boxbody)
v4.box('wall_right', (T, D, wall_h), (W/2-T/2, 0, (zfloor+ztop)/2), bevel=0.0015, material=red, body=boxbody)
v4.box('wall_front', (W-2*T, T, wall_h), (0, -D/2+T/2, (zfloor+ztop)/2), bevel=0.0015, material=red, body=boxbody)
v4.box('wall_back', (W-2*T, T, wall_h), (0, D/2-T/2, (zfloor+ztop)/2), bevel=0.0015, material=red, body=boxbody)
# Interior floor liner sits on the shell floor.
v4.box('interior_floor', (W-2*T, D-2*T, 0.0015), (0, 0, zfloor+T+0.00075), bevel=0.0005, material=cream, body=boxbody)
# Short turned brass feet and collars, mounted beneath the four corners.
for i,(sx,sy) in enumerate([(-1,-1),(1,-1),(-1,1),(1,1)]):
    x,y=sx*0.082,sy*0.075
    v4.lathe(f'foot_{i}', [(0,0.002),(0.012,0.002),(0.013,0.005),(0.011,0.008),(0.009,0.018),(0.011,0.023),(0.011,0.025),(0,0.025)], loc=(x,y,0), material=brass, body=boxbody)
    v4.cylinder(f'foot_pad_{i}', 0.010, 0.002, (x,y,0.001), material=black, body=boxbody)
# Corner edging over the panel joints, with pinstripe rails framing each side.
for sx in (-1,1):
    for sy in (-1,1):
        x=sx*(W/2-0.004)
        v4.box(f'corner_gilt_{sx}_{sy}', (0.008,0.008,wall_h), (x,sy*(D/2-0.004),(zfloor+ztop)/2), bevel=0.0015, material=gold, body=boxbody)
# Wrapped corner pinstripes cover the exposed panel joints.
for sx in (-1,1):
    for sy in (-1,1):
        v4.box(f'corner_face_cover_{sx}_{sy}', (0.003,0.002,wall_h-0.018), (sx*(W/2-0.006), sy*(D/2+0.001), (zfloor+ztop)/2), bevel=0.0008, material=gold, body=boxbody)
        v4.box(f'corner_side_cover_{sx}_{sy}', (0.002,0.012,wall_h-0.018), (sx*(W/2+0.001), sy*(D/2-0.004), (zfloor+ztop)/2), bevel=0.0008, material=gold, body=boxbody)
        v4.box(f'corner_front_wrap_{sx}_{sy}', (0.008,0.003,wall_h-0.018), (sx*(W/2-0.010), sy*(D/2+0.0015), (zfloor+ztop)/2), bevel=0.0008, material=gold, body=boxbody)
# Front/back/side narrow framing bands.
for sy in (-1,1):
    for z in (0.045,0.198):
        v4.box(f'face_rail_y{sy}_{z}', (W-0.024,0.0022,0.003), (0,sy*(D/2+0.001),z), bevel=0.0008, material=gold, body=boxbody)
for sx in (-1,1):
    for z in (0.045,0.198):
        v4.box(f'face_rail_x{sx}_{z}', (0.0022,D-0.024,0.003), (sx*(W/2+0.001),0,z), bevel=0.0008, material=gold, body=boxbody)
# Repeated cream diamond-shaped enamel inlays on all four outer faces.
diamond = [(0,0.018),(0.013,0),(0,-0.018),(-0.013,0)]
for face in ('front','back'):
    yy = -D/2-0.0007 if face=='front' else D/2+0.0007
    frot=(math.pi/2,0,0) if face=='front' else (-math.pi/2,0,0)
    for ix,x in enumerate((-0.055,0,0.055)):
        for iz,z in enumerate((0.075,0.13,0.18)):
            if face == 'front' and ix == 1 and iz >= 2:
                continue  # leave the centre clear for the maker's badge
            v4.extrude(f'{face}_diamond_{ix}_{iz}', diamond, 0.0014, loc=(x,yy,z), rot=frot, bevel=0.0010, material=cream if (ix+iz)%2==0 else yellow, body=boxbody)
for side in (-1,1):
    xx=side*(W/2+0.0007)
    srot=(0,side*math.pi/2,0)
    for iy,y in enumerate((-0.052,0,0.052)):
        for iz,z in enumerate((0.075,0.13,0.18)):
            if iy == 1 and iz == 1:
                continue
            v4.extrude(f'side_diamond_{side}_{iy}_{iz}', diamond, 0.0014, loc=(xx,y,z), rot=srot, bevel=0.0010, material=cream if (iy+iz)%2==0 else yellow, body=boxbody)
# Small embossed clown-face medallions add a classic circus-toy cue on both sides.
for side in (-1,1):
    v4.cylinder(f'clown_medallion_rim_{side}',0.020,0.003,(side*0.1115,0,0.130),rot=(0,side*math.pi/2,0),material=gold,body=boxbody)
    v4.cylinder(f'clown_medallion_face_{side}',0.016,0.0015,(side*0.1135,0,0.130),rot=(0,side*math.pi/2,0),material=yellow,body=boxbody)
    for eye_y in (-0.006,0.006):
        v4.sphere(f'clown_eye_{side}_{eye_y}',0.0022,(side*0.115,eye_y,0.135),material=red_dark,body=boxbody)
    v4.sphere(f'clown_nose_{side}',0.003,(side*0.115,0,0.131),material=red,body=boxbody)
    smile=[(side*0.115,-0.008,0.125),(side*0.115,-0.004,0.1225),(side*0.115,0,0.1215),(side*0.115,0.004,0.1225),(side*0.115,0.008,0.125)]
    v4.tube(f'clown_smile_{side}',smile,0.0011,material=red_dark,body=boxbody,resolution=8)
# Decorative front badge/nameplate around the crank and label.
v4.box('nameplate', (0.102,0.0025,0.022), (0, -D/2-0.002, 0.177), bevel=0.003, material=brass, body=boxbody)
inset = v4.box('nameplate_inset', (0.093,0.0012,0.015), (0, -D/2-0.0037, 0.177), bevel=0.002, material=red_dark, body=boxbody)
v4.text('nameplate_text', 'JACK-IN-THE-BOX', 0.0058, loc=(0,-D/2-0.006,0.177), rot=(math.pi/2,0,0), material=cream, body=boxbody, on=inset)
# Screw heads in panel framing; cylinders point outwards from each face.
for sx in (-1,1):
    for sy in (-1,1):
        v4.cylinder(f'corner_screw_{sx}_{sy}',0.0023,0.0018,(sx*0.092,sy*(D/2+0.0013),0.053), rot=(math.pi/2,0,0) if sy<0 else (-math.pi/2,0,0), segments=20, material=gold, body=boxbody)
# Music movement: compact cast housing on interior floor, vent slots and four cover screws.
v4.box('music_movement_case',(0.094,0.070,0.018),(0,0,0.046),bevel=0.004,material=brass,body=boxbody)
v4.box('movement_top',(0.082,0.058,0.002),(0,0,0.056),bevel=0.002,material=gold,body=boxbody)
for i in range(6):
    x=-0.030+i*0.012
    v4.box(f'movement_vent_{i}',(0.006,0.001,0.0008),(x,0.0,0.0572),bevel=0.0003,material=steel,body=boxbody)
for sx in (-1,1):
    for sy in (-1,1):
        v4.cylinder(f'movement_screw_{sx}_{sy}',0.002,0.001,(sx*0.036,sy*0.023,0.058),segments=20,material=steel,body=boxbody)
# Fixed spring post and visible helical steel coil. Coil loops upward around the post.
v4.cylinder('spring_socket',0.018,0.007,(0,0.018,0.061), material=brass, body=boxbody)
v4.cylinder('spring_post',0.005,0.148,(0,0.018,0.136),material=red_dark,body=boxbody)
coil_pts=[]
for i in range(241):
    t=i/240
    a=2*math.pi*6*t
    coil_pts.append((0.028*math.cos(a),0.018+0.028*math.sin(a),0.068+0.137*t))
v4.tube('helical_spring',coil_pts,0.0032,material=gold,body=boxbody,resolution=12)
v4.cylinder('spring_top_cup',0.030,0.006,(0,0.018,0.207),material=brass,body=boxbody)
v4.cylinder('spring_top_button',0.016,0.004,(0,0.018,0.212),material=yellow,body=boxbody)
v4.torus('spring_cup_rim',0.024,0.002,(0,0.018,0.211),material=gold,body=boxbody)
# Interior catch mounted to front wall, with a bent-over retaining tongue.
v4.box('latch_mount',(0.034,0.003,0.025),(0,-0.089,0.194),bevel=0.0015,material=brass,body=boxbody)
v4.box('latch_tongue',(0.018,0.013,0.003),(0,-0.081,0.207),bevel=0.001,material=steel,body=boxbody)
v4.cylinder('latch_pivot',0.003,0.006,(0,-0.087,0.194),rot=(math.pi/2,0,0),material=gold,body=boxbody)
# External spring catch at the front top edge; the lid carries its mating keeper tab.
v4.box('external_catch_backing',(0.040,0.003,0.018),(0,-0.104,0.207),bevel=0.002,material=brass,body=boxbody)
v4.box('external_catch_inset',(0.030,0.0015,0.012),(0,-0.1062,0.207),bevel=0.0015,material=red_dark,body=boxbody)
v4.cylinder('external_catch_pivot',0.0035,0.004,(0,-0.108,0.207),rot=(math.pi/2,0,0),material=gold,body=boxbody)
v4.tube('external_catch_hook',[(-0.010,-0.108,0.207),(-0.010,-0.112,0.213),(-0.010,-0.119,0.216)],0.0022,material=gold,body=boxbody,resolution=10)
# Crank bearing bolted through front wall; front face is y=-0.1025.
v4.cylinder('crank_escutcheon_backplate',0.025,0.003,(0,-0.104,0.090),rot=(math.pi/2,0,0),material=red_dark,body=boxbody)
v4.torus('escutcheon_outer_bead',0.022,0.0025,(0,-0.106,0.090),rot=(math.pi/2,0,0),material=gold,body=boxbody)
v4.cylinder('crank_boss',0.019,0.007,(0,-0.106,0.090),rot=(math.pi/2,0,0),material=brass,body=boxbody)
v4.torus('bearing_bezel',0.015,0.002,(0,-0.110,0.090),rot=(math.pi/2,0,0),material=gold,body=boxbody)
for a in (45,135,225,315):
    r=math.radians(a)
    v4.cylinder(f'bearing_bolt_{a}',0.0018,0.0015,(0.015*math.cos(r),-0.111,0.090+0.015*math.sin(r)),rot=(math.pi/2,0,0),segments=16,material=steel,body=boxbody)

# Lid: slightly overhanging enamelled panel with a framed top and stiffened underside.
lid = v4.body('lid')
lid_w,lid_d,lid_t=0.230,0.220,0.016
hinge_y=D/2
hinge_z=ztop
v4.box('lid_slab',(lid_w,lid_d,lid_t),(0,hinge_y-lid_d/2,hinge_z+lid_t/2),bevel=0.004,material=yellow,body=lid)
# Top perimeter rails, fitting within the lid face.
for x in (-lid_w/2+0.012,lid_w/2-0.012):
    v4.box(f'lid_rail_x_{x}',(0.004,lid_d-0.028,0.002),(x,hinge_y-lid_d/2,hinge_z+lid_t+0.0008),bevel=0.001,material=red,body=lid)
for y in (hinge_y-lid_d+0.012,hinge_y-0.012):
    v4.box(f'lid_rail_y_{y}',(lid_w-0.028,0.004,0.002),(0,y,hinge_z+lid_t+0.0008),bevel=0.001,material=red,body=lid)
# Brass corner caps and a raised eight-point star on the lid top.
for sx in (-1,1):
    for sy in (-1,1):
        x=sx*(lid_w/2-0.012); y=hinge_y-lid_d/2+sy*(lid_d/2-0.012)
        v4.box(f'lid_corner_cap_{sx}_{sy}',(0.014,0.014,0.002),(x,y,hinge_z+lid_t+0.001),bevel=0.002,material=gold,body=lid)
star=[]
for k in range(10):
    a=math.pi/2+k*math.pi/5
    r=0.026 if k%2==0 else 0.011
    star.append((r*math.cos(a),r*math.sin(a)))
v4.extrude('lid_star',star,0.0015,loc=(0,hinge_y-lid_d/2,hinge_z+lid_t+0.0018),bevel=0.0006,material=red,body=lid)
# Two underside braces terminate at the hinge and front edge of the lid.
for x in (-0.075,0.075):
    v4.box(f'lid_underbrace_{x}',(0.012,0.160,0.006),(x,-0.008,hinge_z-0.003),bevel=0.0015,material=brass,body=lid)
    for j,y in enumerate((-0.078,0.062)):
        v4.cylinder(f'lid_brace_screw_{x}_{j}',0.0018,0.0015,(x,y,hinge_z-0.0065),segments=16,material=steel,body=lid)
# Hinge knuckles on lid and matching hinge leaves on box, located on the true rear pivot.
for i,x in enumerate((-0.078,0.078)):
    v4.cylinder(f'lid_hinge_knuckle_{i}',0.0045,0.032,(x,hinge_y,hinge_z),rot=(0,math.pi/2,0),material=gold,body=lid)
    v4.box(f'hinge_leaf_{i}',(0.036,0.003,0.022),(x,hinge_y+0.0035,hinge_z-0.010),bevel=0.0007,material=brass,body=boxbody)
    v4.cylinder(f'hinge_screw_{i}',0.0018,0.0015,(x,hinge_y+0.006,hinge_z-0.010),rot=(math.pi/2,0,0),segments=16,material=steel,body=boxbody)
# A small catch bar on the underside meets the fixed latch tongue when closed.
v4.box('lid_catch_bar',(0.034,0.012,0.006),(0,-D/2+0.018,hinge_z-0.003),bevel=0.0015,material=brass,body=lid)
v4.box('lid_front_keeper_tab',(0.010,0.008,0.008),(-0.010,-0.114,hinge_z-0.002),bevel=0.0015,material=gold,body=lid)
v4.joint('lid_hinge','revolute',boxbody,lid,origin=(0,hinge_y,hinge_z),axis=(1,0,0),limits=(-math.radians(110),0))

# Crank is a separate rigid assembly on a continuous axle through the front bearing.
crank=v4.body('crank')
v4.cylinder('axle_shaft',0.004,0.046,(0,-0.132,0.090),rot=(math.pi/2,0,0),material=steel,body=crank)
v4.cylinder('hub',0.010,0.009,(0,-0.151,0.090),rot=(math.pi/2,0,0),material=brass,body=crank)
# Swept L-shaped handle, with a turned grippable knob at its offset end.
v4.tube('crank_arm_L',[(0,-0.157,0.090),(0.038,-0.157,0.090),(0.038,-0.157,0.134)],0.0045,material=brass,body=crank,resolution=12)
v4.cylinder('grip_stem',0.0038,0.012,(0.038,-0.157,0.139),material=brass,body=crank)
v4.lathe('turned_crank_grip',[(0,0),(0.004,0),(0.0065,0.005),(0.007,0.014),(0.0065,0.024),(0.005,0.028),(0,0.030)],loc=(0.038,-0.158,0.139),rot=(math.pi/2,0,0),material=crank_wood,body=crank)
v4.torus('grip_ferrule_inner',0.0057,0.0012,(0.038,-0.161,0.139),rot=(math.pi/2,0,0),material=gold,body=crank)
v4.torus('grip_ferrule_outer',0.0053,0.0010,(0.038,-0.183,0.139),rot=(math.pi/2,0,0),material=gold,body=crank)
v4.cylinder('grip_endcap',0.0045,0.0015,(0.038,-0.188,0.139),rot=(math.pi/2,0,0),material=gold,body=crank)
v4.joint('crank_axle','continuous',boxbody,crank,origin=(0,-0.104,0.090),axis=(0,1,0))

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