// sunrise-native.mjs: the sunrise drawn with Canvas 2D for Helios's browser-free portable renderer.
// No DOM, no CSS: draw(ctx, frame) paints frame.index directly; fonts are explicit bytes.
import { readFileSync } from 'node:fs'

const W = 1920, H = 1080, FRAMES = 240
const HORIZON = 780
const ease = (t) => t * t * (3 - 2 * t)
const mix = (a, b, k) => a.map((v, i) => Math.round(v + (b[i] - v) * k))
const rgb = (c) => `rgb(${c[0]},${c[1]},${c[2]})`
// The same cubic Bézier arc as sunrise.html's offset-path.
const arc = (u) => {
  const p0 = [160, 860], p1 = [520, 100], p2 = [1400, 100], p3 = [1760, 860]
  const v = 1 - u
  return [0, 1].map((i) => v * v * v * p0[i] + 3 * v * v * u * p1[i] + 3 * v * u * u * p2[i] + u * u * u * p3[i])
}

export default {
  width: W, height: H, fps: { num: 30, den: 1 }, frameCount: FRAMES,
  background: '#0e0d0b',
  fonts: {
    display: new Uint8Array(readFileSync(new URL('./jost-600.ttf', import.meta.url))),
    mono: new Uint8Array(readFileSync(new URL('./ibm-plex-mono-500.ttf', import.meta.url))),
  },
  draw(ctx, frame) {
    const t = frame.index / (FRAMES - 1)
    const day = Math.sin(Math.PI * t)                       // 0 at dawn/dusk, 1 at noon
    const top = mix([20, 18, 16], [47, 127, 224], ease(day))
    const low = mix([46, 36, 22], [246, 213, 138], ease(day))

    const sky = ctx.createLinearGradient(0, 0, 0, HORIZON)
    sky.addColorStop(0, rgb(top)); sky.addColorStop(1, rgb(low))
    ctx.fillStyle = sky; ctx.fillRect(0, 0, W, HORIZON)
    const ground = ctx.createLinearGradient(0, HORIZON, 0, H)
    ground.addColorStop(0, '#1c1a17'); ground.addColorStop(1, '#0e0d0b')
    ctx.fillStyle = ground; ctx.fillRect(0, HORIZON, W, H - HORIZON)

    // Exposures: one every 24 frames, left where the sun was. The ground hides anything below the horizon.
    ctx.save()
    ctx.beginPath(); ctx.rect(0, 0, W, HORIZON); ctx.clip()
    ctx.font = `22px ${frame.fonts.mono}`
    ctx.textAlign = 'center'
    for (let f = 0; f <= frame.index; f += 24) {
      const [x, y] = arc(f / (FRAMES - 1))
      ctx.beginPath(); ctx.arc(x, y, 70, 0, Math.PI * 2)
      ctx.fillStyle = 'rgba(248,203,77,0.12)'; ctx.fill()
      ctx.lineWidth = 2; ctx.strokeStyle = 'rgba(248,203,77,0.75)'; ctx.stroke()
      ctx.fillStyle = 'rgba(243,238,227,0.85)'; ctx.fillText(`f ${f}`, x, y - 86)
    }

    // The sun: glow, then disc.
    const [sx, sy] = arc(t)
    const glow = ctx.createRadialGradient(sx, sy, 40, sx, sy, 320)
    glow.addColorStop(0, 'rgba(245,180,0,0.5)'); glow.addColorStop(1, 'rgba(245,180,0,0)')
    ctx.fillStyle = glow; ctx.fillRect(sx - 320, sy - 320, 640, 640)
    const disc = ctx.createRadialGradient(sx, sy, 0, sx, sy, 70)
    disc.addColorStop(0, '#fff3c9'); disc.addColorStop(0.36, '#fff3c9'); disc.addColorStop(0.6, '#f8cb4d'); disc.addColorStop(1, '#f5b400')
    ctx.beginPath(); ctx.arc(sx, sy, 70, 0, Math.PI * 2); ctx.fillStyle = disc; ctx.fill()
    ctx.restore()
    ctx.fillStyle = 'rgba(243,238,227,0.4)'; ctx.fillRect(0, HORIZON, W, 2)

    // HUD
    ctx.textAlign = 'left'; ctx.font = `24px ${frame.fonts.mono}`; ctx.fillStyle = 'rgba(243,238,227,0.85)'
    ctx.fillText('sunrise-native.mjs · no browser', 120, 84)
    ctx.textAlign = 'right'
    ctx.fillText(`frame ${frame.index} / ${FRAMES}`, W - 120, 84)
    ctx.fillStyle = 'rgba(243,238,227,0.22)'; ctx.fillRect(120, 106, W - 240, 3)
    ctx.fillStyle = '#f5b400'; ctx.fillRect(120, 106, (W - 240) * t, 3)

    // Title
    ctx.textAlign = 'left'; ctx.fillStyle = '#f3eee3'; ctx.font = `104px ${frame.fonts.display}`
    ctx.fillText('Video is', 120, 900)
    ctx.fillText('light over time.', 120, 1000)
  },
}
