import "luxe: world" for World, Camera, Entity, Transform
import "luxe: render" for Render
import "luxe: bytes" for Floats

import "outline/Renderer" for Renderer
import "globals" for Globals
import "math/rect" for AABB


class App {

  world       { _world }
  ui          { _ui_world }

  camera      { _camera }
  ui_camera   { _ui_camera }

  color       { _color }
  color=(v)   { _color = v }

  width       { Render.window_w() }
  height      { Render.window_h() }
  scale       { Render.drawable_ratio() }

  construct new() {

    _color = [0,0,0.2,1]

    //create worlds

      _world = World.create("game")
      _ui_world = World.create("ui")

    //create cameras

      _camera = Entity.create(_world, "app.camera")
      Transform.create(_camera)
      Camera.create(_camera)
      Camera.set_default(_world, _camera)

      _ui_camera = Entity.create(_ui_world, "app.ui_camera")
      Transform.create(_ui_camera)
      Camera.create(_ui_camera)
      Camera.set_default(_ui_world, _ui_camera)

      update_cam()

  } //new

  update_cam(){
      Camera.ortho(_camera, 0, 128, 128, 0, -2000, 2000)
      Camera.ortho(_ui_camera, 0, Globals["Renderer"].height, Globals["Renderer"].width, 0, -2000, 2000)
  }

  destroy() {

    //destroy cameras

      Camera.destroy(_camera)
      Camera.destroy(_ui_camera)

      Entity.destroy(_camera)
      Entity.destroy(_ui_camera)

    //destroy worlds

      World.destroy(_ui_world)
      World.destroy(_world)

  } //destroy

  tick(delta) {

    //update worlds

      World.tick_systems(_world, delta)
      World.tick_systems(_ui_world, delta)

      World.tick_world(_world, delta)
      World.tick_world(_ui_world, delta)

    //render worlds
      render_empty("clear")
      World.render(_world, _camera, "game", "",  Globals["RelativeGameRect"].pos_size_list, {"clear_color":_color})
      World.render(_ui_world, _ui_camera, "ui")
      render_empty("copy_to_screen")

  } //tick

  render_empty(path){
    //prepare data
    if(!_empty_set) _empty_set = Render.create_set()
    var floats = Floats.new(16)

    //and submit
    Render.submit(
      _empty_set,        //empty render set 
      path, //render path pass to your renderer (origin of #1)
      "",         //the render target resource name
      [0, 0, 1, 1],    //region to render to
      floats, floats)   //matrices
  }

} //
