// Kitt
// The beloved KittyCAD mascot in a voxelized style.

// Pixel box function
fn pixelBox(kitExtrude, extrudeTag, positionY, positionZ, width, height, depth) {
  pixelBoxBody = startSketchOn(kitExtrude, face = extrudeTag)
    |> startProfile(at = [positionY, positionZ])
    |> line(end = [0, height])
    |> line(end = [width, 0])
    |> line(end = [0, -height])
    |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
    |> close()
    |> extrude(length = depth)
  return pixelBoxBody
}

// 1. Kitty Body
kitBodyElevation = 6
kitBodyWidth = 26
kitBodyHeight = 25
kitBodyDepth = 18

kitBody = startSketchOn(XZ)
  |> startProfile(at = [-kitBodyWidth / 2, kitBodyElevation])
  |> line(end = [0, kitBodyHeight])
  |> line(end = [kitBodyWidth, 0], tag = $seg01)
  |> line(end = [0, -kitBodyHeight], tag = $seg02)
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()
  |> extrude(length = kitBodyDepth)

// 2. Kitty Head (Frame of display)
kitHeadOffset = 1
kitHeadHeight = 16

kitHeadElevation = kitBodyElevation + kitBodyHeight - kitHeadOffset - kitHeadHeight

kitHeadWidth = kitBodyWidth - (kitHeadOffset * 2)
kitHeadDepth = 3
kitHead = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = END,
  positionY = -kitHeadWidth / 2,
  positionZ = kitHeadElevation,
  width = kitHeadWidth,
  height = kitHeadHeight,
  depth = kitHeadDepth,
)
kitFaceElevation = kitHeadElevation + 2

// 3. Kitty Face
kitFaceWidth = kitHeadWidth - 4
kitFaceHeight = kitHeadElevation + kitHeadHeight - kitFaceElevation - 3
kitFaceDepth = 2

kitFace = startSketchOn(kitHead, face = END)
  |> startProfile(at = [-kitFaceWidth / 2, kitFaceElevation])
  |> line(end = [0, 1]) // left lower corner up
  |> line(end = [-1, 0]) // left lower corner left
  |> line(end = [0, kitFaceHeight]) // left side up
  |> line(end = [1, 0]) // left upper corner right
  |> line(end = [0, 1]) // left upper corner up
  |> line(end = [kitFaceWidth, 0]) // upper side right
  |> line(end = [0, -1]) // right upper corner down
  |> line(end = [1, 0]) // right upper corner right
  |> line(end = [0, -kitFaceHeight]) // right side down
  |> line(end = [-1, 0]) // right lower corner left
  |> line(end = [0, -1]) // right lower corner down
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()
  |> extrude(length = -kitFaceDepth)

  // Kitty Face Features:
  // 3.1 Kitty Eyes

// 3.1.1 Kitty Left Eye
kitEyeDepth = 0.5
kitEyeHeight = kitFaceElevation + 7
kitEyeOffset = 7

// 3.1.2 Kitty Right Eye
kitLeftEye1 = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = -kitEyeOffset,
  positionZ = kitEyeHeight,
  width = 1,
  height = 1,
  depth = kitEyeDepth,
)

// 3.2 Kitty Nose
kitLeftEye2 = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = -kitEyeOffset + 1,
  positionZ = kitEyeHeight + 1,
  width = 3,
  height = 1,
  depth = kitEyeDepth,
)
kitLeftEye3 = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = -kitEyeOffset + 4,
  positionZ = kitEyeHeight,
  width = 1,
  height = 1,
  depth = kitEyeDepth,
)
kitRightEye = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = kitEyeOffset - 3,
  positionZ = kitEyeHeight - 1,
  width = 2,
  height = 4,
  depth = kitEyeDepth,
)
kitNoseElevation = kitEyeHeight - 5
kitNose = startSketchOn(kitFace, face = START)
  |> startProfile(at = [-2, kitNoseElevation]) // H V
  |> line(end = [0, 1]) // lower-left up
  |> line(end = [2, 0]) // lower-left right
  |> line(end = [0, 2]) // mid-left up
  |> line(end = [-1, 0]) // upper-left left
  |> line(end = [0, 1]) // upper-left up
  |> line(end = [3, 0]) // upper-mid right
  |> line(end = [0, -1]) // upper-right down
  |> line(end = [-1, 0]) // upper-right left
  |> line(end = [0, -2]) // mid-left down
  |> line(end = [2, 0]) // lower-right right
  |> line(end = [0, -1]) // lower-right down
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
  |> close()
  |> extrude(length = kitEyeDepth)

