use <MCAD/boxes.scad>
use <MCAD/regular_shapes.scad>


use <MIDI_Lighter_Mic_Adapter.scad>
include <RP2350_MIDI_Lighter_PCB.scad>
include <RP2350_MIDI_Lighter_Definition.scad>


Show_FAD_Logo 	    = false;
Show_PCB		    = false;
Audio_Jack_Present	= false;
Debug_Present	    = true;
$preview 		    = true;



if(Show_PCB)
{
	PCB(Audio_Jack_Present);
	translate([0,0,+PCB_T/2]) Spacer_Top();
	translate([0,0,-PCB_T/2]) Spacer_Bot();
}

// Only for display purposes
// translate([0, 0, eps])	

// Top();
// Top_Wall();

// Bottom();
// Bottom_Wall();
// rotate([0, 180, 0])
// translate([0, 0, eps]) Display_GC9A01A_Cover();
MIDI_Plug_Solder_Guide();

module MIDI_Plug_Solder_Guide()
{
    Main_Block_X = PCB_W + 2*Cover_O;
    Main_Block_Y = 13.335 + 2;
    
    difference()
    {
        translate([PCB_W/2, , 0])
        difference()
        {
            translate([0, -Main_Block_Y/2+MIDI_Plugs[0][1]-eps, 2]) cube([Main_Block_X, Main_Block_Y, 8], center=true);
            translate([0, PCB_H/2, -50 + PCB_T/2]) cube([1*PCB_W, PCB_H, 100], center=true);
        }

        for (i = MIDI_Plugs) {
		    translate(i) {
                translate([0, -5, 10.2/2])                  cube([18.2, 10.0, 10.20], center=true);
                translate([0, -0.5-10-0.5+eps, 20.35/2])    cube([20.5,  2.0, 20.35], center=true);
            }	
	    } 
    }
}


module Display_GC9A01A_Cover()
{
	color([0.4, 0.4, 1.0], 1.0)
	translate([0, 0, +Spacer_Top_Height + PCB_T/2 + Cover_T_T + 0.0]) 
	difference()
	{
		translate(GC9A01A) GC9A01A_Cover_Plate(1.0, 1, false);
		Screw_Pits_M3(10);
	}
}


module Top()
{
	color([0.2, 0.8, 0.2])
	difference()
	{
		translate([0, 0, +Spacer_Top_Height + PCB_T/2]) 
		union()
		{
			difference()
			{
				union()
				{
					Top_Plate();
				}
				
				Screw_Holes_M3();
				// Top_Wall_Screw_Plates(0.6, -eps, true);
				translate([0, 0, +Cover_T_T]) 	Screw_Pits_M3(1);
				translate(Rotary_Encoder)		Screw_Drill(7.5);
				translate(GC9A01A)				GC9A01A_Cutout(5*Cover_T_T);
				
				translate([0, 0, +Cover_T_T])
				{
					translate([0, 0, -1.0])		// Penetration Depth
					translate(Rotary_Encoder)	cylinder(d=15, h=100, center=false, $fn = $preview ? 32 : 100);
					
					translate([0, 0, -0.5])		// Penetration Depth
					translate(GC9A01A)			GC9A01A_Cover_Plate(1, 1.01, true);
				}

				translate([0, 0, -Spacer_Top_Height - PCB_T/2])  PCB_Cutouts_Top_Plate();
			}
			
			for (i = MIDI_Plugs) { 
				translate([0, 0, Cover_T_T]) translate(i) MIDI_Plug_Arc();
			}

			if(Show_FAD_Logo == true) {
				translate([PCB_W/2, 9.5, Cover_T_T])
				scale([0.02, 0.02, 2])
				intersection() {
					translate([0, 0, 0.5]) cube([500, 500, 1], center=true);
					import("FAD_Logo.stl", convexity=3, $fn = $preview ? 32 : 100);  
				}
			}
		}
	}
}

module Top_Plate()
{
	Cover_O_Factor = 0.9;
	translate([PCB_W/2, PCB_H/2, Cover_T_T/2])
	// roundedBox([PCB_W + 2*Cover_O, PCB_H + 2*Cover_O, Cover_T_T], 2, true, $fn = $preview ? 32 : 100);
	roundedBox([PCB_W + Cover_O_Factor*Cover_O, PCB_H + Cover_O_Factor*Cover_O, Cover_T_T], 1.0, true, $fn = $preview ? 32 : 100);
}

