using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight;
using System.Reflection;
using System.IO;
using C1.Silverlight.SpellChecker;

namespace SpellCheckerSample
{
    public partial class MultiLanguage : UserControl
    {
        // declare the C1SpellChecker
        C1SpellChecker _c1SpellChecker = new C1SpellChecker();

        public MultiLanguage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MultiLanguage_Loaded);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _c1SpellChecker.CheckControlAsync(_plainTextBox);
        }

        void MultiLanguage_Loaded(object sender, RoutedEventArgs e)
        {
            cmbLanguage.SelectionChanged += new SelectionChangedEventHandler(cmbLanguage_SelectionChanged);
            cmbLanguage.ItemTemplate = (DataTemplate)Resources["DictionaryTemplate"];
            cmbLanguage.ItemsSource = SpellDictionaryWrapper.GetLanguages();
            cmbLanguage.SelectedIndex = 0;

            // load sample text into text boxes
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SpellCheckerSample.test.txt"))
            using (var sr = new StreamReader(stream))
            {
                var text = sr.ReadToEnd();
                _plainTextBox.Text = text;
            }
       }

        void cmbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetDictionaryFromServer();
        }

        private void GetDictionaryFromServer()
        {
            var dctWrapper = (SpellDictionaryWrapper)cmbLanguage.SelectedItem;

            // load the dictionary from the server
            var dict = _c1SpellChecker.MainDictionary;
            dict.LoadAsync(dctWrapper.Address);
            dict.LoadCompleted += (s, e) => 
            {
                // dictionary loaded, enable buttons
                if (e.Error != null)
                {
                    WriteLine("Failed to load {0} dictionary: {1}", dctWrapper.Name, e.Error.Message);
                }
                else
                {
                    WriteLine("Loaded {0} dictionary ({1:n0} words).", dctWrapper.Name, _c1SpellChecker.MainDictionary.WordCount);
                }
            };
        }

        //------------------------------------------------------------
        #region ** utilities

        void WriteLine(string format, params object[] args)
        {
            WriteLine(string.Format(format, args));
        }
        void WriteLine(string text)
        {
            _outputTextBox.Text = text + "\r\n";
        }

        #endregion
    }

    #region SpellDictionaryWrapper

    public class SpellDictionaryWrapper
    {
        // You can download more dictionaries from
        // http://www.componentone.com/SuperProducts/SpellCheckerSilverlight/Dictionaries/
        public static List<SpellDictionaryWrapper> GetLanguages()
        {
            var list = new List<SpellDictionaryWrapper>();
            list.Add(new SpellDictionaryWrapper() { Address = "C1Spell_de-DE.dct", Name = "German (Germany)", FlagUri = new Uri("Flags/ger.jpg", UriKind.Relative) });
            list.Add(new SpellDictionaryWrapper() { Address = "C1Spell_en-US.dct", Name = "English (USA)", FlagUri = new Uri("Flags/uni.jpg", UriKind.Relative) });
            list.Add(new SpellDictionaryWrapper() { Address = "C1Spell_es-ES.dct", Name = "Spanish (Spain)", FlagUri = new Uri("Flags/spa.jpg", UriKind.Relative) });
            list.Add(new SpellDictionaryWrapper() { Address = "C1Spell_fr-CA.dct", Name = "French (Canada)", FlagUri = new Uri("Flags/can.jpg", UriKind.Relative) });
            list.Add(new SpellDictionaryWrapper() { Address = "C1Spell_it-IT.dct", Name = "Italian (Italy)", FlagUri = new Uri("Flags/ita.jpg", UriKind.Relative) });
            list.Add(new SpellDictionaryWrapper() { Address = "C1Spell_ru-RU.dct", Name = "Russian (Russia)", FlagUri = new Uri("Flags/rus.jpg", UriKind.Relative) });
            return list;
        }

        public string Address { get; set; }
        public string Name { get; set; }
        public Uri FlagUri { get; set; }
    }

    #endregion
}
