"""Privacy cap: snaps into the lens ring and physically blinds the camera.

A disc that rests on the ring's flat rim, with a short plug that presses into the eye
aperture (three crush ribs). Printed top-down: the flat top is the first layer.
"""
import os
from build123d import Cylinder, Pos, Rot, Align, chamfer, Axis, export_stl, export_step, Box
import coral_eye as ce

CAP_D = 2 * ce.RING_D / 2 - 0.6          # rests on the ring's flat rim
CAP_T = 1.6
PLUG_D = ce.EYE_D - 0.25                 # clearance; the ribs make the press fit
PLUG_L = ce.RING_BORE_H + 1.0            # grips the ring's bore and 1 mm of the face's eye (centring jig);
                                         # stays 1.4 mm clear of the lens


def make_cap():
    z_top = ce.Z_FRONT + ce.RING_PROUD + CAP_T          # cap top, device frame
    z_rim = ce.Z_FRONT + ce.RING_PROUD                   # ring rim top
    disc = Pos(ce.LENS_X, ce.LENS_Y, z_rim) * Cylinder(radius=CAP_D / 2, height=CAP_T,
                                                       align=(Align.CENTER, Align.CENTER, Align.MIN))
    disc = chamfer(disc.edges().group_by(Axis.Z)[-1], length=0.5)
    z_lip = ce.Z_FRONT + ce.RING_BORE_H                  # top of the ring's straight bore
    plug = Pos(ce.LENS_X, ce.LENS_Y, z_lip - PLUG_L) * Cylinder(radius=PLUG_D / 2, height=z_rim - z_lip + PLUG_L + 0.01,
                                                              align=(Align.CENTER, Align.CENTER, Align.MIN))
    plug -= Pos(ce.LENS_X, ce.LENS_Y, z_lip - PLUG_L - 0.01) * Cylinder(radius=PLUG_D / 2 - 1.2, height=PLUG_L + 0.6,
                                                                       align=(Align.CENTER, Align.CENTER, Align.MIN))
    cap = disc + plug
    rib_r = 0.25
    rib_c = ce.EYE_D / 2 + 0.12 - rib_r                 # crush ribs reach 0.12 mm past the aperture
    for a in (0, 120, 240):
        cap += Pos(ce.LENS_X, ce.LENS_Y, z_lip - PLUG_L) * Rot(0, 0, a) * Pos(rib_c, 0, 0) * Cylinder(
            radius=rib_r, height=PLUG_L, align=(Align.CENTER, Align.CENTER, Align.MIN))
    # pull tab
    cap += Pos(ce.LENS_X + CAP_D / 2 + 2.0, ce.LENS_Y, z_rim) * Box(5.0, 6.0, CAP_T, align=(Align.CENTER, Align.CENTER, Align.MIN))
    return cap, z_top


if __name__ == "__main__":
    cap, z_top = make_cap()
    out = ce.OUT_DIR
    export_step(cap, os.path.join(out, "CoralEye_PrivacyCap.step"))
    printable = Pos(0, 0, z_top) * Rot(180, 0, 0) * Pos(-ce.LENS_X, -ce.LENS_Y, 0) * cap
    export_stl(printable, os.path.join(out, "CoralEye_PrivacyCap.stl"), tolerance=0.005, angular_tolerance=0.1)
    ring, face = ce.make_lens_ring(), ce.make_face("skin")
    i1, i2 = cap & ring, cap & face
    print("cap volume %.2f cm3; overlap with ring %.3f mm3 (ribs), with face %.3f mm3"
          % (cap.volume / 1000, i1.volume if i1 else 0, i2.volume if i2 else 0))
    pcb, lens, conn = ce.make_camera_mock()
    i3 = cap & lens
    print("cap vs lens: %.3f mm3" % (i3.volume if i3 else 0.0))
    export_stl(cap, os.path.normpath(os.path.join(out, "..", "render", "meshes", "cap.stl")), tolerance=0.005, angular_tolerance=0.1)
