"""Detailed retractable USB-A flash drive. Run with Blender's Python interpreter."""
import math
import os
import sys
import bpy

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


v4.reset()

# ---- Materials ----------------------------------------------------------------------
shell = v4.plastic("charcoal_polycarbonate", (0.075, 0.09, 0.105), roughness=0.39, texture=0.12)
shell_top = v4.plastic("graphite_shell_panels", (0.11, 0.13, 0.15), roughness=0.39, texture=0.10)
track_black = v4.rubber("track_and_recess_black", (0.025, 0.032, 0.04), roughness=0.65)
blue = v4.plastic("cobalt_slider", (0.018, 0.18, 0.72), roughness=0.3, texture=0.08)
blue_accent = v4.plastic("blue_side_accents", (0.02, 0.16, 0.58), roughness=0.34, texture=0.08)
steel = v4.metal("satin_nickel_steel", (0.70, 0.73, 0.76), roughness=0.30, brushed=True)
dark_metal = v4.metal("dark_stainless_track", (0.25, 0.28, 0.31), roughness=0.38, brushed=False)
gold = v4.metal("gold_plated_contacts", (0.88, 0.60, 0.19), roughness=0.24, brushed=False)
pcb_mat = v4.mat("green_fr4", (0.045, 0.28, 0.13), metallic=0.05, roughness=0.62)
chip_mat = v4.plastic("black_epoxy_components", (0.025, 0.03, 0.035), roughness=0.32, texture=0.04)
white = v4.mat("warm_white_ink", (0.88, 0.90, 0.87), roughness=0.55)
led_mat = v4.emissive("status_led_green", (0.15, 0.9, 0.28), strength=1.4)

# ---- Real-scale envelope (metres) -----------------------------------------------------
# Case: 48 x 18.5 x 8.5 mm.  The metal sleeve is 14.5 x 12 x 4.5 mm and
# advances 14 mm through the open nose.  X is the insertion/slide direction.
CASE_REAR = -0.024
CASE_NOSE = 0.024
CASE_HALF_W = 0.00925
CASE_HALF_H = 0.00425
INNER_HALF_W = 0.00775
INNER_HALF_H = 0.00345
SLOT_HALF_WIDTH = 0.0011
SLOT_REAR = -0.0195
SLOT_FRONT = 0.0075

housing = v4.body("housing")
connector = v4.body("connector_slider")

# ---- Fixed molded shell: lower tray, side cheeks, split roof, end walls ----------------
# The shell surrounds the USB-A sleeve but leaves its travel channel and nose open.
v4.box("shell_lower_tray", (0.048, 0.0155, 0.0008), (0, 0, -0.00385),
       bevel=0.00032, material=shell, body=housing)
for sy, side in ((-1, "left"), (1, "right")):
    v4.box(f"shell_{side}_cheek", (0.048, 0.0015, 0.0069), (0, sy * 0.0085, 0),
           bevel=0.00055, material=shell, body=housing)

# Two rounded roof rails leave a 2.2 mm slot for the narrow slider stalk. Their
# outside edges meet the cheek walls; short center bridges conceal the track
# behind and ahead of the button's full travel envelope.
roof_width = INNER_HALF_W - SLOT_HALF_WIDTH
for sy, side in ((-1, "left"), (1, "right")):
    roof_y = sy * (SLOT_HALF_WIDTH + roof_width / 2)
    v4.box(f"shell_roof_{side}", (0.048, roof_width, 0.0008), (0, roof_y, 0.00385),
           bevel=0.00032, material=shell_top, body=housing)
v4.box("roof_rear_bridge", (SLOT_REAR - CASE_REAR, 2 * SLOT_HALF_WIDTH, 0.0008),
       ((SLOT_REAR + CASE_REAR) / 2, 0, 0.00385),
       bevel=0.00028, material=shell_top, body=housing)
v4.box("roof_nose_bridge", (CASE_NOSE - SLOT_FRONT, 2 * SLOT_HALF_WIDTH, 0.0008),
       ((CASE_NOSE + SLOT_FRONT) / 2, 0, 0.00385),
       bevel=0.00030, material=shell_top, body=housing)

# Rear closure with a genuine round lanyard opening.
rear_wall = v4.box("rear_end_wall", (0.001, 0.0155, 0.0069), (-0.0235, 0, 0),
                   bevel=0.00012, material=shell, body=housing)
hole_tool = v4.cylinder("lanyard_hole_tool", 0.0021, 0.002, (-0.0235, 0, 0),
                         rot=(0, math.pi / 2, 0), segments=40, bevel=0, material=None)
v4.boolean(rear_wall, hole_tool)
# Metal-lined, fine-wire eyelet sits against the rear wall around the bore.
eyelet_points = [(-0.0245, 0.00285 * math.cos(2 * math.pi * i / 64),
                  0.00285 * math.sin(2 * math.pi * i / 64)) for i in range(64)]
v4.tube("rear_lanyard_eyelet", eyelet_points, 0.00052, material=steel, body=housing, cyclic=True)

