// Walkie Talkie Case
// The plastic case for the front of the walkie talkie

// Set units
@settings(defaultLengthUnit = in, kclVersion = 1.0)

// Import parameters and Zoo logo
import width, height, chamferLength, offset, screenWidth, screenHeight, screenYPosition, screenDepth, speakerBoxWidth, speakerBoxHeight, squareHoleSideLength, caseTolerance from "parameters.kcl"
import zLogo, oLogo, oLogo2 from "zoo-logo.kcl"

// Sketch the profile of the screen
screenSketch = startSketchOn(XZ)
  |> startProfile(at = [-screenWidth / 2, screenYPosition])
  |> xLine(length = screenWidth)
  |> yLine(length = -screenHeight)
  |> xLine(length = -screenWidth)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()

// Create transform functions for the speaker grid pattern
fn transformX(@i) {
  return { translate = [.125 * i, 0] }
}
fn transformY(@i) {
  return { translate = [0, -.125 * i] }
}

// Sketch the square hole grid pattern
squareHolePatternSketch = startSketchOn(XZ)
  |> startProfile(at = [-screenWidth / 2 + .100, 0])
  |> line(end = [squareHoleSideLength / 2, 0])
  |> line(end = [0, -squareHoleSideLength / 2])
  |> line(end = [-squareHoleSideLength / 2, 0])
  |> close()
  |> patternTransform2d(instances = 13, transform = transformX)
  |> patternTransform2d(instances = 11, transform = transformY)

// Sketch the outer profile of the case and extrude with holes using the previously made profiles
case = startSketchOn(XZ)
  |> startProfile(at = [
       -width / 2 + offset + caseTolerance,
       height / 2 - (chamferLength + (offset + caseTolerance) / 2 * cos(45deg))
     ])
  |> angledLine(angle = 45, endAbsoluteY = height / 2 - (offset + caseTolerance))
  |> line(endAbsolute = [
       width / 2 - (chamferLength + (offset + caseTolerance) / 2 * cos(45deg)),
       height / 2 - (offset + caseTolerance)
     ])
  |> angledLine(angle = -45, endAbsoluteX = width / 2 - (offset + caseTolerance))
  |> line(endAbsolute = [
       width / 2 - (offset + caseTolerance),
       -(height / 2 - (chamferLength + (offset + caseTolerance) / 2 * cos(45deg)))
     ])
  |> angledLine(angle = -135, endAbsoluteY = -height / 2 + offset + caseTolerance)
  |> line(endAbsolute = [
       -(width / 2 - (chamferLength + (offset + caseTolerance) / 2 * cos(45deg))),
       -height / 2 + offset + caseTolerance
     ])
  |> angledLine(angle = -225, endAbsoluteX = -width / 2 + offset + caseTolerance)
  |> close()
  |> subtract2d(tool = screenSketch)
  |> subtract2d(tool = squareHolePatternSketch)

  // Create the Zoo logo
  |> subtract2d(tool = zLogo(surface = startSketchOn(XZ), origin = [-.30, -1.825], scale = .20))
  |> subtract2d(tool = oLogo(surface = startSketchOn(XZ), origin = [-.075, -1.825], scale = .20))
  |> subtract2d(tool = oLogo2(surface = startSketchOn(XZ), origin = [-.075, -1.825], scale = .20))
  |> subtract2d(tool = oLogo(surface = startSketchOn(XZ), origin = [.175, -1.825], scale = .20))
  |> subtract2d(tool = oLogo2(surface = startSketchOn(XZ), origin = [.175, -1.825], scale = .20))
extrude(case, length = -0.0625)
  |> appearance(color = '#D0FF01', metalness = 0, roughness = 50)
