package edu.caltech.cs2.project02.choosers;

import edu.caltech.cs2.project02.interfaces.IHangmanChooser;

import java.io.FileNotFoundException;
import java.util.*;

public class RandomHangmanChooser implements IHangmanChooser {
    private static final Random RANDOM = new Random();
    public int guesses;
    public String targetword;
    public SortedSet<Character> guessedletters = new TreeSet<>();

    public RandomHangmanChooser(int wordLength, int maxGuesses) throws FileNotFoundException {
        if (wordLength < 1 || maxGuesses < 1) {
            throw new IllegalArgumentException("wordLength and maxGuesses need to be >= 1");
        }
        Scanner sc = new Scanner("data/scrabble.txt");
        int count = 0;
        int largeststring = 0;
        SortedSet<String> ts = new TreeSet<String>();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            ts.add(line);
            if (line.length() > largeststring) {
                largeststring = line.length();
            }
            count += 1;
        }
        if (wordLength > largeststring) {
            throw new IllegalStateException("wordLength largest than largest string");
        }
        boolean state = false;
        String currline = "";
        while (!state) {
            Iterator<String> i = ts.iterator();
            int x = RANDOM.nextInt(count);
            int linenum = 0;
            currline = "";
            while (x != linenum) {
                currline = i.next();
                linenum += 1;
            }
            if (currline.length() == wordLength) {
                state = true;
            }
        }
        this.guesses = maxGuesses;
        this.targetword = currline;
    }

  @Override
  public int makeGuess(char letter) {
      if (guesses < 1) {
          throw new IllegalStateException("Out of guesses");
      }
      if (guessedletters.contains(letter)) {
          throw new IllegalArgumentException("Letter guessed previously");
      }
      int count = 0;
      for (int i = 0; i < targetword.length(); i++) {
          if (targetword.charAt(i) == letter) {
              count++;
          }
      }
      this.guesses =- 1;
      this.guessedletters.add(letter);
      return count;
  }

  @Override
  public boolean isGameOver() {
    if (this.guesses == 0 ) {
        return true;
    }
    for (int i=0; i<targetword.length(); i++) {
        if (!guessedletters.contains(targetword.charAt(i))) {
            return false;
        }
    }
    return true;
  }

  @Override
  public String getPattern() {
    String guessed = "";
    for (int i=0; i<targetword.length(); i++) {
        if (guessedletters.contains(targetword.charAt(i))) {
            guessed += targetword.charAt(i);
        }
        else{
                guessed += "-";
            }
        }
    return guessed;
    }

  @Override
  public SortedSet<Character> getGuesses() {
    return this.guessedletters;
  }

  @Override
  public int getGuessesRemaining() {
    return this.guesses;
  }

  @Override
  public String getWord() {
    return this.targetword;
  }
}