# ---- Fixed connector aperture and front bezel -----------------------------------------
# The inner step forms a 12.4 x 5.1 mm throat around the 12 x 4.5 mm sleeve.
for sy, side in ((-1, "left"), (1, "right")):
    v4.box(f"nose_aperture_{side}_lip", (0.0014, 0.00155, 0.0048),
           (0.0230, sy * 0.006975, 0), bevel=0.00016, material=dark_metal, body=housing)
for sz, side in ((-1, "lower"), (1, "upper")):
    v4.box(f"nose_aperture_{side}_lip", (0.0014, 0.0124, 0.0009),
           (0.0230, 0, sz * 0.0030), bevel=0.00014, material=dark_metal, body=housing)

# A slim, inset collar steps down to the narrower inner throat. Its larger
# rounded opening and smaller return lip reduce the heavy, square end-frame look.
nose_bezel = v4.box("nose_end_bezel", (0.0006, 0.0173, 0.0075), (0.0243, 0, 0),
                    bevel=0.00024, material=dark_metal, body=housing)
bezel_tool = v4.box("nose_bezel_opening_tool", (0.0012, 0.0136, 0.0060),
                    (0.0243, 0, 0), bevel=0, material=None)
v4.boolean(nose_bezel, bezel_tool)

# A shallow dark well below the actuator makes the long shell slot read as a
# recessed mechanical track.  It ends before the connector sleeve begins.
v4.box("slider_track_well", (SLOT_FRONT - SLOT_REAR, 0.0018, 0.00035),
       ((SLOT_FRONT + SLOT_REAR) / 2, 0, 0.00180),
       bevel=0.00010, material=track_black, body=housing)

# Two fixed stainless guide rails are anchored to the inside faces of the cheeks.
for sy, side in ((-1, "left"), (1, "right")):
    v4.box(f"fixed_slider_rail_{side}", (0.032, 0.0008, 0.00125), ( -0.005, sy * 0.00735, 0.00280),
           bevel=0.00018, material=dark_metal, body=housing)
    for k, x in enumerate((-0.018, -0.005, 0.008)):
        v4.box(f"rail_mount_{side}_{k}", (0.0014, 0.0004, 0.0011), (x, sy * 0.00795, 0.0028),
               bevel=0.00010, material=steel, body=housing)

# ---- Fixed flash-memory board and small components ------------------------------------
# The rear board is supported off the tray and remains clear of the connector's path.
for sx, x in enumerate((-0.0210, -0.0105)):
    for sy, y in enumerate((-0.0030, 0.0030)):
        v4.box(f"pcb_standoff_{sx}_{sy}", (0.0014, 0.0012, 0.00030),
               (x, y, -0.00330), bevel=0.00007, material=dark_metal, body=housing)
v4.box("fr4_flash_board", (0.0140, 0.0090, 0.0008), (-0.0150, 0, -0.00275),
       bevel=0.00022, material=pcb_mat, body=housing)
# The component bases meet the board's upper surface (z = -2.35 mm).
v4.box("nand_flash_package", (0.0052, 0.0064, 0.0010), (-0.0180, 0, -0.00185),
       bevel=0.00025, material=chip_mat, body=housing)
v4.box("usb_controller_package", (0.0038, 0.0044, 0.00085), (-0.0105, 0, -0.001925),
       bevel=0.00020, material=chip_mat, body=housing)
for i, y in enumerate((-0.0033, -0.00165, 0, 0.00165, 0.0033)):
    v4.box(f"pcb_gold_test_pad_{i}", (0.00125, 0.0008, 0.00006), (-0.0132, y, -0.00232),
           bevel=0.000025, material=gold, body=housing)
for i, x in enumerate((-0.0200, -0.0160, -0.0082)):
    v4.box(f"pcb_passive_{i}", (0.0012, 0.0008, 0.00032), (x, 0.0028, -0.00205),
           bevel=0.00008, material=steel, body=housing)

# Fine molding seam and blue side accent rails; both sides are modeled symmetrically.
for sy, side in ((-1, "left"), (1, "right")):
    v4.box(f"side_panel_seam_{side}", (0.038, 0.00010, 0.00016), (0, sy * 0.00925, 0.00215),
           bevel=0.00004, material=track_black, body=housing)
    v4.box(f"side_blue_accent_{side}", (0.014, 0.00014, 0.00034), (0.003, sy * 0.00929, -0.0025),
           bevel=0.00007, material=blue_accent, body=housing)

# ---- Moving connector, carriage, and thumb slider -------------------------------------
# Folded metal sleeve, modeled as five joined sheets around an open nose rather than
# a solid block.  Its outer envelope is x=6..20.5 mm, y=+/-6 mm, z=+/-2.25 mm.
v4.box("usb_shell_top_sheet", (0.0145, 0.0120, 0.00035), (0.01325, 0, 0.002075),
       bevel=0.00010, material=steel, body=connector)
v4.box("usb_shell_bottom_sheet", (0.0145, 0.0120, 0.00035), (0.01325, 0, -0.002075),
       bevel=0.00010, material=steel, body=connector)
