import { Application, settings, SCALE_MODES, Ticker } from "pixi.js"
import globals from "./globals"
import { canvasWidth, canvasHeight } from "./constants"
import { loadResources } from "./resources"
import { setup } from "./setup"

// Initialize pixi
globals.app = new Application({
  width: canvasWidth,
  height: canvasHeight,
  backgroundColor: 0x10101010,
  resolution: 10,
  antialias: false,
})
document.body.appendChild(globals.app.view)

settings.SCALE_MODE = SCALE_MODES.NEAREST
settings.ROUND_PIXELS = true

recalculateSize()

window.addEventListener("resize", recalculateSize)

function recalculateSize() {
  let multiplier = Math.min(
    (window.innerWidth / canvasWidth) | 0,
    (window.innerHeight / canvasHeight) | 0
  )
  globals.app.view.style.width = canvasWidth * multiplier + "px"
  globals.app.view.style.height = canvasHeight * multiplier + "px"
}

loadResources(init)

function init() {
  setup()

  // Run!
  Ticker.shared.add((delta: number) => {
    let time = performance.now()
    // Run all the systems
    globals.world.execute(delta / 100, time)
  })
}
