/// <reference path="../../AI.ts" />
module AIRules {

    export function printGrudgeResult (atom : ContentAtomAnnoy) : Say {
        return ContentMoleculeAnnoy.getSay(new ContentMolecule(atom));
    }

    export var actedUponAggressively = AI.reacttoRules.createAndAddRule({
        name : "Reacting to Aggressive action",
        code : (runner : RulebookRunner<Person>) => {
            let person = runner.noun;
            let pai = runner.noun.AI;
            let action = pai.reactingTo;

            person.AI.addHostility(action.actor, action.aggressivenessRating);
            person.reputation -= action.aggressivenessRating;

            if (person.AI.hostileTargets.includes(action.actor)) {
                return; // Already hostile
            }
            if (action.actor == WorldState.player) {
                person.reputation -= action.aggressivenessRating;
            }

            let ai = person.AI;
            let response : Say;
            let gain = ai.grudgeRate * action.aggressivenessRating;
            let actionLevel = ContentAtomAnnoy.TYPE_LOW;
            let result = ContentAtomAnnoy.RESULT_FRIENDLY;
            if (ai.getHostilityTo(action.actor) > 100) {
                result = ContentAtomAnnoy.RESULT_HOSTILE;
            } else if (ai.retaliates && ai.getHostilityTo(action.actor) >= (ai.hostileThreshold / 2)) {
                result = ContentAtomAnnoy.RESULT_PISSED;
            }
            if (gain >= (ai.hostileThreshold / 2)) {
                actionLevel = ContentAtomAnnoy.TYPE_HIGH;
            } else if (gain >= (ai.hostileThreshold / 4)) {
                actionLevel = ContentAtomAnnoy.TYPE_MEDIUM;
            }
            response = printGrudgeResult(new ContentAtomAnnoy(action.actor, person, [actionLevel, result]));

            let nAct : Action;
            if (result != ContentAtomAnnoy.RESULT_FRIENDLY) {
                nAct = new ActionAttack(person, action.actor);
                nAct.finalSay = response.add(Say.PARAGRAPH_BREAK);
                nAct.finalSayOnEnd = false;
                person.AI.storedReaction = nAct;
            } else {
                // nAct = new ActionWait(person);
                // nAct.finalSay = response.add(Say.PARAGRAPH_BREAK);
                // Stop waiting when hit?
            }
        },
        conditions : (runner : RulebookRunner<Person>) => {
            let pai = runner.noun.AI;
            let action = pai.reactingTo;
            return action.actingAgressively;
        }
    })
}