"""Detailed articulated tabletop tri-fold mirror, authored with v4lib."""
import math
import os
import sys

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

v4.reset()

# Product palette: satin champagne aluminium, graphite polymer and mirror silver.
alum = v4.metal("satin_champagne_aluminium", color=(0.72, 0.67, 0.56), roughness=0.3)
bright = v4.metal("bright_nickel", color=(0.82, 0.84, 0.86), roughness=0.22, brushed=False)
steel = v4.metal("hinge_steel", color=(0.48, 0.51, 0.54), roughness=0.34)
black = v4.plastic("graphite_backing", (0.055, 0.065, 0.075), roughness=0.34)
dark = v4.rubber("soft_black_rubber", (0.025, 0.028, 0.032))
mirror = v4.mat("cool_silver_mirror", (0.86, 0.89, 0.92), metallic=1.0, roughness=0.025, coat=0.45)
ink = v4.mat("marking_ink", (0.16, 0.19, 0.21), roughness=0.55)
felt = v4.fabric("charcoal_backing_felt", (0.075, 0.083, 0.09), weave_scale=240)

# Open dimensions 560 x 430 mm; centre 280 mm, two 140 mm wings.
CW, WW = 0.280, 0.140
H = 0.400
ZBOT, ZTOP = 0.035, 0.435
ZC = (ZBOT + ZTOP) / 2
HINGE_Y = -0.008
root = v4.body("centre")


def frame(name, width, height, x, z, body, y_front=-0.010, bar=0.012,
          material=alum, depth=0.010, bevel=0.002):
    """Four non-overlapping rails framing a clear inset."""
    # Vertical rails run full height; horizontal rails fit between them.
    v4.box(name + "_rail_L", (bar, depth, height), (x-width/2+bar/2, y_front+depth/2, z),
           bevel=bevel, material=material, body=body)
    v4.box(name + "_rail_R", (bar, depth, height), (x+width/2-bar/2, y_front+depth/2, z),
           bevel=bevel, material=material, body=body)
    inner = width - 2*bar
    v4.box(name + "_rail_top", (inner+0.002, depth, bar), (x, y_front+depth/2, z+height/2-bar/2),
           bevel=bevel, material=material, body=body)
    v4.box(name + "_rail_bottom", (inner+0.002, depth, bar), (x, y_front+depth/2, z-height/2+bar/2),
           bevel=bevel, material=material, body=body)


# Centre cassette, rear pan, inset mirror and front frame. The rear pan also
# carries the stand hinges and hinge-leaf screws.
v4.box("centre_rear_pan", (CW, 0.014, H), (0, 0.010, ZC), bevel=0.004,
       material=black, body=root)
v4.box("centre_mirror", (0.246, 0.003, 0.366), (0, -0.0005, ZC), bevel=0.0015,
       material=mirror, body=root)
frame("centre_bezel", CW, H, 0, ZC, root, y_front=-0.009, bar=0.017,
      depth=0.008, material=alum, bevel=0.0035)
# A felt insert breaks up the broad rear pan and adds a soft product finish.
v4.box("centre_back_felt", (0.242, 0.002, 0.350), (0, 0.018, ZC), bevel=0.004,
       material=felt, body=root)

# Weighted tabletop foot. The centre cassette sits directly on this plinth.
v4.box("weighted_foot", (0.340, 0.075, 0.035), (0, 0.006, 0.0175), bevel=0.007,
       material=black, body=root)
v4.box("foot_front_trim", (0.318, 0.006, 0.006), (0, -0.0310, 0.019), bevel=0.001,
       material=alum, body=root)

# Unobtrusive lower brand plate and two touch controls.
v4.box("maker_badge", (0.076, 0.002, 0.012), (0, -0.011, 0.052), bevel=0.002,
       material=bright, body=root)
v4.text("maker_mark", "LUMA", 0.008, loc=(0, -0.0122, 0.052),
        rot=(math.pi/2, 0, 0), material=ink, body=root, on=None, extrude_depth=0.0002)
for x in (-0.122, 0.122):
    v4.cylinder("touch_key_%s" % x, 0.0035, 0.0015, (x, -0.0105, 0.052),
                rot=(math.pi/2, 0, 0), segments=24, bevel=0.0004, material=bright, body=root)

# Offset piano hinge spine: interleaved knuckles on the stationary and moving
# leaves share a single full-height vertical pivot line.
hinge_xs = (-CW/2, CW/2)
for side, hx in zip(("left", "right"), hinge_xs):
    # Fixed leaf and its screws, secured to the centre cassette edge.
    v4.box(side+"_fixed_hinge_leaf", (0.003, 0.030, H-0.020),
           (hx + (0.002 if hx < 0 else -0.002), 0.014, ZC), bevel=0.0008,
           material=steel, body=root)
    for z in (ZBOT+0.035, ZC, ZTOP-0.035):
        v4.cylinder(side+"_fixed_leaf_screw_"+str(z), 0.0025, 0.0015,
                    (hx + (0.004 if hx < 0 else -0.004), 0.017, z),
                    rot=(0, math.pi/2, 0), segments=16, bevel=0.0003,
                    material=bright, body=root)

