/// <reference path="./Classes/Rulebook.ts" />
/// <reference path="./Classes/Rule.ts" />
/// <reference path="Classes/Things/Person.ts" />
module EveryTurn {
    export var EveryTurn = new Rulebook("Every Turn");

    export var RunAIRule = EveryTurn.createAndAddRule({
        name : "Run NPC AI Rule",
        code : async function () {
            function isAIAvailable (person : Person) {
                return (person != WorldState.player
                    && ((person.getRoom() instanceof RoomRandom
                        && (<RoomRandom> person.getRoom()).placed)));
            }


            let people = <Array<Person>> Thing.InsideRoomRelation.getAnyRightType(Person).filter(isAIAvailable);
            for (let i = 0; i < people.length; i++) {
                let action = await people[i].AI.execute(people[i]);
                let person = people[i];
                let visible = people[i].isVisibleTo(WorldState.player);

                if (action != undefined) {
                    let printValue: Say = await action.execute();

                    if (
                        (
                            visible ||
                            person.isVisibleTo(WorldState.player)
                        ) && printValue != undefined) {
                        Elements.CurrentTurnHandler.printAsContent(printValue);
                    }
                }
            }
        }
    });

    export var incrementTurnCounterRule = EveryTurn.createAndAddRule({
        firstPriority: Rule.PRIORITY_LOWEST,
        priority: Rule.PRIORITY_LOWEST,
        name: "Increment Turn Counter",
        code: function () {
            WorldState.incrementWorldTurn();
        }
    });
}