import { Loader, Sprite } from "pixi.js"
import { Door } from "./Components/door"
//import { DebugRect } from "./Components/rendering/debugRect"
//import { roomBounds } from "./constants"
import globals from "./globals"
import { createRandomHuman } from "./util"
import { Position } from "./Components/position"
import { Point } from "./Datatypes/MathTypes/point"
import { World } from "ecsy"
import { TestSystem } from "./Systems/TestSystem"
import { AdventureReturnSystem } from "./Systems/AdventureReturnSystem"
import { DoorSystem } from "./Systems/DoorSystem"
import { RandomWalkSystem } from "./Systems/RandomWalkSystem"
import { PathWalkerSystem } from "./Systems/PathWalkerSystem"
import { VisibleHumanSystem } from "./Systems/VisibleHumanSystem"
import { SpriteSystem } from "./Systems/rendering/SpriteSystem"
import { MouseSystem } from "./Systems/ClickableSystem"
import { InfoSystem } from "./Systems/UI/InfoSystem"
import { DebugRenderSystem } from "./Systems/rendering/DebugRenderSystem"
import { ZOrderSystem } from "./Systems/rendering/ZOrderSystem"
import { PixiCleanupSystem } from "./Systems/rendering/PixiCleanupSystem"
import { RenderSystem } from "./Systems/rendering/RenderSystem"
import { Vector } from "./Datatypes/MathTypes/vector"
import { TooltipSystem } from "./Systems/UI/TooltipSystem"

export function setup() {
  // Create world and register the systems on it
  //we could also register components here but they should get automatically registered once used for the first time
  globals.world = new World()
  globals.world
    //before everything -100 to -1
    .registerSystem(TestSystem) //prio -100
    .registerSystem(AdventureReturnSystem) //prio -100
    //most stuff 0 to 50
    .registerSystem(DoorSystem) //prio 0
    .registerSystem(RandomWalkSystem) //prio 0
    .registerSystem(PathWalkerSystem) //prio 50
    .registerSystem(VisibleHumanSystem) //prio 50
    //rendering 51 - 100
    .registerSystem(InfoSystem) //prio 60
    .registerSystem(TooltipSystem)
    .registerSystem(SpriteSystem) //prio 70
    .registerSystem(DebugRenderSystem) //prio 70
    .registerSystem(MouseSystem) //prio 80
    .registerSystem(ZOrderSystem) //prio 80
    .registerSystem(PixiCleanupSystem) //prio 99
    .registerSystem(RenderSystem) //prio 100

  let resources = Loader.shared.resources

  //base sprites without entity representation
  const bgTex = new Sprite(resources["Background"].texture)
  globals.app.stage.addChild(bgTex)

  //start entities

  //door
  globals.world
    .createEntity()
    .addComponent(Position, <Position>{ value: new Point(76, 4) })
    .addComponent(Door, <Door>{
      open: true,
      openOffset: new Vector(0),
      openTex: resources["Door"].spritesheet.textures[0],
      closedOffset: new Vector(0),
      closedTex: resources["Door"].spritesheet.textures[1],
    })

  //debug room bounds
  //globals.world
  //  .createEntity()
  //  .addComponent(DebugRect, <DebugRect>{ color: 0x0000ff, rect: roomBounds })

  //example humans
  //TODO delete those
  for (let i = 0; i < 5; i++) createRandomHuman(globals.world)
}
