package edu.caltech.cs2.project02.choosers;

import edu.caltech.cs2.project02.interfaces.IHangmanChooser;
//Added Line 5
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class RandomHangmanChooser implements IHangmanChooser {
  private static final Random random = new Random();
  private static String DICTIONARY_FILE = "data/scrabble.txt";
  private static String chosen = ""; //How do I do this with final?
  private static int wordLength;
  private static int maxGuesses;
  private static int remainingGuesses;
  private static SortedSet<Character> lettersGuessed = new TreeSet<>();
  public RandomHangmanChooser(int wordLength, int maxGuesses) throws FileNotFoundException {
    this.wordLength = wordLength;
    this.maxGuesses = maxGuesses;
    this.remainingGuesses = maxGuesses;

    if(wordLength < 1 || maxGuesses < 1){
      throw new IllegalArgumentException();
    }

    SortedSet<String> dictionary = new TreeSet<>();
    Scanner input = new Scanner(new File(DICTIONARY_FILE));
    while (input.hasNext()){
      String next = input.next();
      if (next.length() == wordLength){
        dictionary.add(next);
      }
    }
    if(dictionary.size() == 0){
      throw new IllegalStateException();
    }

    String wordArray[] = new String[dictionary.size()];
    wordArray = dictionary.toArray(wordArray);

    int RAND = random.nextInt();
    int idx = 0;
    for(int i = 0; i < RAND; i++){
      idx = i % dictionary.size();
    }
    this.chosen = wordArray[idx];
  }
  private static final String CHOSEN = chosen;

  @Override
  public int makeGuess(char letter) {
    if (!Character.isLowerCase(letter)) {
      throw new IllegalArgumentException("Letter should be lower-cased.");
    }
    if(this.remainingGuesses < 1){
      throw new IllegalStateException("No guesses left.");
    }
    if(this.lettersGuessed.contains(letter)){
      throw new IllegalArgumentException("This letter was guessed previously");
    }
    this.lettersGuessed.add(letter);
    int occurs = 0;
    String word = this.CHOSEN;
    for(int i = 0; i < word.length(); i++){
      if(word.charAt(i) == letter){
        occurs++;
      }
    }
    this.remainingGuesses = this.remainingGuesses - 1;

    return occurs;
  }

  @Override
  public boolean isGameOver() {
    boolean gameOver = false;

    boolean guessedWord = true;
    for(int i = 0; i < this.CHOSEN.length(); i++){
      Character checkLetter = this.CHOSEN.charAt(i);
      if (!this.lettersGuessed.contains(checkLetter)){
        guessedWord = false;
        break;
      }
    }

    if(guessedWord || remainingGuesses < 1){
      gameOver = true;
    }
    return gameOver;
  }

  @Override
  public String getPattern() {
    String pattern = "";
    for(int i = 0; i < this.CHOSEN.length(); i++){
      Character checkLetter = this.CHOSEN.charAt(i);
      if (this.lettersGuessed.contains(checkLetter)){
        pattern+=checkLetter;
      }
      else{
        pattern+="-";
      }
    }
    return pattern;
  }

  @Override
  public SortedSet<Character> getGuesses() {
    return this.lettersGuessed;
  }

  @Override
  public int getGuessesRemaining() {
    return this.remainingGuesses;
  }

  @Override
  public String getWord() {
    this.remainingGuesses = 0;
    return this.CHOSEN;
  }
}