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

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

  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),
          ),
        });
      });

    const entities = [
      ...grasses,
      new Player({ x: 9, y: 5 }),
      // Cadr
      new Wall({ x: 2, y: 3 }),
      new Wall({ x: 2, y: 4 }),
      new Wall({ x: 5, y: 3 }),
      new LambdaFactory({ x: 4, y: 4 }, "(\\ (x) . x)", 1),
      new FunctionApplication({ x: 3, y: 5 }, "(_INPUT _KEY)"),
      new Wall({ x: 2, y: 5 }),
      new Wall({ x: 4, y: 5 }),
      new FunctionApplication({ x: 2, y: 6 }, "(_INPUT _EMPTY)"),
      new Wall({ x: 4, y: 7 }),
      new Wall({ x: 3, y: 7 }),
      new Wall({ x: 2, y: 7 }),
      new Wall({ x: 9, y: 3 }),
      // Car
      new LambdaFactory({ x: 10, y: 4 }, "(\\ (x) . x)", 1),
      new Wall({ x: 12, y: 4 }),
      new FunctionApplication({ x: 11, y: 5 }, "(_INPUT _EMPTY)"),
      new Wall({ x: 10, y: 5 }),
      new Wall({ x: 12, y: 5 }),
      new Wall({ x: 12, y: 3 }),
      new FunctionApplication({ x: 12, y: 6 }, "(_INPUT _KEY)"),
      new Wall({ x: 10, y: 7 }),
      new Wall({ x: 11, y: 7 }),
      new Wall({ x: 12, y: 7 }),
      // solve!
      new LockedDoor({ x: 7, y: 8 }),
      new LockedDoor({ x: 7, y: 9 }),
      new Curry({ x: 7, y: 10 }),
      new Wall({ x: 6, y: 9 }),
      new Wall({ x: 8, y: 9 }),
      new Wall({ x: 6, y: 10 }),
      new Wall({ x: 8, y: 10 }),
      new Wall({ x: 7, y: 11 }),
    ];

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