// Scene 2: the galaxy map. One mark per star system and one per living player,
// drawn in cells rather than in world units, so a dot is a glyph at every map
// zoom instead of a sub-cell smudge the raster averages away. The systems are the
// rings the generator lays out; nothing here adds a shape the galaxy does not have.
import * as THREE from 'three';
import type { Ship, System } from '../sim';
import { snapCell } from './grid';
import type { Camera } from './index';
import { lum } from './ink';
import type { Part } from './pieces';
import { free, plate } from './shapes';

// A system is the one bright thing in its space: a gold core with four dim rays,
// which is also the only mark that is more than a single cell, so a system is
// never read as a player. Every ship is one white cell, and yours is the only one
// at full brightness: on a map of the population you have to find yourself first.
const CORE = lum(0.95, 6);
const RAY = lum(0.45, 6);
const OWN = lum(1, 12);
const OTHER = lum(0.7, 12);

const systemMark = (): THREE.Group => {
  const g = new THREE.Group();
  for (const [dx, dy] of [[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1]]) {
    const p = plate(1, 1, 1);
    p.material.color.copy(dx || dy ? RAY : CORE);
    p.position.set(dx, dy, 0);
    g.add(p);
  }
  return g;
};

export class Galaxy {
  readonly scene = new THREE.Scene();
  private stars = new Map<number, THREE.Group>();
  private ships = new Map<number, Part>();

  // Marks are placed from the camera, like the order marker: a pan slides the map
  // under glyphs that never move, and a dot never straddles four of them.
  show(cam: Camera, systems: System[], ships: Ship[], ownId: number, wpc: number, cols: number, rows: number): void {
    const stars = new Set<number>();
    for (const s of systems) {
      stars.add(s.id);
      let g = this.stars.get(s.id);
      if (!g) {
        g = systemMark();
        this.stars.set(s.id, g);
        this.scene.add(g);
      }
      g.scale.setScalar(wpc); // the mark is built in cells, the map is in world units
      g.position.set(snapCell(s.x, cam.x, wpc, cols), snapCell(s.y, cam.y, wpc, rows, -1), 0);
    }
    for (const [id, g] of this.stars) {
      if (stars.has(id)) continue;
      free(g);
      this.scene.remove(g);
      this.stars.delete(id);
    }
    // Only real players: an AI hull never leaves its system, so it says nothing
    // about where the population is.
    const live = new Set<number>();
    for (const s of ships) {
      if (s.ai) continue;
      live.add(s.id);
      let m = this.ships.get(s.id);
      if (!m) {
        m = plate(1, 1, 1);
        this.ships.set(s.id, m);
        this.scene.add(m);
      }
      m.material.color.copy(s.id === ownId ? OWN : OTHER);
      m.scale.setScalar(wpc);
      m.position.set(snapCell(s.x, cam.x, wpc, cols), snapCell(s.y, cam.y, wpc, rows, -1), 0);
    }
    for (const [id, m] of this.ships) {
      if (live.has(id)) continue;
      free(m);
      this.scene.remove(m);
      this.ships.delete(id);
    }
  }
}
