package thb.jeanluc.adventure.command.impl;

import thb.jeanluc.adventure.command.Command;
import thb.jeanluc.adventure.game.Conditions;
import thb.jeanluc.adventure.game.GameContext;
import thb.jeanluc.adventure.model.DialogueLine;
import thb.jeanluc.adventure.model.Npc;

import java.util.List;
import java.util.Optional;

/**
 * Prints an NPC's greeting text. Usage: {@code talk <npc-id>}.
 */
public class TalkCommand implements Command {

    @Override
    public void execute(GameContext ctx, List<String> args) {
        if (args.isEmpty()) {
            ctx.getIo().write("Talk to whom?");
            return;
        }
        String npcId = args.getFirst();
        Optional<Npc> npc = ctx.getPlayer().getCurrentRoom().findNpc(npcId);
        if (npc.isEmpty()) {
            ctx.getIo().write("There is no '" + npcId + "' here to talk to.");
            return;
        }
        Npc found = npc.get();
        String line = found.getGreeting();
        for (DialogueLine dl : found.getDialogue()) {
            if (Conditions.all(dl.when(), ctx)) {
                line = dl.text();
                break;
            }
        }
        ctx.getIo().write(found.getName() + " says: " + line);
    }

    @Override
    public String help() {
        return "talk <npc>  -  start a conversation with an NPC (alias: speak)";
    }
}
