"""Articulated retractable USB-A drive. Run with Blender's background Python."""
import math
import os
import sys

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

v4.reset()

# Materials: molded shell, low-friction carriage, board, USB shield and contacts.
shell_mat = v4.plastic("charcoal_molded_polycarbonate", (0.055, 0.065, 0.075), roughness=0.34, texture=0.12)
panel_mat = v4.plastic("top_shell_satin_black", (0.09, 0.105, 0.12), roughness=0.31, texture=0.08)
rail_mat = v4.rubber("POM_slider_rails", (0.025, 0.03, 0.034), roughness=0.63)
slider_mat = v4.plastic("slider_button_black", (0.12, 0.14, 0.15), roughness=0.32, texture=0.1)
steel = v4.metal("USB_plug_brushed_stainless", (0.72, 0.75, 0.78), roughness=0.25, brushed=True)
steel_dark = v4.metal("stamped_shield_edges", (0.43, 0.47, 0.5), roughness=0.32, brushed=False)
gold = v4.metal("gold_plated_contacts", (0.88, 0.58, 0.16), roughness=0.22, brushed=False)
pcb_mat = v4.mat("green_fiberglass_PCB", (0.025, 0.19, 0.10), metallic=0.1, roughness=0.52)
chip_mat = v4.mat("controller_epoxy", (0.025, 0.03, 0.034), roughness=0.38)
cream = v4.plastic("USB_insulator", (0.78, 0.75, 0.66), roughness=0.42, texture=0.04)
white = v4.mat("warm_white_ink", (0.8, 0.82, 0.79), roughness=0.65)

# Housing is the sole root body. The narrow open central slot lets the single
# moving carriage's thumb pad pass through while its lower sled remains inside.
housing = v4.body("housing")
v4.box("shell_floor", (0.052, 0.018, 0.0015), (-0.002, 0, 0.001), bevel=0.0012, material=shell_mat, body=housing)
for side in (-1, 1):
    sy = side * 0.00875
    v4.box(f"housing_side_wall_{side}", (0.049, 0.0025, 0.0085), (-0.0015, sy, 0.0055),
           bevel=0.0014, material=shell_mat, body=housing)
    # Top deck rails flank the moving thumb slot and are molded into the shell.
    panel = v4.box(f"top_deck_{side}", (0.050, 0.0069, 0.0014), (-0.0015, side * 0.00655, 0.01045),
                   bevel=0.0013, material=panel_mat, body=housing)
# Close the shell across the back of the slider slot and over the nose of the
# retracted connector. The 6.2 mm center track is just wide enough for the pad.
v4.box("rear_slot_bridge", (0.010, 0.0062, 0.0014), (-0.0215, 0, 0.01045),
       bevel=0.0010, material=panel_mat, body=housing)
v4.box("nose_top_cover", (0.007, 0.0062, 0.0014), (0.020, 0, 0.01045),
       bevel=0.0008, material=panel_mat, body=housing)

# Rounded tail and nose bezel frame the open plug exit. The nose rails leave a
# 14 mm wide opening around the 12 mm USB-A shield.
v4.box("rear_end_cap", (0.003, 0.018, 0.0095), (-0.028, 0, 0.0055), bevel=0.0014, material=shell_mat, body=housing)
v4.box("nose_bezel_lower", (0.002, 0.018, 0.0018), (0.0245, 0, 0.0019), bevel=0.0005, material=panel_mat, body=housing)
for side in (-1, 1):
    v4.box(f"nose_bezel_jamb_{side}", (0.002, 0.002, 0.007), (0.0245, side * 0.008, 0.0055),
           bevel=0.0005, material=panel_mat, body=housing)

# Fixed guide channel and POM runners. Their ends stop short of the moving USB
# shield and the runs continue beneath the carriage throughout its travel.
v4.box("slider_track_base", (0.039, 0.007, 0.0012), (-0.005, 0, 0.0024), bevel=0.0004, material=rail_mat, body=housing)
for side in (-1, 1):
    v4.box(f"track_rail_{side}", (0.040, 0.0010, 0.0018), (-0.005, side * 0.0031, 0.0038),
           bevel=0.00035, material=rail_mat, body=housing)

# Stationary electronics are tucked along the right rear side of the housing,
# outside the moving sled's 7 mm wide path.
v4.box("PCB_substrate", (0.016, 0.0045, 0.0010), (-0.019, 0.0065, 0.0044), bevel=0.00025, material=pcb_mat, body=housing)
v4.box("flash_controller", (0.0042, 0.0036, 0.0011), (-0.019, 0.0065, 0.00545), bevel=0.00035, material=chip_mat, body=housing)
v4.box("NAND_package", (0.0042, 0.0034, 0.0008), (-0.0242, 0.0065, 0.0053), bevel=0.0003, material=chip_mat, body=housing)
for i in range(4):
    v4.box(f"PCB_gold_finger_{i}", (0.00055, 0.0032, 0.00008), (-0.012 + i * 0.0011, 0.0065, 0.00496),
           bevel=0.00005, material=gold, body=housing)
