"""Detailed articulated automotive sun visor assembly. Run with Blender in background."""
import math
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import v4lib as v4

v4.reset()

# Interior-grade materials
headliner = v4.plastic("warm_gray_headliner", (0.68, 0.65, 0.59), roughness=0.63, texture=0.08)
cover = v4.plastic("pivot_cover_gray", (0.56, 0.55, 0.51), roughness=0.48, texture=0.04)
charcoal = v4.plastic("clip_charcoal", (0.12, 0.13, 0.13), roughness=0.46, texture=0.06)
steel = v4.metal("satin_zinc_steel", color=(0.72, 0.74, 0.75), roughness=0.28, brushed=True)
fastener = v4.metal("dark_zinc_fasteners", color=(0.36, 0.39, 0.40), roughness=0.32, brushed=False)
fabric = v4.fabric("light_taupe_woven_fabric", (0.72, 0.69, 0.62), weave_scale=250)
stitch = v4.mat("taupe_stitch_thread", (0.42, 0.39, 0.34), roughness=0.85)
mirror_frame = v4.plastic("mirror_frame", (0.34, 0.35, 0.34), roughness=0.38, texture=0.03)
glass = v4.glass("vanity_mirror_glass", color=(0.78, 0.83, 0.84), roughness=0.16)
strap_mat = v4.fabric("ticket_strap_elastic", (0.17, 0.18, 0.18), weave_scale=150)

# Dimensions: visor panel 420 x 155 x 28 mm, with support outside ends.
W = 0.420
H = 0.155
T = 0.028
rod_z = 0.142
pivot = (-0.188, 0.0, 0.215)

roof = v4.body("roof_mount")
bracket = v4.body("support_bracket")
visor = v4.body("visor")

# Roof/headliner shoe; its lower face lies close to the upper visor support.
v4.box("mounting_shoe", (0.086, 0.060, 0.018), (-0.188, 0.0, 0.215), bevel=0.008, material=headliner, body=roof)
v4.box("mounting_flange", (0.078, 0.046, 0.006), (-0.188, 0.0, 0.203), bevel=0.003, material=cover, body=roof)
# Slim fixed leaf and visible screw heads on the roof side.
v4.box("roof_hinge_leaf", (0.042, 0.012, 0.024), (-0.188, -0.028, 0.215), bevel=0.002, material=steel, body=roof)
for i, x in enumerate((-0.203, -0.173)):
    v4.cylinder(f"mount_screw_{i}", 0.0032, 0.0018, (x, -0.0345, 0.216), rot=(math.pi/2, 0, 0),
                segments=20, material=fastener, body=roof)
# Fixed headliner retaining clip. A single extruded U-profile avoids seams between
# the spring arms and bridge; its bridge overlaps the right side of the mounting shoe.
v4.extrude("retaining_clip", [(-0.025, 0.012), (0.025, 0.012), (0.025, -0.013),
                             (0.020, -0.013), (0.020, 0.006), (-0.020, 0.006),
                             (-0.020, -0.013), (-0.025, -0.013)], 0.012,
           loc=(-0.121, 0.006, 0.212), rot=(math.pi/2, 0, 0), bevel=0.001,
           material=charcoal, body=roof)

# Pivoting formed support arm and cross-car visor rod.
# Arm descends from the roof pivot and transitions into the horizontal support axis.
v4.cylinder("bracket_pivot_barrel", 0.009, 0.035, pivot, rot=(0, math.pi/2, 0), material=steel, body=bracket)
v4.cylinder("pivot_cap_outer", 0.012, 0.003, (-0.207, 0.0, 0.215), rot=(0, math.pi/2, 0), material=cover, body=bracket)
v4.cylinder("pivot_cap_inner", 0.010, 0.003, (-0.169, 0.0, 0.215), rot=(0, math.pi/2, 0), material=cover, body=bracket)
v4.rod("formed_support_arm", (-0.188, 0.0, 0.207), (-0.188, -0.075, rod_z), 0.0045, material=steel, body=bracket)
v4.rod("visor_support_rod", (-0.235, -0.075, rod_z), (0.235, -0.075, rod_z), 0.0035, material=steel, body=bracket)
# Two small end collars identify the rod termination beyond the padded board.
for i, x in enumerate((-0.222, 0.222)):
    v4.cylinder(f"rod_end_collar_{i}", 0.0052, 0.008, (x, -0.075, rod_z),
                rot=(0, math.pi/2, 0), material=cover, body=bracket)

