// Intents: every pointer and key here becomes a message for the server, and the
// server decides what happened. The only local decisions are the camera's.
import { aim } from './dev';
import { pull } from './pull';
import { at, cam, pan, switchScene, wpc, wpp, zoomAt } from './stage';
import { send } from './world';

const keys = new Set<string>();
let pan0: { x: number; y: number } | null = null; // right/middle drag moves the camera
let pulling = false; // left button down: keep reporting where the piece is
let pullTo: { x: number; y: number } | null = null;
let pullIn = 0;
let hover: { clientX: number; clientY: number } | null = null; // the pointer on screen

// The browser's own pointer is hidden; the game draws this one, and it has to be
// re-read every frame because panning moves the cell under a still pointer.
export const pointer = () => hover;

// Whatever is in hand goes back to the server where it was let go, so a piece can
// never ride a scene change out to a galaxy-scale coordinate.
const release = () => {
  if (!pulling) return;
  pulling = false;
  const p = pull.to;
  pullTo = null;
  pull.to = null;
  if (p) send({ t: 'drop', x: p.x, y: p.y });
};

export function bind(): void {
  addEventListener('pointerdown', (e) => {
    hover = e;
    aim(e);
    if (e.button !== 0) {
      pan0 = { x: e.clientX, y: e.clientY };
      return;
    }
    if (cam.scene === 'galaxy') return; // the map has nothing to pick up yet
    const p = at(e);
    pulling = true;
    pull.to = p; // the piece in hand sits under the pointer, not a tick behind it
    send({ t: 'pick', x: p.x, y: p.y });
  });

  addEventListener('pointermove', (e) => {
    hover = e;
    aim(e);
    if (pan0) {
      pan(-(e.clientX - pan0.x) * wpp(), (e.clientY - pan0.y) * wpp());
      pan0 = { x: e.clientX, y: e.clientY };
      return;
    }
    if (!pulling) return;
    pull.to = pullTo = at(e);
  });

  addEventListener('pointerup', (e) => {
    if (e.button !== 0) {
      pan0 = null;
      return;
    }
    if (!pulling) return;
    pulling = false;
    pullTo = null;
    pull.to = null;
    const p = at(e);
    send({ t: 'drop', x: p.x, y: p.y });
  });
  addEventListener('pointerleave', () => (hover = null));
  addEventListener('contextmenu', (e) => e.preventDefault());

  addEventListener('wheel', (e) => zoomAt(e));

  addEventListener('keydown', (e) => {
    keys.add(e.code);
    if (e.code === 'Space') cam.follow = true;
    // The map is a map: it opens on the galaxy as a whole, and whatever you panned
    // and zoomed it to the last time is exactly where it still is.
    if (e.code === 'KeyM') {
      release();
      switchScene(cam.scene === 'local' ? 'galaxy' : 'local');
    }
  });
  addEventListener('keyup', (e) => keys.delete(e.code));
}

// Per frame: pan on screen-space speed so it feels the same at every zoom, and
// report a drag no faster than the server needs it.
export function step(dt: number): void {
  const px = (keys.has('KeyD') || keys.has('ArrowRight') ? 1 : 0) - (keys.has('KeyA') || keys.has('ArrowLeft') ? 1 : 0);
  const py = (keys.has('KeyW') || keys.has('ArrowUp') ? 1 : 0) - (keys.has('KeyS') || keys.has('ArrowDown') ? 1 : 0);
  if (px || py) {
    const s = (30 * wpc() * dt) / Math.hypot(px, py); // 30 cells / second
    pan(px * s, py * s);
  }
  if (pullTo && (pullIn += dt) > 1 / 30) {
    pullIn = 0;
    send({ t: 'drag', x: pullTo.x, y: pullTo.y });
    pullTo = null;
  }
}
