// The world is a three.js scene drawn at exactly one texel per character cell,
// then a single fullscreen pass quantizes it into glyphs of the amber ramp.
// Two channels carry the shading: R = luminance (which glyph), G = palette index.
import * as THREE from 'three';

export const GLYPHS = ' .:-=+*#%@';
// 0-7 is the amber ramp used for everything that flies. 8-12 are reserved for
// celestial bodies, so a rust planet and a steel planet never read as each other.
// 13-14 are damage: a hurt piece can never be mistaken for a healthy one. 15 is
// the AI hull: neither the gold of another player nor the red of an enemy.
export const PALETTE = [
  0x0c0803, 0x241806, 0x3d2709, 0x57370c, 0x754c11, 0x98631a, 0xc78a20, 0xffe9a8,
  0x4a1d0f, 0x9c4f28, 0x1f4453, 0x7fc0d4, 0xfff6dd, 0x7a1512, 0xff4a3d, 0x5fbf8a,
];
const ATLAS_CELL = 32;

// R = luminance, G = palette index.
export const lum = (v: number, idx = 7) => new THREE.Color(v, idx / (PALETTE.length - 1), 0);

// One cell of white ink per character, in the same monospace face as the ramp: the
// glyph atlas and any hud word are the same kind of texture.
export function textAtlas(text: string): THREE.Texture {
  const c = document.createElement('canvas');
  c.width = text.length * ATLAS_CELL;
  c.height = ATLAS_CELL;
  const g = c.getContext('2d')!;
  g.font = `700 ${ATLAS_CELL - 1}px ui-monospace, SFMono-Regular, Menlo, monospace`;
  g.textAlign = 'center';
  g.textBaseline = 'middle';
  g.fillStyle = '#fff';
  for (let i = 0; i < text.length; i++) {
    g.fillText(text[i], i * ATLAS_CELL + ATLAS_CELL / 2, ATLAS_CELL / 2);
  }
  const t = new THREE.CanvasTexture(c);
  t.minFilter = t.magFilter = THREE.LinearFilter;
  t.generateMipmaps = false;
  return t;
}

export function paletteTexture(): THREE.DataTexture {
  const d = new Uint8Array(PALETTE.length * 4);
  PALETTE.forEach((hex, i) => d.set([(hex >> 16) & 255, (hex >> 8) & 255, hex & 255, 255], i * 4));
  const t = new THREE.DataTexture(d, PALETTE.length, 1, THREE.RGBAFormat);
  t.minFilter = t.magFilter = THREE.NearestFilter;
  t.needsUpdate = true;
  return t;
}

export const VERT = /* glsl */ `void main() { gl_Position = vec4(position.xy, 0.0, 1.0); }`;

export const FRAG = /* glsl */ `
uniform sampler2D uScene;
uniform sampler2D uAtlas;
uniform sampler2D uPalette;
uniform vec3 uBg;
uniform vec2 uCells;
uniform float uCellPx;
uniform sampler2D uWord; // hud ink, in cells, drawn over the glyphs
uniform vec4 uWordCell; // x, y, width, height of the word, in cells from the corner
uniform vec3 uWordCol;
uniform sampler2D uWord2; // a second word, on the same row as the first
uniform vec4 uWord2Cell;
uniform vec3 uWord2Col;

// One word, spelled from its own atlas and placed in cells. A zero-sized rect
// covers nothing, which is also how a word is hidden.
vec3 spell(vec3 col, vec2 uv, sampler2D atlas, vec4 cell, vec3 ink) {
  vec2 w = (uv - cell.xy) / cell.zw;
  return (w.x >= 0.0 && w.x < 1.0 && w.y >= 0.0 && w.y < 1.0)
    ? mix(col, ink, texture2D(atlas, w).a)
    : col;
}

void main() {
  vec2 uv = gl_FragCoord.xy / uCellPx;
  vec3 s = texture2D(uScene, (floor(uv) + 0.5) / uCells).rgb;
  float band = floor(s.r * ${GLYPHS.length - 1}.0 + 0.5);
  vec3 col = texture2D(uPalette, vec2((s.g * ${PALETTE.length - 1}.0 + 0.5) / ${PALETTE.length}.0, 0.5)).rgb;
  vec2 local = clamp(fract(uv), 0.02, 0.98);
  float ink = texture2D(uAtlas, vec2((band + local.x) / ${GLYPHS.length}.0, local.y)).a;
  vec3 outCol = mix(uBg, col, ink);
  outCol = spell(outCol, uv, uWord, uWordCell, uWordCol);
  outCol = spell(outCol, uv, uWord2, uWord2Cell, uWord2Col);
  gl_FragColor = vec4(outCol, 1.0);
}
`;
