// A 2D scene: a draw set + an orthographic Camera2D. Opening a scene asks the host to bring up the
// 2D engine and starts its RAF loop; only the active scene renders. Grown from the Phase 0 seed.

import { Color, type ColorInput } from "../core/color"
import { _installAspectFrames } from "../core/Aspect"
import { cx, cy, type Vec2Like } from "../math/vec"
import type { ClickEvent, TouchStartEvent } from "../runtime/touch"
import { Camera2D } from "./Camera2D"
import { node2dRegistry, type Node2D } from "./Node2D"
import { ensurePointerEvents } from "./touch"

// Route aspect update(dt) through the render-synced native phases (c2dRender early = before physics,
// late = after animations / before draw), so followers/cameras are in lockstep with the draw. Done from
// the 2D layer (not core) so a 3D-only bundle never pulls in the 2D bridge. Installed once, lazily on
// the first Scene2D. On an older wasm without the hooks the installer returns false → setLoop fallback.
let framesInstalled = false
const installRenderSyncedFrames = (): void => {
  if (framesInstalled) return
  framesInstalled = true
  _installAspectFrames((early, late) => {
    if (typeof _creator2d.setEarlyUpdate !== "function") return false
    _creator2d.setEarlyUpdate(early)
    _creator2d.setLateUpdate(late)
    return true
  })
}

export type Scene2DOptions = {
  /** Background color — '#10131a', 0x10131a, or [r,g,b]/[r,g,b,a] in 0..1. */
  background?: ColorInput
  /** Texture sampling for the 2D renderer: `'nearest'` (default — crisp pixel art) or `'linear'`
   *  (smooth — for hi-res / non-pixel-art assets). Applied when the scene is opened, so switching
   *  scenes restores the active scene's choice. NOTE: this is a GLOBAL renderer setting (one sampler
   *  for ALL textures, not per-texture), so the currently-open scene's filter wins. Omit to leave the
   *  current setting (engine default is nearest). */
  filter?: 'nearest' | 'linear'
}

/** Returned by scene2d.layer(n); chain .ySort() to enable top-down depth sorting for that layer. */
class LayerHandle {
  private readonly sceneId: number
  private readonly layer: number
  constructor(sceneId: number, layer: number) { this.sceneId = sceneId; this.layer = layer }
  /** In this layer, lower-on-screen sprites overlap higher ones (top-down feet sorting). */
  ySort(enabled = true): this {
    _creator2d.setLayerYSort(this.sceneId, this.layer, enabled)
    return this
  }
}

export class Scene2D {
  /** Native scene handle. */
  readonly id: number
  readonly camera: Camera2D

  private static _active: Scene2D | null = null
  static get active(): Scene2D | null { return Scene2D._active }

  /** @internal — scene-level pointer listeners (the catch-all; target = hit node or null). */
  readonly _clickListeners: Array<(ev: ClickEvent<Node2D | null>) => void> = []
  readonly _touchStartListeners: Array<(ev: TouchStartEvent<Node2D | null>) => void> = []

  /** Texture filter to (re)assert on open(); the sampler is global, so each scene reclaims it. */
  private readonly _filter?: 'nearest' | 'linear'

  constructor(options: Scene2DOptions = {}) {
    installRenderSyncedFrames()
    this.id = _creator2d.createScene()
    this.camera = new Camera2D(this.id)
    this._filter = options.filter
    if (options.background !== undefined) this.background = options.background
  }

  set background(color: ColorInput) {
    const [ r, g, b, a ] = Color.toRgba01(color)
    _creator2d.sceneSetClearColor(this.id, r, g, b, a)
  }

  /** Address a draw layer to configure it, e.g. scene2d.layer(1).ySort(). */
  layer(n: number): LayerHandle {
    return new LayerHandle(this.id, n)
  }

  /** Topmost visible sprite whose world bounds contain the world point, or null. Body-free
   *  hit-test for tap/click picking — pair with camera.screenToWorld(). */
  pick(worldPoint: Vec2Like): Node2D | null {
    const id = _creator2d.pickSprite(this.id, cx(worldPoint), cy(worldPoint))
    return id ? node2dRegistry.get(id) ?? null : null
  }

  /** Scene-level pointer events. The handler fires for every tap/press in the scene, with
   *  `ev.target` = the hit Physics2D node (or null). On 'touchstart' you can ev.track(...) a drag. */
  addEventListener(channel: "click", callback: (ev: ClickEvent<Node2D | null>) => void): void
  addEventListener(channel: "touchstart", callback: (ev: TouchStartEvent<Node2D | null>) => void): void
  addEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
    const list = channel === "click" ? this._clickListeners : this._touchStartListeners
    if (!list.includes(callback)) { list.push(callback); ensurePointerEvents() }
  }
  removeEventListener(channel: "click" | "touchstart", callback: (ev: any) => void): void {
    const list = channel === "click" ? this._clickListeners : this._touchStartListeners
    const i = list.indexOf(callback)
    if (i >= 0) list.splice(i, 1)
  }

  add(...nodes: Node2D[]): this {
    for (const n of nodes) _creator2d.addEntityToScene(this.id, n.id)
    return this
  }
  remove(...nodes: Node2D[]): this {
    for (const n of nodes) _creator2d.removeEntityFromScene(this.id, n.id)
    return this
  }

  /** Make this the active scene (brings up the 2D engine on the canvas; closes any other). */
  open(): this {
    // Reassert this scene's texture filter (the sampler is global — see Scene2DOptions.filter).
    if (this._filter !== undefined) _creator2d.setDefaultFilter(this._filter === 'linear')
    _creator2d.openScene(this.id)
    Scene2D._active = this
    return this
  }
  close(): this {
    _creator2d.closeScene()
    if (Scene2D._active === this) Scene2D._active = null
    return this
  }
  destroy(): void {
    this.close()
    _creator2d.destroyScene(this.id)
  }
}