// 3.3 Kitty Mouth
kitMouthOffset = 4
kitMouthHeight = kitEyeHeight - 3
kitMouthUpLeft = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = -kitMouthOffset,
  positionZ = kitMouthHeight,
  width = 1,
  height = 1,
  depth = kitEyeDepth,
)

// 4. Kitty Belly
kitMouthDownLeft = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = -kitMouthOffset + 1,
  positionZ = kitMouthHeight - 1,
  width = 1,
  height = 1,
  depth = kitEyeDepth,
)
kitMouthUpRight = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = kitMouthOffset,
  positionZ = kitMouthHeight,
  width = 1,
  height = 1,
  depth = kitEyeDepth,
)
kitMouthDownRight = pixelBox(
  kitExtrude = kitFace,
  extrudeTag = START,
  positionY = kitMouthOffset - 1,
  positionZ = kitMouthHeight - 1,
  width = 1,
  height = 1,
  depth = kitEyeDepth,
)
kitBellyElevation = kitBodyElevation + 1

kitBellyHeight = kitHeadElevation - kitBellyElevation - 1

// 4.1 Kitty VHS
kitBellyWidth = kitHeadWidth
kitBellyDepth = kitHeadDepth
kitBelly = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = END,
  positionY = -kitBellyWidth / 2,
  positionZ = kitBellyElevation,
  width = kitBellyWidth,
  height = kitBellyHeight,
  depth = kitBellyDepth,
)
kitVHSelevation = kitBellyElevation + 1

kitVHSheight = 2

// 4.2 Kitty Floppy
kitVHSwidth = 8
kitVHSdepth = 1
kitVHS = pixelBox(
  kitExtrude = kitBelly,
  extrudeTag = END,
  positionY = -kitVHSwidth / 2,
  positionZ = kitVHSelevation,
  width = kitVHSwidth,
  height = kitVHSheight,
  depth = kitVHSdepth,
)
kitFloppyElevation = kitBellyElevation + 1
kitFloppyHeight = 1

kitFloppyWidth = 5
kitFloppyOffset = kitBellyWidth / 2 - 1
kitFloppyDepth = 2

// 4.3 Kitty Belly Button
kitFloppy1 = pixelBox(
  kitExtrude = kitBelly,
  extrudeTag = END,
  positionY = -kitFloppyOffset,
  positionZ = kitFloppyElevation,
  width = kitFloppyWidth,
  height = kitFloppyHeight,
  depth = -kitFloppyDepth,
)
kitFloppy2 = pixelBox(
  kitExtrude = kitBelly,
  extrudeTag = END,
  positionY = -kitFloppyOffset,
  positionZ = kitFloppyElevation + 2,
  width = kitFloppyWidth,
  height = kitFloppyHeight,
  depth = -kitFloppyDepth,
)
kitFloppy3 = pixelBox(
  kitExtrude = kitBelly,
  extrudeTag = END,
  positionY = kitFloppyOffset,
  positionZ = kitFloppyElevation,
  width = -kitFloppyWidth,
  height = kitFloppyHeight,
  depth = -kitFloppyDepth,
)
kitBellyButtonOffset = kitHeadWidth / 2 - 3
kitBellyButtonElevation = kitHeadElevation - 1

kitBellyButtonWidth = 2

