// dependency: https://github.com/revarbat/BOSL
include <BOSL/constants.scad>
use <BOSL/transforms.scad>
use <BOSL/shapes.scad>
use <BOSL/masks.scad>
use <BOSL/sliders.scad>

// DEBUG:
//validation = 1;
//xray = 1;

// EXPORT:
MODEL_STRAP_PLATFORM_BASE=10;
MODEL_STRAP_PLATFORM_RIB=11;
MODEL_STRAP_PLATFORM_RIB_BRACE=12;
MODEL_STRAP_SECONDARY=13;
export=0; // [0:20]
function is_not_export() = is_undef(export) || export == 0;
function is_export() = !is_not_export();
function is_model_strict(m) = is_export() && export == m;
function is_model(m) = is_not_export() || export == m;

function get_phone_size() = [160, 76.7, 10];

module arm() {
    $fn=$preview ? 10 : 30;
    arm_size=[200, 55, 40];
color("gray")
down(arm_size.z/2) {
        cuboid(arm_size, fillet=16, edges=EDGES_X_ALL);
        left(arm_size.x*0.6) scale([1, 1, 0.8]) yrot(90) cylinder(d1=arm_size.y*1.4, d2=arm_size.y*0.95, h=arm_size.x/2);
    }
}

module strap_platform() {
    base_size=[40, 70, 2];
    rib_thickness=5;
    rib_height=20;
    rib_pos=[base_size.x/2 - rib_thickness, 0, -1.3];
    brace_size=[base_size.x, 3.6, 4.8];
    brace_pos=[0, base_size.y/2 - 4, -1.3];
    module platform_base() {
        cutout_size=[(base_size.x-rib_thickness*2 - 2*(base_size.x/2-rib_pos.x)), (base_size.y - 5), 2*base_size.z];
        difference() {
            down(base_size.z/2) cuboid(base_size, fillet=2, edges=EDGES_Z_ALL);
            down(base_size.z/2) cuboid(cutout_size, fillet=2, edges=EDGES_Z_ALL);
            model_rib_left(socket=true);
            model_rib_right(socket=true);
        }
    }
    
    module platform_rib(socket=false) {
        rib_size=[rib_thickness, base_size.y - 2, rib_height];
        difference() {
            union() {
                difference() {
                    down(rib_size.z/2) scale(socket ? 1.01 : 1) sparse_strut(h=rib_size.z, l=rib_size.y, thick=rib_size.x, strut=1.2, maxang=40, max_bridge=4);
                    arm();
                }
                difference() {
                    intersection() {
                        down(rib_size.z/2) cube(rib_size, center=true);
                        scale(1.07) arm();
                    }
                    down(2) scale(1.0) arm();
                }
            }
            fwd(brace_pos.y) platform_rib_brace(socket=true);
            back(brace_pos.y) platform_rib_brace(socket=true);
        }
    }
    
    module platform_rib_brace(socket=false) {
        down(rib_height - 1.2 - brace_size.z/2) scale(socket ? 1.02 : 1) cuboid(brace_size, fillet=1, edges=EDGES_X_ALL);
    }
    
    module model_rib_left(socket=false) {
        left(rib_pos.x) up(rib_pos.z) platform_rib(socket=socket);
    }
    module model_rib_right(socket=false) {
        right(rib_pos.x) up(rib_pos.z) platform_rib(socket=socket);
    }
    module model_brace_front() {
        fwd(brace_pos.y) up(brace_pos.z) platform_rib_brace();
    }
    module model_brace_back() {
        back(brace_pos.y) up(brace_pos.z) platform_rib_brace();
    }
    
    if (is_model(MODEL_STRAP_PLATFORM_BASE)) platform_base();
    if (is_model(MODEL_STRAP_PLATFORM_RIB)) model_rib_left();
    if (is_model(MODEL_STRAP_PLATFORM_RIB_BRACE)) model_brace_front();
    if (is_not_export()) {
        model_rib_right();
        model_brace_back();
    }
}

module strap_secondary() {
    size=[40, 15, 10];
    size_base=[size.x, size.y];
    size_top=[size.x * 0.9, size.y * 0.4];
    cutout_size=[size.x - 10, size.y*10, size.z - 4];
    module anchor() {
        difference() {
            xrot(180) rounded_prismoid(size1=size_base, size2=size_top, h=size.z, r=5);
            down(size.z/2) cube(cutout_size, center=true);
        }
    }
    anchor_pos=[60, 30, 0];
    module anchor_front() {
        left(anchor_pos.x) fwd(anchor_pos.y) anchor();
    }
    module anchor_back() {
        left(anchor_pos.x) back(anchor_pos.y) anchor();
    }
    
    if (is_model(MODEL_STRAP_SECONDARY)) anchor_front();
    if (is_not_export()) anchor_back();
}

module model_strap_platform() {
    strap_platform();
}
module model_strap_secondary() {
    strap_secondary();
}
module model_arm() {
    if (is_not_export()) down(3) arm();
}

model_strap_platform();
model_strap_secondary();
model_arm();