module Top_Wall()
{
	union()
	{
		difference()
		{
			union()
			{
				translate([PCB_W/2, PCB_H/2, +Wall_T_H/2])
				difference() {
					union() {
                        roundedBox(	[PCB_W + 2*Cover_O		, PCB_H + 2*Cover_O			, 1*Wall_T_H], 2, true, $fn = $preview ? 32 : 100);
                        
                        Overlao_H = Wall_TB_Overlap - Wall_PCB_GAP; // Remove a bit for printing margin
                        translate([0, 0, -Wall_T_H/2 - Overlao_H/2 - eps])	cube([PCB_W + 1*Cover_O, PCB_H + 1*Cover_O, Overlao_H], center=true);

					}
                    cube(		[PCB_W + 0*Cover_O + 0.5, PCB_H + 0*Cover_O + 0.5	, 2*Wall_T_H], center=true);

					translate([0, 0, +Wall_T_H/2 - Cover_T_T/2])	cube([PCB_W + 1*Cover_O, PCB_H + 1*Cover_O, Cover_T_T + eps], center=true);
				}

				union()
				{
					// Around the Power Plug Connector
					Top_Wall_Add_Inner_Wall("top", 6, 21, 2, 40.5);
					Top_Wall_Add_Inner_Wall("right", 6, 21, 2, 55.88);

					// Around the LED Connector
					X_Offset = 6.25;
					Top_Wall_Add_Inner_Wall("top", 6, 12.0, 2, 19.05-X_Offset);
					Top_Wall_Add_Inner_Wall("top", 6, 12.0, 2, 19.05+X_Offset);
					translate([15, 0, 0]) Top_Wall_Add_Inner_Wall("left", 3.5, 12, 2, 65.5);
				}
			}



			translate([PCB_W/2, PCB_H/2, +Wall_T_H/2])
			{
				// For the MOLEX Connector
                Top_Wall_Remove_Part("top", Wall_T_H-Cover_T_T/2, 5, MOLEX[0]);
				Top_Wall_Remove_Part("top", 8+PCB_T/2, 10.5, MOLEX[0]);

				if(Audio_Jack_Present) {
                    Top_Wall_Remove_Part("bottom", 3.5, 10, Audio_Jack[0]);
                }
				Top_Wall_Remove_Part("right", 6.5, 3.5, Button[1]);
				translate([-Cover_O, 0, 0]) Top_Wall_Remove_Part("right", PCB_T/2 + 8.0, 8.0, Button[1]);
				Top_Wall_Remove_Part("right", PCB_T/2+2.5, 16, USB_C[1]);

				// Cutout for Debug Connector
				if(Debug_Present) {
					Top_Wall_Remove_Part("left", PCB_T/2+6, 7, 28.575);
				}
			}
			translate([7, -6, Wall_T_H-3+eps]) translate(Mounting_Holes[0]) rotate([0, 0, 0]) cube([8, 8, 2], center=true);
			PCB_Cutouts_Top_Wall(Audio_Jack_Present);
		}

		difference()
		{
			Top_Wall_Screw_Plates(2.0, PCB_T/2 + Spacer_Top_Height - 2);
			translate([4, 4, Wall_T_H/2]) translate(Mounting_Holes[3]) rotate([0, 0, 45]) cube([8, 8, Wall_T_H], center=true);
		}

		translate([0, 0, Cover_T_T]) Top_Wall_Add_Inner_Wall("right", Wall_T_H, Cover_O/2, 1, Power_Plug[1]+2.5);
	}
}

module Top_Wall_Remove_Part(wall_section, height, length, position)
{
	Wall_T = Cover_O/2;
	// Wall_XY = 0;
	Wall_XY =  	wall_section == "right" ? +PCB_W/2+Cover_O/2+Wall_T/2 :
				wall_section == "left" 	? -PCB_W/2-Cover_O/2-Wall_T/2 :
				wall_section == "top" 	? +PCB_H/2+Cover_O/2+Wall_T/2 : 
				wall_section == "bottom"? -PCB_H/2-Cover_O/2-Wall_T/2 : 0;

