using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Core;

namespace LetterWords
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private DictionarySearcher _dictionarySearcher;
		private WordFrequency _wordFrequency;

		public MainWindow()
		{
			InitializeComponent();
			_dictionarySearcher = new DictionarySearcher();
			_wordFrequency = new WordFrequency();
		}

		private void button1_Click(object sender, RoutedEventArgs e)
		{
			var text = textBox1.Text;
			var foundWords = _dictionarySearcher.FindWords(text);
			var scoredWords = JangleWordScorer.GroupByScore(foundWords);
			textBlock1.Text = string.Empty;
			foreach (var s in scoredWords)
			{
				var words = from w in s.Value
				            let wordFrequency = _wordFrequency.GetWordFreq(w.ToLower())
				            orderby wordFrequency descending
				            select w;

				textBlock1.Text += (s.Key + " " + string.Join(" ", words));
				textBlock1.Text += Environment.NewLine;
			}
		}

		private void textBox1_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.Key != Key.Enter) return;

			// your event handler here
			e.Handled = true;
			button1_Click(sender, null);
		}
	}
}
