/// <reference path="../Action.ts" />
/// <reference path="../Rule.ts" />
/// <reference path="../Rulebook.ts" />
class ActionFollow extends Action {
    public static check = new Rulebook<ActionFollow>("Check Follow");
    public static carry = new Rulebook<ActionFollow>("Carry out Follow");

    public constructor (actor : Thing, ...nouns : Array<any>) {
        super(actor, ...nouns);
        this.requiresNoun = true;
        this.requiresVisibility = false;
    }

    public getCommandText () {
        return "follow " + this.getNoun(0).getPrintedName();
    }

    public static isCloseEnough (actor : Person, stalked : Thing) {
        let cRoom = <RoomRandom> actor.getRoom();
        let tRoom = <RoomRandom> stalked.getRoom();
        let distance = tRoom.getDistanceTo(cRoom);

        if (distance == 1 || distance < (actor.getStat(Attributes.Intelligence)/2)) {
            return true;
        }
        return false;
    }

    public checkFollowClose = ActionFollow.check.createAndAddRule({
        name : "Follow - Is the stalked close enough?",
        code : (rulebook) => {
            let action = <ActionFollow> rulebook.noun;
            let actor = <Person> action.actor;
            let stalked = action.getNoun(0);
            let cRoom = <RoomRandom> actor.getRoom();
            let tRoom = <RoomRandom> stalked.getRoom();

            if (ActionFollow.isCloseEnough(actor, stalked)) {
                let direction = cRoom.getAStarBestDirectionTo(tRoom);
                return new ActionGo(actor, direction);
            } else {
                action.stop();
            }
        }
    })
}

Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
    {
        name : "Hyperlink - Follow",
        firstPriority : Rule.PRIORITY_HIGHEST,
        code : (rulebook : RulebookRunner<Thing>) => {
            let thing = <Thing> rulebook.noun;

            if (thing instanceof Person && thing.getRoom() != WorldState.player.getRoom() && ActionFollow.isCloseEnough(WorldState.player, thing)) {
                Elements.HyperlinkHandler.addAvailableAction("Follow", new ActionFollow(WorldState.player, thing));
            }
        }
    }
));