	if(wall_section == "right" || wall_section == "left")
	{
		translate([Wall_XY, position-PCB_H/2, -Wall_T_H/2+height/2 - Wall_TB_Overlap/2 - eps])
		cube([3*Wall_T, length, height + Wall_TB_Overlap], center=true);
	}
	else if(wall_section == "top" || wall_section == "bottom")
	{
		translate([position-PCB_W/2, Wall_XY, -Wall_T_H/2+height/2 - Wall_TB_Overlap/2 - eps])
		cube([length, 3*Wall_T, height + Wall_TB_Overlap], center=true);
	}	
}

module Top_Wall_Add_Inner_Wall(wall_section, height, length, width, position)
{
	Wall_XY =  	wall_section == "right" ? +PCB_W + Cover_O - length/2 - eps :
				wall_section == "left" 	? -Cover_O + length/2 - eps :
				wall_section == "top" 	? +PCB_H + Cover_O - length/2 - eps : 
				wall_section == "bottom"? -Cover_O + length/2 + eps : 0;

	if(wall_section == "right" || wall_section == "left")
	{
		translate([Wall_XY, position, PCB_T/2+Spacer_Top_Height-height/2])
		cube([length, width, height], center=true);
	}
	else if(wall_section == "top" || wall_section == "bottom")
	{
		translate([position, Wall_XY, PCB_T/2+Spacer_Top_Height-height/2])
		cube([width, length, height], center=true);
	}	
}

module Top_Wall_Screw_Plates(height, height_offset, cutout=false)
{
	Screw_Plate_Size = [
		[10, 10, height],
		[10,  9, height],
		[ 0,  0, 0],
		[10,  7, height],
		[16,  7, height]
	];

	Screw_Plate_Offset = [
		[-1, +1, height/2 + height_offset],
		[+1, +2, height/2 + height_offset],
		[ 0,  0, height/2 + height_offset],
		[-1, 0, height/2 + height_offset],
		[+4, 0, height/2 + height_offset]
	];
	
	for (i = [0:4]) {
		translate(Mounting_Holes[i]) 

		if(cutout) {
			 translate(Screw_Plate_Offset[i]) scale([1.1, 1.1, 1]) cube(Screw_Plate_Size[i], center=true);
		}
		else {
			difference() {
				translate(Screw_Plate_Offset[i]) roundedBox(Screw_Plate_Size[i], 2, true);
				Screw_Drill(Drill_M3);
			}
		}
	}
}



module Bottom()
{
	color([0.8, 0.8, 0.0])
	difference()
	{
		translate([0, 0, -Spacer_Bot_Height - PCB_T/2 - 1])
		{
			difference()
			{
				translate([0, 0, -Cover_B_T])
				union()
				{
					difference()
					{
						Bottom_Plate();
						translate([PCB_W/2, PCB_H/2, 0]) 	KandM_14510_Mount(1, true);
					}
					translate([PCB_W/2, PCB_H/2, 5.5]) 	KandM_14510_Mount();
                    translate(Mounting_Holes[2]) translate([0, 0, Cover_B_T-eps]) cylinder(h=1, d=10, , $fn = $preview ? 32 : 100);
				}

				Screw_Holes_M3();
				translate([0, 0, -Cover_B_T]) mirror([0, 0, 1]) Screw_Pits_M3(1);
				
			}
		}

		PCB_Cutouts_From_Bottom();
	}
}

