use <../../WebWeaver/scad/Util.scad>;



$fs = 0.5;
$fa = 1.0;



pcb_w      =      40.5;
pcb_h      =      67.5;
pcb_r      =         5;  // edge radius
pcb_off    =       0.4;
pcb_t      =      5 +1;  // thickness including LED and inductor
pcb_sc_off =         5;  // offset of screw holes from edge
pcb_conn_w =        20;
pcb_conn_h =         3;

box_str    =         3;  // Material strength
box_h      = pcb_t + 2;


module pcb(w=pcb_w, h=pcb_h, r=pcb_r, off=pcb_off) {

  _w = w + 2*off;
  _h = h + 2*off;
  _r = r +   off;

  hull() {

    // function of WebWeaver
    roundRect(_w, _h, _r, true, true);
  
    translate([0, -_h/2 +1 -pcb_conn_h, 0]) square([pcb_conn_w, pcb_conn_h + 1], center=true);
  
  }
}


// height means the overall "length" along the z-axis
module pcb3D(wi, hi, ri, height=1.6, off=pcb_off) {

  linear_extrude(height) pcb(wi, hi, ri, off);
}


module body(wi, hi, ri, height=pcb_t, off=pcb_off, _str=box_str) {


  // clipDisplacementX
  clDsX = wi/2 + off + _str;

  difference() {
    pcb3D(wi, hi, ri, height=height, off=_str);
    
    translate([0, 0, _str]) pcb3D(wi, hi, ri, height=height, off=off);
    
    //for (tx=[-clDsX, clDsX]) translate([tx, 0, height/2]) rotate([0, 0, 0]) clipReceptacle();
  }
}


module PCBMockUp(w=pcb_w, h=pcb_h, r=pcb_r, off_xy=pcb_sc_off) {
  
  w2 = w / 2;
  h2 = h / 2;
  
  color([0.1, 0.1, 0.1], 1.0) difference() {
    roundRect(w, h, r, true, true);
    
    for (tx=[-w2 + off_xy, w2 - off_xy], ty=[-h2 + off_xy, h2 - off_xy]) translate([tx, ty, 0]) circle(d=3.2);
  }
}



// WARNIN: The clip is in an abandoned state at the time.
// Code will stay here only for reference for future projects
// reason: I don't really see my printer doing this itty-gritty kinda stuff

// TODO: is "rastnase" really "clip" in english???

// The female form of the clip connection
//
// width, height and depth of the pocket
module clipReceptacle(w=10, h=3, d=2) {
  
  _y = 0.25;  // TODO: calculate from angle!!!!
  
  hd = h - d;  // TODO: calculate from angle!!!
  
  translate([-d/2, w/2, 0]) rotate([90, 0, 0]) linear_extrude(w) polygon([
    [0,   0],
    [d, -_y],
    [d,  hd],
    [0,   h]
  ]);
  
}


module triangularClipReceptacle(w=20, h=3, d=3) {
  
  h2 = h / 2;

  translate([0, w/2, 0]) rotate([90, 0, 0]) linear_extrude(w) polygon([
    [0, -h2],
    [0,  h2],
    [d,   0]
  ]);
  
}

module triangularClip(w=20, h=3, d=3, length=10, _str=3) {
  
  h2   =    h / 2;
  l    =   length;

  _lx  =     0.25;  // TODO: calculate from angle!!!!
  _x   =  l * _lx;
  _hlx = h2 * _lx;
  
  translate([_str - 0.01, 0, h2])  triangularClipReceptacle(w, h, d);

  translate([0, w/2, 0]) rotate([90, 0, 0]) linear_extrude(w) polygon([
    [   0,       -l],
    [ -_x, h + _hlx],
    [_str,        h],
    [_str,        0],
    [_str,       -l],
  ]);
  
}
triangularClip();


module minkowskiDemonstration() {
  translate([-70, -25, 0]) {
    rotate([0, 0, 90]) {
      color([10 /255, 230 /255, 20 /255]) {
        cube([50, 20, 10]);
        cylinder(d1=0, d2=10, h=5);
      }

      translate([0, 0, 15]) minkowski() {
        cube([50, 20, 10]);
        cylinder(d1=0, d2=10, h=5);
      }
    }
  }
}


module outerRing(w=pcb_w, h=pcb_h, r=pcb_r, off=pcb_off, _str=box_str) {

  s2 = _str/2;

  minkowski() {
    difference() {
      pcb3D(w,    h,    r, off=off);
      pcb3D(w-s2, h-s2, r, off=off);
    }
    
    cylinder(d1=0, d2=10, h=10, $fs=1.0, $fa=0.7);
  }
}


TpOrChamfer = 3;

module TransportProtection(w=pcb_w, h=pcb_h, r=pcb_r, height=pcb_t, off=pcb_off, _str=box_str, _boxH=box_h) {

  _h = _boxH + 2;

  difference() {
    body(w, h, r, _h, off, _str);

    translate([0, 0, _h-TpOrChamfer]) outerRing(w, h, r, off, _str);

    translate([0, 0, 5.5]) rotate([0, 180, 0]) outerRing(w, h, r, _str+1, _str);

  }

}


/*if ($preview) {
  
  minkowskiDemonstration();

  translate([0, 0,box_str]) PCBMockUp();

  TransportProtection();

} else {
  
  TransportProtection();
  
}
*/