// asdfadsf

//radius = product_of_the_triangle_side_lengths / area_of_the_triangle_multiplied_by_4;

FN=80*1;

width_shortest = 46.5;
width_longest = 55.0;
height = 40;
wall_thickness = 3;

max_length = 30;

socket_diameter = 40;
socket_distance_from_edge = 5 + (width_longest-width_shortest)/2;

wire_diameter = 8;
wire_cover_diameter = 20;

switch_width = 20 + 100; // enough not to leave material hanging
switch_length = 30;
switch_distance_from_edge = 5 + (width_longest-width_shortest)/2;

wireend = 0; // 0 or 1
withswitch = 0; // 0 or 1

// Gotta do some mathematics

triangle_height = (width_longest - width_shortest) / 2;
triangle_width = height / 2;

slanted_side = sqrt(pow(triangle_height, 2) + pow(triangle_width,2));

product_triangle_sides = slanted_side * slanted_side * height;
triangle_area = (height*triangle_height)/2;

radius_side = product_triangle_sides / (4 * triangle_area);

echo(str("Radius: ", radius_side));

module body(radius_sides, body_height, body_width, inner){
	intersection(){
		intersection(){
			intersection(){
				translate([0,0,-body_height/2]){
					cube([body_width,100,body_height]);
				}
					translate([radius_sides,0,0]){
						rotate([-90,0,0]){
							cylinder(h=100, r=radius_sides, $fn=FN);
						}
					}
			}
			translate([body_width-radius_sides,0,0]){
				rotate([-90,0,0]){
					cylinder(h=100, r=radius_sides, $fn=FN);
				}
			}
		}
		union(){
			translate([0,radius_sides,-body_height/2]){
				cube([body_width,100,body_height]);
			}
			if (inner == 1) {
				translate([0,radius_sides,0]){
					rotate([0,90,0]){
						cylinder(h=100, r=radius_sides, $fn=FN);
					}
				}
			} else {
				translate([0,0,-body_height/2]){
					cube([body_width,100,body_height]);
				}
			}
		}
	}
}

module shell(){
	intersection(){
		difference(){
			body(radius_side + wall_thickness*2, height + wall_thickness*2, width_longest + wall_thickness*2);
			translate([wall_thickness, wall_thickness, 0]){
				body(radius_side, height, width_longest, 1);
			}

			if (withswitch == 0) {
				// substract socket
				translate([(width_longest + wall_thickness*2)/2, socket_distance_from_edge+socket_diameter/2, (height + wall_thickness*2)/2 - wall_thickness*2]){
					cylinder(h=wall_thickness*3, r=socket_diameter/2, $fn=FN);
				}
			} else {
				translate([(width_longest + wall_thickness*2)/2 - switch_length/2, switch_distance_from_edge, (height + wall_thickness*2)/2 - wall_thickness*2]){
					cube([switch_length, switch_width, wall_thickness*3]);
				}
			}

			if (wireend == 1) {
				// substract wire cover bit
				translate([(width_longest + wall_thickness*2)/2, - wall_thickness,  0]){
					rotate([270, 0, 0]){
						cylinder(h=wall_thickness*3, r=wire_cover_diameter/2, $fn=FN);
					}
				}
				// and substract space for the wire to be able to fit the cover
				translate([(width_longest + wall_thickness*2)/2 - wire_diameter/2, -1, -(height+wall_thickness*2)/2-1]){
					cube([wire_diameter, max_length+10, (height + wall_thickness*2)/2 + 1]);
				}
			}
		}
		translate([0,0,-(height+wall_thickness*2)/2]){
				cube([width_longest+ wall_thickness*2,max_length,height+wall_thickness*2]);
		}
	}
}

module screwdown(){
	difference(){
		cube([15+(width_longest-width_shortest)/2+wall_thickness,15,wall_thickness]);
		translate([7.5,7.5,0]){
			screw_hole();
		}
	}
}

module screw_hole() {
	// screw head that I measured maxes at 7mm
	// screw thread is 3.5 mm
	head = 7;
	thread = 3.5 + 0.5;

	translate([0,0,-20]){
		cylinder(h=30, r=thread/2, $fn=FN);
	}
	translate([0,0,wall_thickness-(thread/2)]){
		cylinder(h=head-(thread/2), r1=thread/2, r2=head);
	}
}

module assembly(){
	shell();
	translate([-15,0,- height/2 -wall_thickness]){
		screwdown();
	}

	translate([width_longest+wall_thickness*2+15,0,- height/2 -wall_thickness]){
		mirror([1,0,0]){
			screwdown();
		}
	}
}

rotate([90,0,0]){
	translate([0,0,height/2+wall_thickness]){
		assembly();
	}
}