// 4.4 Kitty Buttons
kitBellyButtonHeight = 1
kitBellyButtonDepth = kitHeadDepth + 1
kitBellyButton = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = END,
  positionY = -kitBellyButtonOffset,
  positionZ = kitBellyButtonElevation,
  width = kitBellyButtonWidth,
  height = kitBellyButtonHeight,
  depth = kitBellyButtonDepth,
)

kitButtonWidth = 1
kitButtonHeight = 2
kitButtonDepth = kitFloppyDepth
kitButtonElevation = kitFloppyElevation + 2

kitButton1 = pixelBox(
  kitExtrude = kitBelly,
  extrudeTag = END,
  positionY = kitFloppyOffset,
  positionZ = kitFloppyElevation + 2,
  width = -kitButtonWidth,
  height = kitButtonHeight,
  depth = -kitButtonDepth,
)

// 5. Kitty Legs
kitButton2 = pixelBox(
  kitExtrude = kitBelly,
  extrudeTag = END,
  positionY = kitFloppyOffset - kitButtonWidth - 1,
  positionZ = kitFloppyElevation + 2,
  width = -kitButtonWidth,
  height = kitButtonHeight,
  depth = -kitButtonDepth,
)
kitButton3 = pixelBox(
  kitExtrude = kitBelly,
  extrudeTag = END,
  positionY = kitFloppyOffset - (2 * (kitButtonWidth + 1)),
  positionZ = kitFloppyElevation + 2,
  width = -kitButtonWidth,
  height = kitButtonHeight,
  depth = -kitButtonDepth,
)

kitShoeWidth = 7
kitShoeLength = 10
kitShoeHeight = 3

fn kitLeg(offsetFront, offsetSide) {
  kitShoeOffsetFront = kitShoeLength / 2 - (kitBodyDepth / 2) - offsetFront

  kitFootPrint = startSketchOn(XY)
    |> startProfile(at = [offsetSide, kitShoeOffsetFront])
    |> line(end = [kitShoeWidth, 0])
    |> line(end = [0, -kitShoeLength])
    |> line(end = [-kitShoeWidth, 0])
    |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
    |> close()

  kitShoe = extrude(kitFootPrint, length = kitShoeHeight)

  kitPantsOffsetSide = offsetSide + 1
  kitPantsOffsetFront = 2 * kitShoeOffsetFront - 2
  kitPantsWidth = kitShoeWidth - 2
  kitPantsFrontWidth = kitPantsWidth
  kitPantsHeight = kitBodyElevation - kitShoeHeight

  kitPants = pixelBox(
    kitExtrude = kitShoe,
    extrudeTag = END,
    positionY = kitPantsOffsetSide,
    positionZ = kitPantsOffsetFront,
    width = kitPantsFrontWidth,
    height = kitPantsWidth,
    depth = kitPantsHeight,
  )

  return kitShoe
}
kitLegOffset = 3

kitRightLeg = kitLeg(offsetFront = 0, offsetSide = kitLegOffset)
kitLeftLeg = kitLeg(offsetFront = 0, offsetSide = -kitLegOffset - kitShoeWidth)

// 6. Kitty Ears
kitEarWidth = 8
kitEarDepth = 8
kitEarHeight = 2

