import { Human } from "../Components/human"
import { Name } from "../Components/name"
import { Appearance } from "../Components/appearance"
import { Loader } from "pixi.js"
import { InCabin } from "../Components/inCabin"
import { Position } from "../Components/position"
import { OrderZ } from "../Components/rendering/orderZ"
import { AABB } from "../Datatypes/MathTypes/aabb"
import { System, Entity } from "ecsy"
import globals from "../globals"

// MovableSystem
export class TestSystem extends System {
  priority = -100
  // This method will get called on every frame by default
  execute(delta: number) {
    //this.removeHumans(this.queries.humans, 2)
    //this.addHumans(2)
  }

  removeHumans(query: { results: Entity[] }, amount: number): void {
    for (let i = 0; i < amount; i++) {
      let ent = query.results[i]
      ent.remove()
    }
  }
  addHumans(amount: number): void {
    let roomBounds = new AABB(10, 17, 62, 56)
    let resources = Loader.shared.resources
    for (let i = 0; i < amount; i++) {
      globals.world
        .createEntity()
        .addComponent(Human)
        .addComponent(Name, <Name>{ first: "Sarah", last: "Lee" })
        .addComponent(Appearance, <Appearance>{
          idleTexture: resources["Human"].texture,
        }) //Todo: generate appearance from body traits instead?
        .addComponent(InCabin)
        .addComponent(Position, <Position>{ value: roomBounds.randomPoint() })
        .addComponent(OrderZ)
    }
  }

  static queries = {
    humans: {
      components: [Human],
    },
  }
  queries: any
}
