using System.Collections.Generic;
using System.Globalization;
using System.IO.IsolatedStorage;
using System.Threading;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;

namespace SamplesCommon
{
    public class LocalizationComboBox : ComboBox
    {
        private string[] cultures = { "", "ar", "da", "de", "es", "fi", "fr", "he", "it", "ja", "nl", "no", "pt", "ro", "ru", "en", "sv", "cs", "el", "pl", "tr", "sk", "zh", "zh-Hans", "zh-Hant" };

        public LocalizationComboBox()
            : base()
        {
            // get cultures
            CultureInfo[] availableNeutralLanguages = GetCultures();
            ItemsSource = availableNeutralLanguages;
            SelectedItem = availableNeutralLanguages[0];
            foreach (var neutralLanguage in availableNeutralLanguages)
            {
                if (neutralLanguage.Name == Thread.CurrentThread.CurrentUICulture.Name)
                {
                    SelectedItem = neutralLanguage;
                    break;
                }
            }
            DisplayMemberPath = "DisplayName";
            SelectionChanged += new SelectionChangedEventHandler(LocalizationComboBox_SelectionChanged);
        }

        private CultureInfo[] GetCultures()
        {
            var result = new List<CultureInfo>();
            foreach (var cultureName in cultures)
            {
                try
                {
                    var culture = new CultureInfo(cultureName);
                    result.Add(culture);
                }
                catch
                {
                    // some cultures might be unsupported, in such case it will throw exception
                }
            }
            return result.ToArray();
        }

        private void LocalizationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = e.AddedItems[0] as CultureInfo;
            Application.Current.SaveCulture();
            Application.Current.Refresh();
        }
    }

    public static class ApplicationExtensions
    {
        public static void Refresh(this Application app)
        {
            ((ScriptObject)HtmlPage.Window.GetProperty("location")).Invoke("reload");
        }

        public static void LoadCulture(this Application app)
        {
            try
            {
                if (!IsolatedStorageSettings.ApplicationSettings.Contains("language"))
                {
                    app.SaveCulture();
                }
                UpdateAppCulture(IsolatedStorageSettings.ApplicationSettings["language"] as string);
            }
            catch
            {
                MessageBox.Show("Please, open Silverlight settings and enable Application Storage.");
            }
        }

        private static void UpdateAppCulture(string language)
        {
            if (! string.IsNullOrEmpty(language))
            {
                Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            }
        }

        public static void SaveCulture(this Application app)
        {
            try
            {
                IsolatedStorageSettings.ApplicationSettings["language"] = Thread.CurrentThread.CurrentUICulture.Name;
            }
            catch
            {
                MessageBox.Show("Please, open Silverlight settings and enable Application Storage.");
            }
        }
    }
}