fn kitEar(earOffsetFront, earOffsetSide) {
  kitNewEarOffsetFront = kitBodyDepth - earOffsetFront
  kitNewEarOffsetSide = -(kitBodyWidth / 2 - earOffsetSide)
  baseVolume = pixelBox(
    kitExtrude = kitBody,
    extrudeTag = seg01,
    positionY = kitNewEarOffsetSide,
    positionZ = kitNewEarOffsetFront,
    width = kitEarWidth,
    height = -kitEarDepth,
    depth = kitEarHeight,
  )

  secondOffset = 1
  secondLevel = pixelBox(
    kitExtrude = baseVolume,
    extrudeTag = END,
    positionY = kitNewEarOffsetSide + secondOffset,
    positionZ = kitNewEarOffsetFront - 0.01,
    width = kitEarWidth - (secondOffset * 2),
    height = -kitEarDepth + secondOffset * 2,
    depth = kitEarHeight,
  )

  thirdOffset = 2
  thirdLevel = pixelBox(
    kitExtrude = secondLevel,
    extrudeTag = END,
    positionY = kitNewEarOffsetSide + thirdOffset,
    positionZ = kitNewEarOffsetFront - 0.02,
    width = kitEarWidth - (thirdOffset * 2),
    height = -kitEarDepth + thirdOffset * 2,
    depth = kitEarHeight,
  )

  fourthOffset = 3
  fourthLevel = pixelBox(
    kitExtrude = thirdLevel,
    extrudeTag = END,
    positionY = kitNewEarOffsetSide + fourthOffset,
    positionZ = kitNewEarOffsetFront - 0.03,
    width = kitEarWidth - (fourthOffset * 2),
    height = -kitEarDepth + fourthOffset * 2,
    depth = kitEarHeight,
  )

  return baseVolume
}
kitEarOffsetFront = 4
kitEarOffsetSide = 1

kitRightEar = kitEar(earOffsetFront = kitEarOffsetFront, earOffsetSide = kitEarOffsetSide)
kitLeftEar = kitEar(earOffsetFront = kitEarOffsetFront, earOffsetSide = kitBodyWidth - kitEarWidth - kitEarOffsetSide)

// 7. Kitty Side
// 7.1 Grill
grillOffset = 4

grillRowA = kitBodyElevation + kitBodyHeight - grillOffset
grillRowB = grillRowA - 2
grillRowC = grillRowA - 4

grillColumnA = kitBodyDepth - grillOffset
grillColumnB = grillColumnA - 1
grillColumnC = grillColumnA - 2
grillColumnD = grillColumnA - 3
grillColumnE = grillColumnA - 4

grillHoleSize = 1
grillHoleDepth = -2

grillHoleAB = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = grillRowA,
  positionZ = grillColumnB,
  width = grillHoleSize,
  height = grillHoleSize,
  depth = grillHoleDepth,
)

grillHoleAD = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = grillRowA,
  positionZ = grillColumnD,
  width = grillHoleSize,
  height = grillHoleSize,
  depth = grillHoleDepth,
)

grillHoleBA = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = grillRowB,
  positionZ = grillColumnA,
  width = grillHoleSize,
  height = grillHoleSize,
  depth = grillHoleDepth,
)

grillHoleBC = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = grillRowB,
  positionZ = grillColumnC,
  width = grillHoleSize,
  height = grillHoleSize,
  depth = grillHoleDepth,
)

grillHoleBE = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = grillRowB,
  positionZ = grillColumnE,
  width = grillHoleSize,
  height = grillHoleSize,
  depth = grillHoleDepth,
)

grillHoleCB = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = grillRowC,
  positionZ = grillColumnB,
  width = grillHoleSize,
  height = grillHoleSize,
  depth = grillHoleDepth,
)

grillHoleCD = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = grillRowC,
  positionZ = grillColumnD,
  width = grillHoleSize,
  height = grillHoleSize,
  depth = grillHoleDepth,
)

// 7.2 Kitty Vent
kitVentElevation = kitBodyElevation + 1
kitVentOffset = 1
kitVentHoleWidth = 1
kitVentHoleHeight = 4
kitVentHoleDepth = grillHoleDepth

kitVentA = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = kitVentElevation,
  positionZ = kitVentOffset,
  width = kitVentHoleHeight,
  height = kitVentHoleWidth,
  depth = kitVentHoleDepth,
)

kitVentB = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = kitVentElevation,
  positionZ = kitVentOffset + 2,
  width = kitVentHoleHeight,
  height = kitVentHoleWidth,
  depth = kitVentHoleDepth,
)

kitVentC = pixelBox(
  kitExtrude = kitBody,
  extrudeTag = seg02,
  positionY = kitVentElevation,
  positionZ = kitVentOffset + 4,
  width = kitVentHoleHeight,
  height = kitVentHoleWidth,
  depth = kitVentHoleDepth,
)
