// Class: WordReader
//
// Author: Alyce Brady
//
// Created on 9 April 2020

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOError;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * This class contains code to read line from a file and to
 * break those lines up into individual words.
 * 
 * @author Alyce Brady
 * @version 9 April 2020
 */
public class MenuItemReader 
{
    private String filename;
    private BufferedReader reader;
    private int lineCount;
    private HashMap<String, Integer> wordCountMap;

    /** Constructs an object that can read words
     *  from a file.  It uses the following regular expression
     *  to parse words: ("\\P{L}+")
     *  (Thanks to Pierre C on Stack Overflow.)
     *      @param filename  the name of the file to open for reading
     */
    public MenuItemReader(String filename) 
    {
        this.filename = filename;
        this.lineCount = 0;
        this.wordCountMap = new HashMap<String, Integer>();

        // Get two readers: one for reading line-by-line, and one to
        // populate the word count map.
        BufferedReader mapReader = null;
        try
        {
            this.reader = new BufferedReader(new FileReader(filename));
            mapReader = new BufferedReader(new FileReader(filename));
        } 
        catch (IOException e)
        {
            throw new IOError(new Error("Cannot open file " + filename
                + " for reading"));
        }
    }

    /** Reads the next non-empty line from the file provided to the constructor.
     *  Precondition: the file must have been successfully opened for
     *                reading.
     *      @return void
     */
    public String getNextLine()
    {
        String nextLine = null;
        try 
        {
            // Read lines until hitting a null or blank line.
            while ( (nextLine = this.reader.readLine()) != null  )
            {
                nextLine = nextLine.trim();
                if ( nextLine.length() != 0 && ! nextLine.equals("") )
                {
                    lineCount++;
                    return nextLine;
                }
            }

            // End-of-file
            return null;

            
        } 
        catch (IOException e) 
        {
            System.err.println("Could not read file " + filename);
            return null;
        }      
    }

    /** Takes a line of text containing multiple words and returns an
     *  ArrayList of the individual words that were in the line.
     *      @param lineOfText   a line of text read in using this WordReader
     *      @return  a list of the words that were in the line
     */
    public MenuItem convertToMenuItem(String lineOfText)
    {
        // Regular expression below is from Pierre C on Stack Overflow:
        String[] fields = lineOfText.split(",");

        // Convert the string version of the price to a double.
        double price = new Double(fields[2]);
        return new MenuItem(fields[0], fields[1], price);
    }

}