for x in (-0.025, -0.022, -0.016, -0.013):
    v4.box(f"PCB_trace_{round((x+0.03)*1000)}", (0.00035, 0.0022, 0.00006), (x, 0.0065, 0.00497),
           bevel=0, material=gold, body=housing)

# Fixed eyelet reinforcement and a formed metal lanyard loop at the tail.
for side in (-1, 1):
    v4.box(f"lanyard_boss_{side}", (0.003, 0.009, 0.002), (-0.028, side * 0.0055, 0.006),
           bevel=0.0005, material=panel_mat, body=housing)
v4.torus("rear_lanyard_loop", 0.0043, 0.00065, loc=(-0.033, 0, 0.006), rot=(math.pi / 2, 0, 0),
         material=steel_dark, body=housing)
v4.cylinder("lanyard_eyelet", 0.0017, 0.019, loc=(-0.029, 0, 0.006), rot=(math.pi / 2, 0, 0),
            material=steel_dark, body=housing)

# Moving body: the control, guided carrier, shield, insulating tongue and
# contacts are all one rigid assembly. At rest, the shield is inside the nose.
carriage = v4.body("connector_carriage")
v4.box("carriage_sled", (0.031, 0.0062, 0.0020), (-0.004, 0, 0.0048), bevel=0.0008, material=slider_mat, body=carriage)
v4.box("slider_neck", (0.025, 0.0040, 0.0024), (0.006, 0, 0.0066), bevel=0.0007, material=slider_mat, body=carriage)
v4.box("thumb_button_support", (0.006, 0.0040, 0.0043), (-0.006, 0, 0.00965), bevel=0.0005, material=slider_mat, body=carriage)
button = v4.box("thumb_slider_button", (0.012, 0.0058, 0.0022), (-0.009, 0, 0.0120), bevel=0.0011, material=slider_mat, body=carriage)
for i in range(5):
    x = -0.010 + i * 0.0015
    v4.box(f"button_grip_rib_{i}", (0.00065, 0.0052, 0.00025), (x, 0, 0.0132),
           bevel=0.00012, material=panel_mat, body=carriage)

# USB-A plug: a stamped 0.4 mm stainless enclosure, open at its +X mating end.
# Four sheet walls and a rear shoulder leave a real hollow cavity instead of a
# solid metal block. The dielectric tongue and contacts sit well inside that rim.
wall = 0.0004
plug_x0, plug_x1 = 0.0, 0.020
plug_z0, plug_z1 = 0.00445, 0.00895
plug_y0, plug_y1 = -0.006, 0.006
v4.box("USB_shield_bottom_wall", (plug_x1 - plug_x0, plug_y1 - plug_y0, wall),
       ((plug_x0 + plug_x1) / 2, 0, plug_z0 + wall / 2), bevel=0.00005, material=steel, body=carriage)
v4.box("USB_shield_top_wall", (plug_x1 - plug_x0 - wall, plug_y1 - plug_y0, wall),
       ((plug_x0 + plug_x1 + wall) / 2, 0, plug_z1 - wall / 2), bevel=0.00005, material=steel, body=carriage)
for side in (-1, 1):
    v4.box(f"USB_shield_side_wall_{side}", (plug_x1 - plug_x0 - wall, wall, plug_z1 - plug_z0 - 2 * wall),
           ((plug_x0 + plug_x1 - wall) / 2, side * (0.006 - wall / 2), (plug_z0 + plug_z1) / 2),
           bevel=0.00005, material=steel, body=carriage)
v4.box("USB_shield_closed_rear", (wall, plug_y1 - plug_y0, plug_z1 - plug_z0),
       (plug_x0 + wall / 2, 0, (plug_z0 + plug_z1) / 2), bevel=0.00005, material=steel, body=carriage)

# The tongue sits close to the shield's bottom and stops just short of the
# mating rim. Its contacts are mounted on its upper face, recessed by >1.3 mm.
v4.box("USB_A_insulator_tongue", (0.018, 0.0106, 0.00075), (0.0105, 0, 0.00685),
       bevel=0.00015, material=cream, body=carriage)
for i in range(4):
    y = (i - 1.5) * 0.00155
    v4.box(f"USB_contact_{i+1}", (0.0075, 0.00092, 0.00012), (0.0145, y, 0.007265),
           bevel=0.00004, material=gold, body=carriage)

# Sliding axis is world +X. At maximum travel the metal plug clears the bezel
# by 13 mm; the button remains within the housing's guide slot.
v4.joint("connector_slide", "prismatic", housing, carriage,
         origin=(-0.003, 0, 0.0055), axis=(1, 0, 0), limits=(0, 0.018))

# Screen printed details on the top deck. Text is wrapped to the molded surfaces.
v4.text("capacity_marking", "32 GB", 0.0022, loc=(-0.009, 0.0071, 0.01115),
        material=white, body=housing, on=None, extrude_depth=0.00004)
v4.text("USB_speed_marking", "USB 3.0", 0.0017, loc=(0.009, -0.0071, 0.01115),
        material=white, body=housing, on=None, extrude_depth=0.00004)

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