class ForestFodder extends RoomRandomFodder {
    constructor () {
        super();
        //this.cloneInterface(ForestFodder.POSSIBILITIES.getOne());
        this.backgroundImage = "roomForestFodder";
    }

    public static beautified : Array<ForestFodder> = [];
    // This defines the important rooms first
    public static definedRooms (rooms : Array<ForestFodder>) {
        let plainifyAround = [
            [Forest.CentaurCoordinates[0] - 1, Forest.CentaurCoordinates[1] + 1, 5, Forest.CentaurCoordinates],
            [Forest.OrcCoordinates[0] + 1, Forest.OrcCoordinates[1] + 1, 5, Forest.OrcCoordinates]
        ];

        plainifyAround.forEach((coordinates) => {
            let availRooms = rooms.filter(room => {
                return !ForestFodder.beautified.includes(room) && room instanceof ForestFodder && room.getDistanceToCoordinates(<any> coordinates) <= coordinates[2];
            });
            availRooms.forEach((room) => {
                ForestFodder.beautified.push(room);
                room.cloneInterface(ForestFodder.PlainsPossibility);
                if (coordinates[3] == Forest.CentaurCoordinates) {
                    let directionTo = room.getOverallDirectionTo(coordinates);
                    room.description.add("You can see a settlement with wooden walls to the " + DirectionNames[directionTo] + ".");
                }
                if (coordinates[3] == Forest.OrcCoordinates) {
                    let directionTo = room.getOverallDirectionTo(coordinates);
                    room.description.add("You can see a fortified settlement with spiked defensive walls to the " + DirectionNames[directionTo] + ".");
                }
                room.extraConnectionChance = 100;
            });
        });
    }

    // This makes the paths and mustbe done AFTER the extra connections happen
    public static makePaths (rooms : Array<ForestFodder>) {
        // initial rules:
        // There is a beaten path from the Witch Hut to the Centaur Village
        // There will be a beaten path from the Orc Village to the Ominous Cave.
        let beatenPath : Array<ForestFodder> = [];
        let destinations : Array<Room> = [Forest.WitchHut, Forest.CentaurVillageSoutheastGates];
        function makePath (start, end) {
            let pathTo = start.getAStarPathTo(end);
            pathTo.forEach((path) => {
                let room = path.room;
                if (room instanceof ForestFodder && !beatenPath.includes(room)) {
                    beatenPath.push(room);
                }
            });
            if (!destinations.includes(start)) destinations.push(start);
            if (!destinations.includes(end)) destinations.push(end);
        }

        makePath(Forest.WitchHut, Forest.CentaurVillageSoutheastGates);
        makePath(Forest.WitchHut, Forest.MazeEntrance);
        makePath(Forest.OrcVillageNorthwestGates, Forest.OminousCave);
        // makePath(Forest.OrcVillageSouthGates, Forest.Pond);
        // makePath(Forest.CentaurVillageSoutheastGates, Forest.Pond);

        beatenPath.forEach(room => {
            room.cloneInterface(ForestFodder.BeatenPathPossibility);
            if (!ForestFodder.beautified.includes(room)) ForestFodder.beautified.push(room);
            let pathFollows = "The path continues ";
            let directions = [];
            room.connections.forEach((room : RoomRandom, direction : Direction) => {
                if (beatenPath.includes(<any> room) || destinations.includes(room)) {
                    directions.push(DirectionNames[direction].toLowerCase());
                }
            });
            for (let i = 0; i < directions.length; i++) {
                if ((i + 1) == directions.length) {
                    pathFollows += " and ";
                }
                pathFollows += directions[i];
                if ((i + 1) < directions.length && directions.length > 2) {
                    pathFollows += ", ";
                }
            }
            pathFollows += ".";
            room.description.add(pathFollows);
        });
    }

    // Last resort to find purpose for rooms.
    public static panicModeBeautify (rooms : Array<ForestFodder>) {
        // Give purpose to rooms without purpose
        let realFodder = 0;
        rooms.forEach(room => {
            if (!ForestFodder.beautified.includes(room)) {
                room.cloneInterface(ForestFodder.POSSIBILITIES.getOne());
                realFodder +=1;
            }
        });
        console.log(realFodder.toString() + " rooms were left without real purpose, out of " + rooms.length.toString() + " rooms.");
    }

    public static POSSIBILITIES : OneOf = new OneOf(OneOf.ROTATING_RANDOM);
    public static addPossibility (...pos : Array<FodderPossibility>) {
        ForestFodder.POSSIBILITIES.addPossibilities(...pos);
    }

    public static BeatenPathPossibility : FodderPossibility = {
        name : "Beaten path",
        description: "You are in a beaten path through the woods. "
    };

    public static PlainsPossibility : FodderPossibility = {
        name : "Plains",
        description : "The woods have mostly cleared up, leaving you in open fields with lush green grass. "
    }
}

ForestFodder.addPossibility({
    name : "Clearing in the Woods",
    description : new Say("The trees have cleared around here and you can finally see something other than leaves up ahead, even though the lush green plains leave you exposed.")
});

ForestFodder.addPossibility({
    name : "Dense Forest",
    description : new Say("There are so many trees around that you can barely see where you are going, the floor is littered with sticks and leaves.")
});

ForestFodder.addPossibility({
    name : "Flowery Plains",
    description : new Say("This colorful area has dozens of types of flowers all around.")
});

ForestFodder.addPossibility({
    name : "Pond",
    description : new Say("The forest opens up to a crystalline pond.")
});