import "luxe: game" for Game
import "luxe: assets" for Assets
import "luxe: input" for Input, Key
import "luxe: world" for World, Entity, Transform, Sprite, Values, Tags, Camera
import "luxe: math" for Math
import "luxe: draw" for Draw
import "luxe: io" for IO
import "luxe: color" for Color

import "outline/app" for App
import "outline/Renderer" for Renderer
import "blocks/ui" for Ui
import "blocks/debug" for DrawDebug, Holder
import "globals" for Globals, RandomInst
import "math/vector" for Vector
import "math/rect" for AABB
import "math/util" for Util
import "blocks/tooltip" for Tooltip
import "blocks/human/human" for Human

class game is Game {
  construct ready() {
    Globals["Game"] = this

    app = App.new()
    _ui = Ui.new(app)
    _tooltip = Tooltip.new(app)

    setup()
    DrawDebug.setup(app.world)

    Globals["Renderer"].events.listen(Renderer.on_change) { app.update_cam() }

  } //ready

  tick(delta) {
    var mouse_pos = Vector.new(Input.mouse_x(), Input.mouse_y())
    var game_mouse = Globals["Renderer"].game_mouse(mouse_pos)
    game_mouse = Camera.screen_point_to_world(app.camera, game_mouse.x, game_mouse.y)
    Globals["GameMouse"] = game_mouse
    var ui_mouse = Globals["Renderer"].ui_mouse(mouse_pos)
    ui_mouse = Camera.screen_point_to_world(app.ui_camera, ui_mouse.x, ui_mouse.y)
    Globals["UiMouse"] = ui_mouse

    if(Input.key_state_released(Key.escape)) {
      IO.shutdown()
    }

    if(Input.key_state_pressed(Key.key_q)) {
      _tooltip.cycle_size()
    }

    _ui.test()
    
    DrawDebug.rect(game_mouse.x, game_mouse.y, 1, 1)
    DrawDebug.commit()

    _tooltip.tick()

    app.tick(delta)

  } //tick

  destroy() {
    System.print("unready!")
    app.destroy()
  } //destroy

  setup(){
    var game_rect = Globals["GameRect"]

    var bg = Entity.create(app.world, "background")
    Transform.create(bg)
    Transform.set_pos(bg, 64, 64, -10)

    var material = Util.material_from_image_path("assets/wip/Room")
    Sprite.create(bg, material, 128, 128)

    create_human()
    create_human()
    create_human()
    create_human()
    create_human()
    create_human()
  }

  create_human(){
    var human = Entity.create(app.world, "human")
    var humanMat = Util.material_from_image_path("assets/wip/Human")
    Human.create(human)
    Human.set_color(human, Util.hsv(RandomInst.float(), 0.5, 1))
  }

  app { _app }
  app=(v) { _app=v }

  ui{_ui}

} //Game
