/// <reference path="../Things/Clothing.ts" />
/// <reference path="../Action.ts" />
/// <reference path="../Rule.ts" />
/// <reference path="../Rulebook.ts" />
/// <reference path="../../../Elements/Classes/Say/SayBold.ts" />
/// <reference path="../../../Elements/Modules/HyperlinkHandler.ts" />

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

    /**
     * Needs to return a string explaining what the player will do if he does this action.
     * For instance, ActionTaking should return something like return "take " + this.nouns[0].getName(),
     *   which would read as "take thing".
     *   remember that things implement PRINTABLE interface, so you can get their names.
     * @returns {Say}
     */
    public getCommandText () {
        return "take off " + (this.getNoun(0) != undefined ? this.getNoun(0).getPrintedName() : "");
    }

    public getClothing () : Clothing{
        return this.getNoun(0);
    }

    public static checkIsWearable = ActionRemove.check.createAndAddRule({
        name : "Is noun a clothing",
        firstPriority : Rule.PRIORITY_HIGHEST,
        code : (runner : RulebookRunner<ActionRemove>) => {
            let action = runner.noun;
            let noun = action.getClothing();
            if (!(noun instanceof Clothing)) {
                if (action.actor == WorldState.player) {
                    action.say.add("You can only remove Clothing.");
                }
                return false;
            }
        }
    });

    public static checkIsHeld = ActionRemove.check.createAndAddRule({
        name : "Is noun worn",
        firstPriority : Rule.PRIORITY_HIGH,
        code : async (runner : RulebookRunner<ActionRemove>) => {
            let action = runner.noun;
            let noun = action.getClothing();
            if (!Thing.WearRelation.isRight(action.actor, noun)) {
                if (action.actor == WorldState.player) {
                    action.say.add("You are not wearing it.");
                }
                return false;
            }
        }
    });

    public static carryDefault = ActionRemove.carry.createAndAddRule({
        name : "Set Clothing as Carried",
        firstPriority : Rule.PRIORITY_MEDIUM,
        code : (runner : RulebookRunner<ActionRemove>) => {
            let action = runner.noun;
            let noun = action.getClothing();
            Thing.CarryRelation.setRelation(action.actor, noun);

            let actor = action.actor;
            let thing = (<Thing>action.getNoun(0));

            if (actor == WorldState.player) {
                action.say.add(new SayBold(thing, ": "), "Removed.");
            } else {
                action.say.add(new SayThe(), actor, " takes off ", new SayThe(), thing, ".");
            }
        }
    });
}
/**
 * Hyperlinking
 */
Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
    {
        name : "Hyperlink - Remove",
        firstPriority : Rule.PRIORITY_HIGHEST,
        code : (rulebook : RulebookRunner<Thing>) => {
            let thing = <Thing> rulebook.noun;

            if (thing instanceof Clothing && (Thing.WearRelation.isRight(WorldState.player, thing))) {
                Elements.HyperlinkHandler.addAvailableAction("Remove", new ActionRemove(WorldState.player, thing));
            }
        }
    }
));

/**
 Inventory
 */
Elements.InventoryHandler.LinkingThing.addRule(new Rule(
    {
        name : "Inventory - Remove",
        firstPriority : Rule.PRIORITY_LOWEST,
        code : (rulebook : RulebookRunner<Thing>) => {
            let thing = <Thing> rulebook.noun;

            if (thing instanceof Clothing && (Thing.WearRelation.isRight(WorldState.player, thing))) {
                Elements.InventoryHandler.printThingLink("R", new ActionRemove(WorldState.player, thing));
            }
        }
    }
));