/// <reference path="../Action.ts" />
/// <reference path="../../../Elements/Modules/HyperlinkHandler.ts" />
/// <reference path="../../../Elements/Classes/Say/SayHeSheIt.ts" />

class ActionTalk extends Action {
    public static PRIORITY_SITUATION_DIALOGUE = 9;
    public static PRIORITY_POSSESSION_DIALOGUE = 7;
    public static PRIORITY_LOCATION_DIALOGUE = 5;
    public static PRIORITY_COMMON_DIALOGUE = 3;
    public static PRIORITY_GLOBAL_DIALOGUE = 1;

    public static check: Rulebook<ActionTalk> = new Rulebook("Check Talking");
    public static carry: Rulebook<ActionTalk> = new Rulebook("Carry out Talking");

    public static defaultCarryTalkingRule = ActionTalk.carry.createAndAddRule({
        name : "Talking - Check with the AI",
        firstPriority : Rule.PRIORITY_HIGHEST,
        priority : Rule.PRIORITY_HIGHEST,
        code : async (rulebook : RulebookRunner<ActionTalk>) => {
            let action = <ActionGo> rulebook.noun;
            //let actor = action.actor;
            let thing = (<Thing>action.getNoun(0));

            if (thing instanceof Person && action.actor instanceof Person) {
                let runAndStop : Array<DialogueTree> = [];
                let runAndContinue : Array<DialogueTree> = [];
                let circumstantialOptions : Array<DialogueHook> = [];

                await thing.AI.answerTo({greeter : action.actor, answerer : thing, options : circumstantialOptions, runAndStop : runAndStop, runFirst : runAndContinue});

                if (runAndStop.length > 0) {
                    await runAndStop[0].execute();
                    return true; // must stop executing now
                } else if (runAndContinue.length > 0) {
                    for (let i = 0; i < runAndContinue.length; i++) {
                        await runAndContinue[i].execute();
                    }
                }

                let investigativeOptions : Array<DialogueHook> = [];
                await thing.AI.interrogateTo({greeter : action.actor, answerer : thing, options : investigativeOptions, runAndStop : runAndStop, runFirst : runAndContinue});

                if (investigativeOptions.length == 0 && circumstantialOptions.length == 0) {
                    return;
                }

                let choices : Array<Say> = [];
                let results = [];
                if (investigativeOptions.length > 0) {
                    // TODO: Add more textx
                    choices.push(new Say(new OneOf(OneOf.PURELY_AT_RANDOM, "Ask about...")));
                    results.push(null);
                }
                for (let i = 0; i < circumstantialOptions.length; i++) {
                    choices.push(circumstantialOptions[i].text);
                    results.push(circumstantialOptions[i].tree);
                }

                // TODO: Add more texts
                choices.push(new Say(new OneOf(OneOf.PURELY_AT_RANDOM, "Goodbye")));
                results.push(undefined);


                let choice = await Controls.giveChoices(true, ...choices);

                if (results[choice[1]] === null) {
                    choices = [];
                    results = [];
                    for (let i = 0; i < investigativeOptions.length; i++) {
                        choices.push(investigativeOptions[i].text);
                        results.push(investigativeOptions[i].tree);
                    }
                    choice = await Controls.giveChoices(true, ...choices);
                    if (results[choice[1]] instanceof DialogueTree) {
                        await (results[choice[1]]).execute();
                    }
                } else if (results[choice[1]] instanceof DialogueTree) {
                    await (results[choice[1]]).execute();
                }

                return true;
            }
        }
    });

    public static lastCarryTalkingRule = ActionTalk.carry.createAndAddRule({
        name : "Talking - Doesn't want to talk",
        firstPriority : -1,
        priority : -1,
        code : (rulebook : RulebookRunner<ActionTalk>) => {
            let action = <ActionGo> rulebook.noun;
            //let actor = action.actor;
            let thing = (<Thing>action.getNoun(0));

            if (thing instanceof Person) {
                action.say = new Say(...Say.Mention(action.actor), " greet", action.actor == WorldState.player ? " " : "s ", ...Say.Mention(thing), ". There is no response.");
            } else if (action.actor == WorldState.player) {
                action.say = new Say("How are you going to talk to that?")
            }
        }
    });

    public getCommandText () {
        return "talk to " + (this.getNoun(0) != undefined ? this.getNoun(0).getPrintedName() : "");
    }
}

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

            if (thing instanceof Person && thing.getRoom() == WorldState.player.getRoom()) {
                Elements.HyperlinkHandler.addAvailableAction("Talk", new ActionTalk(WorldState.player, thing));
            }
        }
    }
));