package hangman;

import java.io.File;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {

	public static void main(String[] args) {
		String fileName = args[0];
		int wordLength = Integer.parseInt(args[1]);
		int guesses = Integer.parseInt(args[2]);
		EvilHangman hangman = new EvilHangman();
		File dic = new File(fileName);
		hangman.startGame(dic, wordLength);
		String key = "";
		for(int i = 0; i < wordLength; i++){
			key = key+"-";
		}
		Set<String> alreadyProcess = new TreeSet<String>();
		while(guesses > 0){
			String guessedLetters = "";
			for(String letter: alreadyProcess){
				guessedLetters = guessedLetters + " " + letter;
			}
			System.out.println("You have " + guesses + " left");
			System.out.println("Used letters:" + guessedLetters);
			System.out.println("Word: " + key); 
			System.out.print("Enter guess: ");
			Scanner scan = new Scanner(System.in);
			boolean keepGoing = true;
			char guess = 0;
			while(keepGoing){
				String input = scan.next().toLowerCase();
				if(!input.matches("[a-z]")){
					System.out.println("Invald input");
				}
				else if(alreadyProcess.contains(input)){
					System.out.println("You already used that letter");
				}
				else{
					keepGoing = false;
					alreadyProcess.add(input);
					guess = input.charAt(0);
				}
			}
			hangman.makeGuess(guess);
			key = hangman.getKey();
			int count = 0;
			for(int i = 0; i < key.length(); i++){
				if(key.charAt(i) == guess){
					count++;
				}
			}
			if(count == 0){
				guesses--;
				if(guesses == 0){
					System.out.println("You lose!");
					System.out.println("The word was: " + hangman.getWord());
				}
				else{
					System.out.println("Sorry, there is no " + guess + "\'s\n");
				}
			}
			else{
				if(!key.contains("-")){
					System.out.println("You Win!");
					System.out.println("The word was: " + key);
					guesses = 0;
				}
				else{
					System.out.println("Yes, there is " + count + " " + guess + "\n");
				}
			}
		}
	}
}