# Each wing is its own rigid body with a silver mirror front, inset frame,
# textured rear cassette, and real hinge leaf/barrels.
wings = []
for side, sign in (("left", -1), ("right", 1)):
    cx = sign * (CW/2 + WW/2)
    wing = v4.body(side+"_wing")
    wings.append((side, sign, cx, wing))
    # Raised 15 mm above the base line, as on many compact tri-fold mirrors.
    wing_h, wing_z = H-0.015, ZC+0.0075
    v4.box(side+"_rear_pan", (WW, 0.014, wing_h), (cx, 0.010, wing_z), bevel=0.004,
           material=black, body=wing)
    v4.box(side+"_back_felt", (0.112, 0.002, 0.345), (cx, 0.018, wing_z), bevel=0.003,
           material=felt, body=wing)
    v4.box(side+"_mirror", (0.108, 0.003, 0.351), (cx, -0.0005, wing_z), bevel=0.0025,
           material=mirror, body=wing)
    frame(side+"_bezel", WW, wing_h, cx, wing_z, wing, y_front=-0.009, bar=0.012,
          depth=0.008, material=alum, bevel=0.003)
    hx = sign * CW/2
    # Offset leaf arm reaches from rear pivot to the wing edge.
    v4.box(side+"_moving_hinge_leaf", (0.003, 0.030, wing_h-0.020),
           (hx + sign*0.003, 0.014, wing_z), bevel=0.0008, material=steel, body=wing)
    # Closely interleaved knuckles form one continuous piano hinge, ending
    # flush with the top and bottom frame edges.
    pitch = H / 16
    for k in range(16):
        z = ZBOT + pitch*(k + 0.5)
        owner = root if k % 2 == (0 if sign < 0 else 1) else wing
        v4.cylinder(side+"_knuckle_"+str(k), 0.0040, pitch-0.00035,
                    (hx, HINGE_Y, z), segments=32, bevel=0.00035,
                    material=steel if k % 2 else bright, body=owner)
    v4.joint(side+"_wing_hinge", "revolute", root, wing,
             origin=(hx, HINGE_Y, ZC), axis=(0, 0, 1),
             limits=(0, math.pi) if sign < 0 else (-math.pi, 0))

# Hinged U-frame easel. It is stowed upright on the rear, and swings down/back
# through 130 degrees so its rubber feet land beside the weighted base.
leg = v4.body("prop_leg")
PIVOT_Z, PIVOT_Y = 0.155, 0.025
LEG_TOP = 0.395
for sx in (-1, 1):
    x = sx*0.088
    v4.rod("easel_rail_"+str(sx), (x, 0.030, PIVOT_Z), (x, 0.030, LEG_TOP),
           0.0045, material=bright, body=leg)
    v4.cylinder("easel_pivot_collar_"+str(sx), 0.009, 0.012,
                (x+sx*0.001, PIVOT_Y, PIVOT_Z), rot=(0, math.pi/2, 0),
                material=steel, body=leg)
    v4.lathe("easel_rubber_tip_"+str(sx), [(0, 0), (0.007, 0), (0.007, 0.012), (0, 0.012)],
             loc=(x, 0.030, LEG_TOP), material=dark, body=leg)
v4.rod("easel_top_crossbar", (-0.088, 0.030, LEG_TOP), (0.088, 0.030, LEG_TOP),
       0.0045, material=bright, body=leg)
# Small spring clips retain the upright rails when the easel is stowed.
for sx in (-1, 1):
    v4.torus("easel_stow_clip_"+str(sx), 0.0052, 0.0012,
             (sx*0.088, 0.030, 0.215), rot=(math.pi/2, 0, 0),
             material=alum, body=leg)
# The fixed paired ears make the transverse axle visibly mounted to the pan.
for sx in (-1, 1):
    # Clevis ears sit just outside the offset moving collars; the collar face
    # meets each ear at the axle plane without solid overlap.
    x = sx*0.101
    v4.box("easel_mount_ear_"+str(sx), (0.012, 0.012, 0.028),
           (x, 0.022, PIVOT_Z), bevel=0.002, material=alum, body=root)
v4.joint("easel_hinge", "revolute", root, leg,
         origin=(0, PIVOT_Y, PIVOT_Z), axis=(1, 0, 0),
         limits=(-math.radians(130), 0))

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