for sy, side in ((-1, "left"), (1, "right")):
    v4.box(f"usb_shell_{side}_fold", (0.0145, 0.00035, 0.0038), (0.01325, sy * 0.005825, 0),
           bevel=0.00010, material=steel, body=connector)
v4.box("usb_shell_rear_fold", (0.00035, 0.0113, 0.0038), (0.006175, 0, 0),
       bevel=0.00008, material=steel, body=connector)

# Type-A black dielectric tongue and four plated contact fingers.
v4.box("usb_tongue_anchor", (0.0017, 0.0090, 0.0018), (0.01115, 0, 0),
       bevel=0.00022, material=track_black, body=connector)
v4.box("usb_dielectric_tongue", (0.0102, 0.0088, 0.0013), (0.0154, 0, 0),
       bevel=0.00022, material=chip_mat, body=connector)
for i, y in enumerate((-0.00315, -0.00105, 0.00105, 0.00315)):
    v4.box(f"usb_contact_finger_{i}", (0.0048, 0.00125, 0.00016), (0.01760, y, 0.00073),
           bevel=0.00008, material=gold, body=connector)

# Two rectangular retention apertures in the sleeve roof reveal dark cavities below.
plug_roof = bpy.data.objects.get("usb_shell_top_sheet")
for i, y in enumerate((-0.00375, 0.00375)):
    cutter = v4.box(f"retention_hole_tool_{i}", (0.0028, 0.0019, 0.0009),
                    (0.0163, y, 0.002075), bevel=0, material=None)
    v4.boolean(plug_roof, cutter)
    v4.box(f"retention_hole_dark_floor_{i}", (0.00235, 0.0015, 0.00014),
           (0.0163, y, 0.00173), bevel=0.00003, material=track_black, body=connector)

# A small mounting yoke touches the sleeve's rear fold and carries both guide shoes.
v4.box("connector_rear_yoke", (0.0010, 0.0128, 0.0044), (0.0065, 0, 0),
       bevel=0.00012, material=dark_metal, body=connector)
v4.box("carriage_crossbar", (0.0080, 0.0139, 0.0008), (-0.0100, 0, 0.00270),
       bevel=0.00012, material=track_black, body=connector)
for sy, side in ((-1, "left"), (1, "right")):
    # Narrow side arms join the rear yoke to the carriage shoes while staying
    # just outside the 12 mm wide metal connector sleeve.
    v4.box(f"carriage_side_arm_{side}", (0.0110, 0.00042, 0.00065),
           (0.0005, sy * 0.00620, 0.00255), bevel=0.00008, material=dark_metal, body=connector)
    v4.box(f"carriage_guide_shoe_{side}", (0.0080, 0.00056, 0.00115),
           (-0.0100, sy * 0.00667, 0.00280), bevel=0.00009, material=steel, body=connector)

# Stalk rises through the open roof slot. The button clears the shell by 0.05 mm
# while its guide shoes remain captured beside the fixed internal rails.
v4.box("slider_button_stalk", (0.0030, 0.0015, 0.00125), (-0.0130, 0, 0.003675),
       bevel=0.00016, material=dark_metal, body=connector)
button = v4.box("blue_thumb_slider", (0.0085, 0.0074, 0.00150), (-0.0130, 0, 0.00505),
                bevel=0.00050, material=blue, body=connector)
# Four transverse grip ribs, seated on the button's top surface.
for i, dx in enumerate((-0.0026, -0.0013, 0.0013, 0.0026)):
    v4.box(f"thumb_grip_rib_{i}", (0.00025, 0.0056, 0.00012),
           (-0.0130 + dx, 0, 0.00586), bevel=0.000055, material=blue_accent, body=connector)
# Raised forward arrow on the blue pad; it travels with the connector.
v4.extrude("slider_forward_arrow", [(-0.00085, -0.00075), (0.0010, 0), (-0.00085, 0.00075)],
           0.00009, loc=(-0.0130, -0.00235, 0.00580), bevel=0.000025, material=white, body=connector)

# ---- Surface lettering and small product cues -----------------------------------------
v4.text("top_brand", "NOVA", 0.0025, loc=(0.0010, -0.0049, 0.00425),
        material=white, body=housing, on=bpy.data.objects.get("shell_roof_left"), height=0.00020)
v4.text("top_usb_spec", "USB 3.2", 0.0020, loc=(0.0010, 0.0049, 0.00425),
        material=white, body=housing, on=bpy.data.objects.get("shell_roof_right"), height=0.00018)
v4.text("underside_capacity", "32 GB  /  FLASH", 0.00135,
        loc=(0, 0, -0.00425), rot=(math.pi, 0, 0), material=white, body=housing,
        on=bpy.data.objects.get("shell_lower_tray"), height=0.00015)
# Small green activity lens on the lower side cheek near the rear pocket.
v4.sphere("activity_indicator", 0.00042, loc=(-0.0185, -0.00943, 0.0010),
          segments=20, material=led_mat, body=housing)

# ---- Slide joint -----------------------------------------------------------------------
v4.joint("connector_slide", "prismatic", housing, connector,
         origin=(0, 0, 0), axis=(1, 0, 0), limits=(0.0, 0.014))

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