import { Hex } from "../../util/Hex";
import { ECellType } from "../ECellType";
import { SimMain } from "../SimMain";
import { SimCommand } from "./SimCommand";

export class SimCommandBlockTerrain extends SimCommand {
    private hex: Hex;

    constructor(hex: Hex) {
        super();
        this.hex = hex;
    }

    public execute(simMain: SimMain) {
        const level = simMain.currentLevel;
        if (!level) return;

        const cellIndex = level.getCellIndex(this.hex);
        const cell = level.cells[cellIndex];
        if (cell.type != ECellType.Free || cell.tower != null || !level.reservePaths(simMain.gdRoot, this.hex)) {
            return;
        }
        cell.type = ECellType.Blocked;
        level.updateBlockedType(cell);
        for (let i = 0; i < 6; ++i) {
            const neighbourHex = Hex.neighbour(cell.hex, i);
            const neighbourIndex = level.getCellIndex(neighbourHex);
            level.updateBlockedType(level.cells[neighbourIndex]);
        }
        level.updatePaths(simMain.gdRoot);
    }
}
