using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class WordListManager : MonoBehaviour
{
	private static WordListManager _instance;
	public static WordListManager shared { get{ return _instance; } }
	
	private Dictionary<int, Dictionary<string, bool> > _wordList = new Dictionary<int, Dictionary<string, bool> >();
	public Dictionary<char, int> _letterProbs = new Dictionary<char, int>();
	
	void Awake()
	{
		_instance = this;
		
		TextAsset wordListText = (TextAsset)Resources.Load("SOWPODS") as TextAsset;
		StringReader reader = new StringReader(wordListText.text);
		string line;
		
		int length = 0;
		while((line = reader.ReadLine()) != null)
		{
			length = line.Length;
			if(_wordList.ContainsKey(length) == false)
				_wordList[length] = new Dictionary<string, bool>();
			
			_wordList[length][line] = true;
		}
		
		_letterProbs['A'] = 55;
		_letterProbs['E'] = 55;
		_letterProbs['I'] = 55;
		_letterProbs['O'] = 55;
		_letterProbs['U'] = 55;
		_letterProbs['Y'] = 55;
		
		_letterProbs['B'] = 40;
		_letterProbs['C'] = 40;
		_letterProbs['D'] = 40;
		_letterProbs['F'] = 40;
		_letterProbs['G'] = 40;
		_letterProbs['H'] = 40;
		_letterProbs['J'] = 40;
		_letterProbs['K'] = 40;
		_letterProbs['L'] = 40;
		_letterProbs['M'] = 40;
		_letterProbs['N'] = 40;
		_letterProbs['P'] = 40;
		_letterProbs['R'] = 40;
		_letterProbs['S'] = 40;
		_letterProbs['T'] = 40;
		_letterProbs['V'] = 40;
		_letterProbs['W'] = 40;
		
		_letterProbs['Q'] = 5;
		_letterProbs['X'] = 5;
		_letterProbs['Z'] = 5;
	}
	
	public char GetRandomLetter()
	{
		int prob = Random.Range(0, GetProbabilitySum());	
		int totes = 0;
		foreach(KeyValuePair<char, int> pair in _letterProbs)
		{
			totes += pair.Value;
			if(prob < totes)
				return pair.Key;
		}
		return 'A';
	}
	
	int GetProbabilitySum()
	{
		int prob = 0;
		foreach(KeyValuePair<char, int> pair in _letterProbs)
		{
			prob += pair.Value;
		}
		return prob;
	}
	

	void Start()
	{
		
	}

	public bool ContainsWord(string word)
	{
		int len = word.Length;
		if(_wordList.ContainsKey(word.Length))
			return _wordList[len].ContainsKey(word);
		return false;
	}
}	
