import Phaser from 'phaser';
import { lookupName, type GuestList, type NameLookup } from '../../rules/guestList';
import { DOOR_PALETTE, MONO, panel, text } from './ui';

// The guest-list clipboard, sharing the desk's left panel with Dazza's rules via
// a two-tab header. There is no spare desk real estate at 640x360, and a tab is
// honest about the tradeoff: you cannot read the dress code and the list at the
// same time, so checking a name costs you a beat of attention. That is the point.

export type ClipboardTab = 'rules' | 'list';

/** Two columns of names; 14 entries is the generator's ceiling. */
const ROWS_PER_COL = 7;
const ROW_H = 10;

export class ClipboardPanel {
  private readonly root: Phaser.GameObjects.Container;
  private readonly rows: Phaser.GameObjects.Text[] = [];
  private readonly hint: Phaser.GameObjects.Text;
  private highlighted = -1;

  constructor(
    scene: Phaser.Scene,
    private readonly list: GuestList,
    x: number,
    y: number,
    w: number,
    h: number,
  ) {
    this.root = scene.add.container(0, 0).setDepth(50);
    this.root.add(panel(scene, x, y, w, h, 0x1d1a14, 0x4a4436));
    this.list.entries.forEach((entry, i) => {
      const col = Math.floor(i / ROWS_PER_COL);
      const row = i % ROWS_PER_COL;
      const label = entry.plusOnes > 0 ? `${entry.name} +${entry.plusOnes}` : entry.name;
      const t = scene.add.text(x + 5 + col * (w / 2 - 4), y + 5 + row * ROW_H, label, {
        fontFamily: MONO,
        fontSize: '6px',
        color: '#c8bda0',
      });
      this.rows.push(t);
      this.root.add(t);
    });

    this.hint = text(scene, x + 5, y + h - 11, '', 6, DOOR_PALETTE.inkDim);
    this.root.add(this.hint);
    this.root.setVisible(false);
  }

  setVisible(on: boolean): void {
    this.root.setVisible(on);
  }

  /**
   * Show what the clipboard says about the name in front of you. It reports the
   * MATCH, never the truth — an exact hit is the suspicious one (rules/guestList
   * has the arithmetic), and nothing here tells the player which way to call it.
   */
  check(claimed: string | null): NameLookup | null {
    this.clearHighlight();
    if (!claimed) {
      this.hint.setText('');
      return null;
    }
    const found = lookupName(claimed, this.list);
    const idx = found.entry ? this.list.entries.indexOf(found.entry) : -1;
    if (idx >= 0 && found.match !== 'none') {
      this.rows[idx]?.setColor('#ffe08a');
      this.highlighted = idx;
    }
    this.hint.setText(
      found.match === 'exact'
        ? `"${claimed}" — written exactly like that`
        : found.match === 'near'
          ? `"${claimed}" — close to one on the list`
          : `"${claimed}" — nothing like it here`,
    );
    this.hint.setColor(found.match === 'none' ? '#c08080' : '#c8bda0');
    return found;
  }

  private clearHighlight(): void {
    if (this.highlighted >= 0) this.rows[this.highlighted]?.setColor('#c8bda0');
    this.highlighted = -1;
  }

  destroy(): void {
    this.root.destroy();
  }
}
