import { Level, LevelNames } from ".";
import { Game } from "..";
import {
  Curry,
  FunctionApplication,
  Grass,
  LambdaFactory,
  LockedDoor,
  Player,
  Sign,
  Wall,
} from "../entities";
import { Grid, SystemNames } from "../systems";
import { normalRandom } from "../utils";

export class Tutorial extends Level {
  constructor() {
    super(LevelNames.Tutorial);
  }

  public init(game: Game) {
    const grid = game.getSystem<Grid>(SystemNames.Grid);
    const dimensions = grid.getGridDimensions();

    const grasses = Array.from({ length: dimensions.width })
      .fill(0)
      .map(() => {
        // random grass
        return new Grass({
          x: Math.floor(
            normalRandom(dimensions.width / 2, dimensions.width / 4, 1.5),
          ),
          y: Math.floor(
            normalRandom(dimensions.height / 2, dimensions.height / 4, 1.5),
          ),
        });
      });

    // TODO: new level which adds introductory syntax
    const entities = [
      ...grasses,
      new Sign(
        "<div>this is a Lambda Factory<br><br>modify the produced term by interacting from the top or bottom ↕️<br><br>then produce the term by pressing the button on the left or right ↔️<br><br></div>",
        { x: 4, y: 3 },
      ),
      new Sign(
        "<div>this is a Term Application; interact to view its code<br><br>push the term ➡️ created by the factory any direction into the Application to produce a new one 💭<br><br>note that:<br><br>+ _INPUT is the term replaced by the pushed term<br><br>+ in this case _KEY is applied to the function to make a new KEY! 🔑</div>",
        { x: 4, y: 6 },
      ),
      new Wall({ x: 10, y: 9 }),
      new Wall({ x: 10, y: 11 }),
      new Wall({ x: 11, y: 10 }),
      new Curry({ x: 10, y: 10 }),
      new LockedDoor({ x: 9, y: 10 }),
      new LambdaFactory({ x: 6, y: 3 }, "// TODO: Remove this comment\n(λ (x) . x)", 3),
      new FunctionApplication({ x: 6, y: 6 }, "(_INPUT _KEY)"),
      new Player({ x: 2, y: 2 }),
    ];

    entities.forEach((entity) => game.addEntity(entity));
  }
}
