// What a piece or an ore chunk looks like: the palette roles, how wear and tier are
// folded into the material colours, and the meshes themselves.
import * as THREE from 'three';
import type { Kind } from '../sim';
import { lum } from './ink';
import { pieceGeometry, poly } from './shapes';

const ORE_R = 6.5; // world radius of an ore piece

// A piece is a bright silhouette with a dimmer inset plate, so the cel shading
// survives the downsample to cell resolution. The roles must never read as each
// other: your hull is the brightest cream, another player's hull is gold, a
// server-run AI hull is green, and loose wreckage is dim sepia. A piece flips
// shade the moment it is bolted on or torn off, and turns red for everyone once
// it starts taking hits.
const SHADE = {
  own: [lum(1, 7), lum(0.5, 7)],
  other: [lum(0.8, 6), lum(0.4, 6)],
  ai: [lum(0.75, 15), lum(0.35, 15)],
  rogue: [lum(0.7, 13), lum(0.35, 13)],
  loose: [lum(0.55, 5), lum(0.28, 5)],
  ore: [lum(0.95, 6), lum(0.5, 4)], // ore in a planet's ring
  towed: [lum(1, 7), lum(0.55, 6)], // ore on someone's line, bright as a hull
};
export type Role = keyof typeof SHADE;
const HURT = [
  [lum(0.85, 13), lum(0.4, 13)], // hit: rust red
  [lum(1, 14), lum(0.5, 14)], // nearly gone: hot red
];
// Upgrade tiers wear a border: gold at one, cream at two. Nothing at zero.
const TIER = [lum(0, 0), lum(1, 6), lum(1, 12)];

export type Part = THREE.Mesh<THREE.BufferGeometry, THREE.MeshBasicMaterial>;

export const shade = (g: THREE.Group, role: Role) => {
  const c = SHADE[role];
  g.children.forEach((m, i) => (m as Part).material.color.copy(c[i]));
};

// What a piece looks like is entirely its health, its tier and whether the ship it
// belongs to is hostile to us. A hostile hull wears a strong red rim, which is a
// statement about the ship, not damage to it.
export type Wear = { hp: number; maxhp: number; tier: number };
export function paint(g: THREE.Group, role: Role, w: Wear, hostile: boolean): void {
  const ratio = w.maxhp > 0 ? w.hp / w.maxhp : 1;
  const [hi, lo] = ratio > 0.66 ? SHADE[role] : ratio > 0.33 ? HURT[0] : HURT[1];
  const [rim, edge, a, b] = g.children as Part[]; // built in this order
  rim.visible = hostile;
  if (hostile) rim.material.color.copy(HURT[1][0]); // the rim of an enemy
  a.material.color.copy(hi);
  b.material.color.copy(lo);
  edge.visible = w.tier > 0 && !hostile; // the rim already says more than a tier
  if (edge.visible) edge.material.color.copy(TIER[w.tier]);
}

// The tier border is a larger copy of the same shape drawn first, so the
// silhouette covers its middle and only a rim of it shows. A hostile hull gets a
// second, fatter copy outside that, so an enemy reads as a strong red border even
// when a cell is several world units wide.
export function piece(kind: Kind): THREE.Group {
  const g = new THREE.Group();
  g.renderOrder = 1; // over the bodies, which are the only thing behind a ship
  for (const [scale, v] of [[1.9, 0], [1.3, 0], [1, 1], [0.7, 0.5]]) {
    g.add(new THREE.Mesh(pieceGeometry(kind, scale), new THREE.MeshBasicMaterial({ color: lum(v) })));
  }
  return g;
}

// Ore is a small faceted chunk: a hexagon with a darker core, the one thing in
// space that is neither a ship piece nor a body.
const ORE_SHELL = new THREE.ShapeGeometry(poly(6, ORE_R));
const ORE_CORE = new THREE.ShapeGeometry(poly(6, ORE_R * 0.55, 0, 0.4));
export function oreChunk(): THREE.Group {
  const g = new THREE.Group();
  g.renderOrder = 1; // ore never hides behind a planet it is orbiting
  g.add(new THREE.Mesh(ORE_SHELL, new THREE.MeshBasicMaterial({ color: SHADE.ore[0] })));
  g.add(new THREE.Mesh(ORE_CORE, new THREE.MeshBasicMaterial({ color: SHADE.ore[1] })));
  shade(g, 'ore');
  return g;
}
