/// <reference path="../Action.ts" />
/// <reference path="../Rule.ts" />
/// <reference path="../Rulebook.ts" />
/// <reference path="../Things/Person.ts" />
import player = WorldState.player;

class ActionRest extends Action {
    public static check = new Rulebook<ActionRest>("Check Rest");
    public static carry = new Rulebook<ActionRest>("Carry out Rest");

    private waiting = false;

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

    public getCommandText () {
        return "rest on " + (<Thing> this.getNoun(0)).getPrintedName();
    }

    public static carryRest = ActionRest.carry.createAndAddRule({
        name : "Rest - Restful Moment",
        code : async (runner : RulebookRunner<ActionRest>) => {
            let actor = runner.noun.actor;
            if (actor instanceof Person) {
                let bodyparts = <Array<Bodypart>> actor.getParts(Bodypart);

                bodyparts.forEach(bodypart => {
                    bodypart.regenerate();
                });
                let action = runner.noun;
                if (WorldState.isPlayer(actor)) {
                    if (action.waiting || (WorldState.player.getHealthOnScale() <= 9 && await Controls.askForConsent("Rest until fully healed?"))) {
                        if (WorldState.player.getHealthOnScale() <= 9) {
                            TurnSequence.repeatedAction = action;
                            action.waiting = true;
                        } else {
                            action.waiting = false;
                        }
                    }

                    if (action.waiting) {
                        runner.noun.say.add("You keep resting on ", Say.Mention(runner.noun.getNoun(0)), ".");
                    } else {
                        runner.noun.say.add("You decide to rest on ", Say.Mention(runner.noun.getNoun(0)), ".");
                    }
                } else {
                    runner.noun.say.add(Say.Mention(action.actor), " rests on ", Say.Mention(action.getNoun(0)), ".");
                }
            }
        }
    });
}

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

            if (thing instanceof RestingStuff && thing.isVisibleTo(WorldState.player)) {
                Elements.HyperlinkHandler.addAvailableAction("Rest", new ActionRest(WorldState.player, thing));
            }
        }
    }
));