# Soft rounded board: front face is toward negative Y.
center_z = rod_z - H/2 + 0.009
v4.box("padded_visorshell", (W, T, H), (0.0, -0.075, center_z), bevel=0.013, material=fabric, body=visor)
# A subtle sewn piping follows the front perimeter, set just proud of the upholstery.
y_front = -0.075 - T/2 - 0.00025
x_edge = W/2 - 0.014
z_top = rod_z - 0.009
z_bot = center_z - H/2 + 0.014
# Rounded rectangular seam made from four fine straight sections and corner turns.
# Segmented points keep the stitch line attached to the padded face.
pts = []
for cx, cz, a0 in ((x_edge-0.006, z_top-0.006, 0), (-x_edge+0.006, z_top-0.006, 90),
                   (-x_edge+0.006, z_bot+0.006, 180), (x_edge-0.006, z_bot+0.006, 270)):
    for j in range(7):
        a = math.radians(a0 + 90*j/6)
        pts.append((cx + 0.006*math.cos(a), y_front, cz + 0.006*math.sin(a)))
v4.tube("perimeter_stitching", pts, 0.00055, material=stitch, body=visor, resolution=6, cyclic=True)

# Upper hinge sleeve moulded into the visor; exposed rod remains visible outside the shell.
v4.cylinder("visor_hinge_sleeve", 0.0064, 0.380, (0.0, -0.075, rod_z), rot=(0, math.pi/2, 0), material=cover, body=visor)
for i, x in enumerate((-0.194, 0.194)):
    v4.cylinder(f"visor_hinge_endcap_{i}", 0.008, 0.005, (x, -0.075, rod_z),
                rot=(0, math.pi/2, 0), material=cover, body=visor)

# Flush vanity mirror assembly on cabin-facing side, framed with a small thumb slider.
mx, mz = 0.105, center_z + 0.006
v4.box("mirror_bezel", (0.132, 0.004, 0.082), (mx, y_front - 0.0012, mz), bevel=0.009, material=mirror_frame, body=visor)
v4.box("mirror_glass", (0.116, 0.0018, 0.066), (mx, y_front - 0.0041, mz), bevel=0.006, material=glass, body=visor)
# Slider lip below the mirror, touching its bezel edge.
v4.box("mirror_slider_track", (0.116, 0.0025, 0.006), (mx, y_front - 0.004, mz - 0.0375), bevel=0.002, material=cover, body=visor)
v4.box("mirror_slider_tab", (0.018, 0.005, 0.009), (mx + 0.032, y_front - 0.007, mz - 0.0375), bevel=0.002, material=headliner, body=visor)
# A restrained small molded label beside the mirror.
v4.text("mirror_label", "MIRROR", 0.006, loc=(-0.075, y_front - 0.0006, mz + 0.010),
        rot=(math.pi/2, 0, 0), material=cover, body=visor)

# Elastic document/ticket strap, two low-profile anchors and the stretched band.
strap_x = -0.125
for i, z in enumerate((center_z - 0.037, center_z + 0.037)):
    v4.box(f"strap_anchor_{i}", (0.055, 0.003, 0.013), (strap_x, y_front - 0.0012, z), bevel=0.003, material=cover, body=visor)
v4.box("ticket_strap", (0.050, 0.004, 0.052), (strap_x, y_front - 0.0034, center_z), bevel=0.004, material=strap_mat, body=visor)
# Small center pull stitch on the band.
v4.box("strap_stitch", (0.002, 0.001, 0.043), (strap_x, y_front - 0.0058, center_z), bevel=0.0005, material=stitch, body=visor)

# Bracket rotates down from the roof. Visor rotates on the rod and can slide laterally.
v4.joint("bracket_hinge", "revolute", roof, bracket, origin=(-0.188, 0.0, 0.215), axis=(1, 0, 0),
         limits=(-math.radians(8), math.radians(100)))
v4.joint("visor_mount_slide", "generic", bracket, visor, origin=(0.0, -0.075, rod_z), axis=(0, 1, 0),
         xdir=(1, 0, 0), dofs=[("rotX", (-math.radians(8), math.radians(100))), ("transX", (-0.055, 0.055))])

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