module Bottom_Wall()
{
	translate([0, 0, 0])
    union()
    {
        difference()
		{
			translate([PCB_W/2, PCB_H/2, -Wall_B_H/2])
			union()
			{
				difference()
				{
					roundedBox( [PCB_W + 2*Cover_O      , PCB_H + 2*Cover_O         , 1*Wall_B_H], 2, true, $fn = $preview ? 32 : 100);
					cube(		[PCB_W + 0*Cover_O + 0.5, PCB_H + 0*Cover_O + 0.5	, 2*Wall_T_H], center=true);
					
                    translate([0, 0, +Cover_B_T/2 - Wall_B_H/2 - eps])
					cube(		[PCB_W + 1*Cover_O + 0.5, PCB_H + 1*Cover_O + 0.5	, Cover_B_T], center=true);

                    Overlao_H = Wall_TB_Overlap + Wall_PCB_GAP; // Add a bit for printing margin
                    translate([0, 0, -Overlao_H/2 + Wall_B_H/2 + eps])
					cube(		[PCB_W + 1*Cover_O + 0.5, PCB_H + 1*Cover_O + 0.5	, Overlao_H], center=true);
				}

				Bottom_Wall_Add_Part("right"	, PCB_T/2		, 9			, Power_Plug[1]-2.4);
				Bottom_Wall_Add_Part("right"	, PCB_T/2+2.5	, 3			, Button[1]);
				Bottom_Wall_Add_Part("right"	, PCB_T/2+2.0	, 16		, USB_C[1]);
				Bottom_Wall_Add_Part("top"		, PCB_T/2		, 10		, MOLEX[0]);
                if(Audio_Jack_Present == true) {
				    Bottom_Wall_Add_Part("bottom"	, 3.2		, 9.5		, Audio_Jack[0]);
                }
			}
			PCB_Cutouts_From_Bottom();
		}
        Screw_Plate_T = 1.0;

        translate([0, 0, -Screw_Plate_T])
        Top_Wall_Screw_Plates(Screw_Plate_T, -PCB_T/2 - Spacer_Bot_Height);
    }
}

module Bottom_Wall_Add_Part(wall_section, height, length, position)
{
	Wall_T = Cover_O/2;
	// Wall_XY = 0;
	Wall_XY =  	wall_section == "right" ? +PCB_W/2+Cover_O/2+Wall_T/2 :
				wall_section == "left" 	? -PCB_W/2-Cover_O/2-Wall_T/2 :
				wall_section == "top" 	? +PCB_H/2+Cover_O/2+Wall_T/2 : 
				wall_section == "bottom"? -PCB_H/2-Cover_O/2-Wall_T/2 : 0;

	if(wall_section == "right" || wall_section == "left")
	{
		translate([Wall_XY, position-PCB_H/2, Wall_B_H/2 + height/2 - eps])
		cube([Wall_T, length, height], center=true);
	}
	else if(wall_section == "top" || wall_section == "bottom")
	{
		translate([position-PCB_W/2, Wall_XY, Wall_B_H/2 + height/2 - eps])
		cube([length, Wall_T, height], center=true);
	}	
}

module Bottom_Plate()
{
	translate([PCB_W/2, PCB_H/2, Cover_B_T/2])
	roundedBox([PCB_W + 1*Cover_O, PCB_H + 1*Cover_O, Cover_B_T], 1, true, $fn = $preview ? 32 : 100);

}

module MIDI_Plug_Cutout()
{
	union()
	{
		translate([0, -10/2, 0]) 		cube([18.5, 10+0.0, 2*Cover_T_T], center=true);
		translate([0, -(10+10/2), 0]) 	cube([22.5, 10+0.1, 2*Cover_T_T], center=true);
		translate([0, 2/2, 0]) 			cube([11.0,  2+0.1, 2*Cover_T_T], center=true);
		translate([0, 4/2, 0]) 			cube([ 3.5,  2+0.1, 2*Cover_T_T], center=true);
	}
}

module MIDI_Plug_Arc()
{
	MIDI_Plug_Arc_Base(9, false);

	difference() {
		translate([0, -eps, 0])
		intersection() {
			translate([0, 4.3, 0]) MIDI_Plug_Arc_Base(4.3, true);
			rotate([0, 90, 0]) oval_prism(20.5, 4, 8, center=true, $fn = $preview ? 32 : 100);
		
		// hull() {
		// 	rotate([  0, 0, 0]) MIDI_Plug_Arc_Base(0.1);
		// 	rotate([-90, 0, 0]) scale([1, 1, 0.6]) MIDI_Plug_Arc_Base(0.1);
		// }

		// 
		}
		MIDI_Plug_Cutout();
	}
}

module MIDI_Plug_Arc_Base(height, filled)
{
	difference()
	{
		rotate([90, 0, 0]) {
			if(filled==true) {
				oval_prism(height, 18.5/2-1.25, 10.2, $fn = $preview ? 32 : 100);
			}
			if(filled==false) {
				oval_tube(height, 18.5/2 + 1, 8, 1, $fn = $preview ? 32 : 100);
			}
		}
		translate([0, -height/2+0.01, -5]) cube([40, 2*height, 10], center=true);
	}
}
