import type { CompanionBones, Eye, Species } from './types.js'
import {
  golem,
  elemental,
  anvilbot,
  furnace,
  molten,
  spark,
  crucible,
  hammerkin,
} from './types.js'

// Forge Golem sprites — 3 lines tall
// Frame 0: rest, Frame 1: fidget, Frame 2: action
const BODIES: Record<Species, string[][]> = {
  // Golem — chunky iron construct, heavy & square
  [golem]: [
    [
      ' ╔══════╗ ',
      ' ║{E} ▓▓ {E}║ ',
      ' ╚╡/--\\╞╝ ',
    ],
    [
      ' ╔══════╗ ',
      ' ║{E} ▓▓ {E}║ ',
      ' ╚╡/--\\╞╝~',
    ],
    [
      ' ╔══════╗ ',
      ' ║{E} ▓▓ {E}║✦',
      ' ╚╡/--\\╞╝ ',
    ],
  ],
  // Elemental — wispy flame spirit, flowing form
  [elemental]: [
    [
      '  )\\~~/( ',
      '  ({E}  {E}) ',
      '  ~\\/\\/~  ',
    ],
    [
      '  (/~~\\) ',
      '  ({E}  {E}) ',
      '  ~\\/\\/~  ',
    ],
    [
      ' *)\\ ~/(* ',
      '  ({E}  {E}) ',
      '  ~*\\/*~  ',
    ],
  ],
  // Anvilbot — anvil-shaped mech, industrial & flat-topped
  [anvilbot]: [
    [
      ' _|‾‾‾‾|_ ',
      ' [{E} ── {E}] ',
      ' /|_/\\_|\\  ',
    ],
    [
      ' _|‾‾‾‾|_ ',
      ' [{E} ── {E}]~',
      ' /|_/\\_|\\  ',
    ],
    [
      ' _|‾‾‾‾|_!',
      ' [{E} ── {E}] ',
      ' /|_/\\_|\\  ',
    ],
  ],
  // Furnace — pot-bellied stove, warm & round with grate mouth
  [furnace]: [
    [
      '  .‾‾‾‾‾‾. ',
      '  |{E}    {E}| ',
      '  |_\\▒▒/_ | ',
    ],
    [
      '  .‾‾~‾‾. ',
      '  |{E}    {E}| ',
      '  |_\\▒▒/_| ',
    ],
    [
      '  ~‾‾‾‾‾~  ',
      '  |{E}    {E}| ',
      '  |_\\▓▓/_| ',
    ],
  ],
  // Molten — lava blob, drippy & organic
  [molten]: [
    [
      '   .~~.   ',
      '  ({E}  {E})  ',
      '  `~,,~`  ',
    ],
    [
      '   .~~.   ',
      '  ({E}  {E})  ',
      '  `~,,~`, ',
    ],
    [
      '   ,~~,   ',
      '  ({E}  {E})  ',
      '  `*,,*`  ',
    ],
  ],
  // Spark — tiny electric critter, smallest species
  [spark]: [
    [
      '   \\*/    ',
      '   ({E}{E})  ',
      '    \\/    ',
    ],
    [
      '   /*/    ',
      '   ({E}{E})  ',
      '    /\\    ',
    ],
    [
      '  *\\*/    ',
      '   ({E}{E})* ',
      '    \\/    ',
    ],
  ],
  // Crucible — wide cauldron, bubbling & squat
  [crucible]: [
    [
      '  |`    `| ',
      '  ( {E}  {E} ) ',
      '   \\____/  ',
    ],
    [
      '  |` ~  `| ',
      '  ( {E}  {E} ) ',
      '   \\____/  ',
    ],
    [
      '  |`*  *`| ',
      '  ( {E}  {E} ) ',
      '   \\*__*/  ',
    ],
  ],
  // Hammerkin — hammer-headed warrior, T-shaped silhouette
  [hammerkin]: [
    [
      ' T======T  ',
      '  ({E}  {E})  ',
      '  /|  |\\  ',
    ],
    [
      ' T======T  ',
      '  ({E}  {E})  ',
      '  /| ~|\\  ',
    ],
    [
      ' T======T! ',
      '  ({E}  {E})  ',
      '  /|  |\\  ',
    ],
  ],
}


export function renderSprite(bones: CompanionBones, frame = 0): string[] {
  const frames = BODIES[bones.species]
  return frames[frame % frames.length]!.map(line =>
    line.replaceAll('{E}', bones.eye),
  )
}

export function spriteFrameCount(species: Species): number {
  return BODIES[species].length
}

export function renderFace(bones: CompanionBones): string {
  const eye: Eye = bones.eye
  switch (bones.species) {
    case golem:
      return `║${eye}▓▓${eye}║`
    case elemental:
      return `(${eye}  ${eye})`
    case anvilbot:
      return `[${eye}──${eye}]`
    case furnace:
      return `|${eye}  ${eye}|`
    case molten:
      return `(${eye}  ${eye})`
    case spark:
      return `(${eye}${eye})`
    case crucible:
      return `(${eye}  ${eye})`
    case hammerkin:
      return `(${eye}  ${eye})`
  }
}
