/// <reference path="../Forest.ts" />
/// <reference path="ForestBed.ts" />
/// <reference path="OrcVillage.ts" />
/// <reference path="WitchHut.ts" />
/// <reference path="MazeEntrance.ts" />
/// <reference path="TheObelisk.ts" />
/// <reference path="CentaurVillage.ts" />
/// <reference path="OminousCave.ts" />
PlayBegins.rulebook.addRule(new Rule({
    name : "Randomize Forest Region",
    firstPriority : Rule.PRIORITY_HIGHEST,
    code : async runner => {
        await Forest.region.randomize();
    }
}));

RegionRandom.rulebookRandomizeRegion.addRule(new Rule({
    name : "Add the first rooms to guarantee position.",
    firstPriority : Rule.PRIORITY_HIGHEST,
    code : async runner => {
        // Add the clearing in the woods
        let cells = [
            Forest.ForestBed,
            Forest.OrcVillageSouthGates,
            Forest.CentaurVillageSoutheastGates,
            Forest.Pond,
            Forest.WitchHut,
            Forest.MazeEntrance,
            Forest.OminousCave,
            Forest.TheObelisk,
        ];

        for (let i = 0; i < cells.length; i++) {
            let options : RandomizingRoomOptions = {
                map : Forest.region.map,
                room : cells[i],
                region : Forest.region
            };
            await RegionRandom.rulebookBeforePlaceRoom.execute({noun : options});
            await RegionRandom.rulebookPlaceRoom.execute({noun : options});
            await RegionRandom.rulebookAfterPlaceRoom.execute({noun : options});
        }

        let minimumAmountOfRooms = cells.length * 7;
        let placed = Forest.region.getRooms().length; // At this stage, every room is placed

        for (let i = (minimumAmountOfRooms - placed); i > 0; i--) {
            Forest.region.place(new Forest.region.fodderRoomClass());
        }
    },
    conditions : runner => {
        return runner.noun == Forest.region;
    }
}));

RegionRandom.rulebookRandomizeRegion.createAndAddRule({
    name : "Beautify the Forest - Plainify",
    firstPriority : RegionRandom.ruleAddExtraConnections.firstPriority,
    priority : RegionRandom.ruleAddExtraConnections.priority + 1,
    code : () => {
        ForestFodder.definedRooms(<Array<ForestFodder>> Forest.region.getRooms().filter((room : RoomRandom) => {
            return room.placed && room instanceof ForestFodder;
        }));
    }
});

RegionRandom.rulebookRandomizeRegion.createAndAddRule({
    name : "Beautify the Forest - Make Beaten Paths",
    firstPriority : RegionRandom.ruleAddExtraConnections.firstPriority,
    priority : RegionRandom.ruleAddExtraConnections.priority - 1,
    code : () => {
        ForestFodder.makePaths(<Array<ForestFodder>> Forest.region.getRooms().filter((room : RoomRandom) => {
            return room.placed && room instanceof ForestFodder;
        }));
    }
});

RegionRandom.rulebookRandomizeRegion.createAndAddRule({
    name : "Beautify the Forest - Just give descriptions to them",
    firstPriority : RegionRandom.ruleAddExtraConnections.firstPriority,
    priority : RegionRandom.ruleAddExtraConnections.priority - 2,
    code : () => {
        ForestFodder.panicModeBeautify(<Array<ForestFodder>> Forest.region.getRooms().filter((room : RoomRandom) => {
            return room.placed && room instanceof ForestFodder;
        }));
    }
});

RegionRandom.rulebookRandomizeRegion.addRule(new Rule({
    name : "After placing and doing extra connections",
    firstPriority : -1,
    code : async runner => {
        // Add the clearing in the woods
        let cells = [

        ];

        for (let i = 0; i < cells.length; i++) {
            let options : RandomizingRoomOptions = {
                map : Forest.region.map,
                room : cells[i],
                region : Forest.region
            };
            await RegionRandom.rulebookBeforePlaceRoom.execute({noun : options});
            await RegionRandom.rulebookPlaceRoom.execute({noun : options});
            await RegionRandom.rulebookAfterPlaceRoom.execute({noun : options});
        }
    },
    conditions : runner => {
        return runner.noun == Forest.region;
    }
}));