import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class MadLibs
{
	private ArrayList<String> tokens;
	private char placeholderStart;
	private char placeholderEnd;
	
	public MadLibs(String fileName) throws FileNotFoundException
	{
		FileReader reader = new FileReader(fileName);
		Scanner fileScanner = new Scanner(reader);
		
		// parse the line delineating the placeholder start and end symbols
		String placeholderLine = fileScanner.nextLine();
		this.placeholderStart = placeholderLine.charAt(0);
		this.placeholderEnd = placeholderLine.charAt(1);
		
		tokens = new ArrayList<String>();
		while(fileScanner.hasNext())
		{
			tokens.add(fileScanner.next());
		}
		fileScanner.close();
	}
	
	private boolean isPunctuation(char ch)
	{
		switch(ch)
		{
			case '.':
			case ',':
			case '!':
			case '?':
			case ';': 
			case '\"':
			case '\'': 
			case '*': return true;
			
			default: return false;
		}
	}
	
	private String trimPunctuation(String token)
	{
		int startIndex = 0;
		int endIndex = token.length() - 1;
		while(startIndex < token.length() && isPunctuation(token.charAt(startIndex)))
		{
			startIndex++;
		}
		while(endIndex >= 0 && isPunctuation(token.charAt(endIndex)))
		{
			endIndex--;
		}
		return token.substring(startIndex, endIndex + 1);
	}
	
	private boolean isPlaceholderStart(String token)
	{
		return token.indexOf(placeholderStart) != -1;
	}
	
	private boolean isPlaceholderEnd(String token)
	{
		return token.indexOf(placeholderEnd) != -1;
	}
	
	private String getPartOfSpeech(String placeholder)
	{
		String trimmedPlaceholder =  trimPunctuation(placeholder);
		return trimmedPlaceholder.substring(1, trimmedPlaceholder.length() - 1);
	}
	
	private String fillPlaceholder(Scanner inputScanner, String fullPlaceholder)
	{
		int placeholderStartIndex = fullPlaceholder.indexOf(placeholderStart);
		int placeholderEndIndex = fullPlaceholder.indexOf(placeholderEnd);
		
		String leadingChars = fullPlaceholder.substring(0, placeholderStartIndex);
		String trailingChars = fullPlaceholder.substring(placeholderEndIndex + 1);
		String partOfSpeech = fullPlaceholder.substring(placeholderStartIndex + 1, placeholderEndIndex);
				
		System.out.print("Give me a(n) " + partOfSpeech + ": ");
		String fill = inputScanner.nextLine();
		return leadingChars + fill + trailingChars;
	}
	
	public void playGame(Scanner inputScanner)
	{
		String output = "";
		for(int tokenIndex = 0; tokenIndex < tokens.size(); tokenIndex++)
		{
			String nextToken = tokens.get(tokenIndex);
			// every token but the first is preceded by a space
			if(tokenIndex > 0)
			{
				output += " ";
			}
			
			if(isPlaceholderStart(nextToken))
			{
				String fullPlaceholder = "";
				while(true)
				{
					String nextPlaceholderToken = tokens.get(tokenIndex);
					fullPlaceholder += nextPlaceholderToken;
					
					if(isPlaceholderEnd(nextPlaceholderToken))
					{
						break;
					}
					else
					{
						fullPlaceholder += " ";
					}
					tokenIndex++;
				}
				output += fillPlaceholder(inputScanner, fullPlaceholder);
			}
			else
			{
				output += nextToken;
			}
		}
		System.out.println(output);
	}
}