package worldoffri;

import java.util.Scanner;

/**
 * This class is part of the "World of Zuul" application. 
 * "World of Zuul" is a very simple, text based adventure game.  
 * 
 * This parser reads user input and tries to interpret it as an "Adventure"
 * command. Every time it is called it reads a line from the terminal and
 * tries to interpret the line as a two word command. It returns the command
 * as an object of class Command.
 *
 * The parser has a set of known command words. It checks user input against
 * the known commands, and if the input is not one of the known commands, it
 * returns a command object that is marked as an unknown command.
 * 
 * @author  Michael Kolling and David J. Barnes
 * @version 2006.03.30
 */
public class Parser 
{
    private NazvyPrikazov aPrikazy;  // holds all valid command words
    private Scanner aCitac;         // source of command input

    /**
     * Create a parser to read from the terminal window.
     */
    public Parser() 
    {
        aPrikazy = new NazvyPrikazov();
        aCitac = new Scanner(System.in);
    }

    /**
     * @return The next command from the user.
     */
    public Prikaz dajPrikaz() 
    {
        String vstupnyRiadok;   // will hold the full input line
        String prikaz = null;
        String parameter = null;

        System.out.print("> ");     // print prompt

        vstupnyRiadok = aCitac.nextLine();

        // Find up to two words on the line.
        Scanner tokenizer = new Scanner(vstupnyRiadok);
        if(tokenizer.hasNext()) {
            prikaz = tokenizer.next();      // get first word
            if(tokenizer.hasNext()) {
                parameter = tokenizer.next();      // get second word
                // note: we just ignore the rest of the input line.
            }
        }

        // Now check whether this word is known. If so, create a command
        // with it. If not, create a "null" command (for unknown command).
        if(aPrikazy.jePrikaz(prikaz)) {
            return new Prikaz(prikaz, parameter);
        }
        else {
            return new Prikaz(null, parameter); 
        }
    }
}
