import "luxe: io" for IO
import "luxe: world" for World, Entity, Modifiers, ModifierSystem, Transform, Sprite
import "math/vector" for Vector
import "math/util" for Util
import "globals" for Globals
import "luxe: assets" for Strings

//User facing API
//This is what the user of your modifier will interact with
class Human {

  static create(entity) { Modifiers.create(This, entity) }
  static destroy(entity) { Modifiers.destroy(This, entity) }
  static has(entity) { Modifiers.has(This, entity) }

  static set_color(entity, color){
    var data = Modifiers.get(This, entity)
    data.color = color
    if(Sprite.has(entity)){
      Sprite.set_color(entity, color.r, color.g, color.b, color.a)
    }
  }
  static set_name(entity, name){
    var data = Modifiers.get(This, entity)
    var id = Strings.add(name)
    System.print("Setting name of %(entity) to %(name) via string id of %(id).")
    //data.name = id
  }

} //Human

//Your modifier system implementation.
//This speaks to the engine and your user facing API
//to do the actual work. You'll get notified when things change
//in the world and respond to them here.
class HumanSystem is ModifierSystem {
  
  player_start{[26, 89]}
  player_size{[10, 12]}

  construct new() {
    //called when your system is first created.
  }

  init(world) {
    _world = world
    //called when your modifier is created in a world

    _material = Util.material_from_image_path("assets/wip/Human")
  }

  destroy() {
    //called when your modifier is removed from a world
  }

  attach(entity, data) {
    //called when attached to an entity
    update()
  }

  detach(entity, data) {
    //called when detached from an entity, like on destroy
    update()
  }

  tick(delta) {
      //called usually once every frame.
      //called when the world that this modifier lives in, is ticked
    each {|entity, data|
      if(!data.active) return

      var mouse =  Globals["GameMouse"]
      var over = Sprite.contains(entity,mouse.x, mouse.y)
      if(over && _hoverEnt != entity){
        _hoverEnt = entity
        Globals["Tooltip"].set(Strings.get(data.name), entity)
      }
      if(!over && _hoverEnt == entity){
        _hoverEnt = null
        Globals["Tooltip"].clear(entity)
      }
    }
  } //tick

  update(){
    var size = Vector.new(player_size)
    var pos = Vector.new(player_start) + size / 2
    each {|entity, data|
      if(!data.active){
        Transform.destroy(entity)
        Sprite.destroy(entity)
        return
      }
      if(!Transform.has(entity)){
        Transform.create(entity)
      }
      if(!Sprite.has(entity)){
        Sprite.create(entity, _material, 10, 12)
        Sprite.set_color(entity, data.color.r, data.color.g, data.color.b, 1)
      }
      Transform.set_pos(entity, pos.x, pos.y)
      pos.x = pos.x + size.x
    }
  }
} //HumanSystem

var Modifier = HumanSystem //required
