import { Entity, EntityNames } from ".";
import { Game } from "..";
import { BoundingBox, Colliding, Grid, Sprite } from "../components";
import { IMAGES, SPRITE_SPECS, SpriteSpec, Sprites } from "../config";
import { Coord2D } from "../interfaces";
import { LevelNames } from "../levels";
import { Level, SystemNames } from "../systems";

export class Curry extends Entity {
  private static spriteSpec: SpriteSpec = SPRITE_SPECS.get(
    Sprites.CURRY
  ) as SpriteSpec;

  constructor(gridPosition: Coord2D) {
    super(EntityNames.Curry);

    this.addComponent(new Grid(gridPosition));

    this.addComponent(new Colliding(this.collisionHandler));

    this.addComponent(
      new BoundingBox(
        {
          x: 0,
          y: 0,
        },
        {
          width: Curry.spriteSpec.width,
          height: Curry.spriteSpec.height,
        },
        0
      )
    );

    this.addComponent(
      new Sprite(
        IMAGES.get(Curry.spriteSpec.sheet)!,
        { x: 0, y: 0 },
        {
          width: Curry.spriteSpec.width,
          height: Curry.spriteSpec.height,
        },
        Curry.spriteSpec.msPerFrame,
        Curry.spriteSpec.frames
      )
    );
  }

  private collisionHandler(game: Game, entity: Entity) {
    if (entity.name !== EntityNames.Player) {
      return;
    }

    game.removeEntity(this.id);

    const levelSystem = game.getSystem<Level>(SystemNames.Level);
    levelSystem.setLevel(LevelNames.LevelSelection);
  }
}
