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.Schedule;
using C1.C1Schedule;
using System.Threading;
using System.Globalization;
using C1.Silverlight;

namespace Localization
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            // adding default popup to the visual tree propagates all changes to the Language property to dialog windows
            C1Window.AddDefaultPopupToVisualTree(LayoutRoot);
            cmbCultures.Items.Add("default");
            cmbCultures.Items.Add("ar-SA");
            cmbCultures.Items.Add("cs-CZ");
            cmbCultures.Items.Add("da-DK");
            cmbCultures.Items.Add("de-DE");
            cmbCultures.Items.Add("el-GR");
            cmbCultures.Items.Add("en-US");
            cmbCultures.Items.Add("es-ES");
            cmbCultures.Items.Add("fi-FI");
            cmbCultures.Items.Add("fr-FR");
            cmbCultures.Items.Add("he-IL");
            cmbCultures.Items.Add("it-IT");
            cmbCultures.Items.Add("ja-JP");
            cmbCultures.Items.Add("nl-NL");
            cmbCultures.Items.Add("nb-NO");
            cmbCultures.Items.Add("pl-PL");
            cmbCultures.Items.Add("pt-BR");
            cmbCultures.Items.Add("ro-RO");
            cmbCultures.Items.Add("ru-RU");
            cmbCultures.Items.Add("sk-SK");
            cmbCultures.Items.Add("sv-SE");
            cmbCultures.Items.Add("tr-TR");
            cmbCultures.Items.Add("zh");
            cmbCultures.Items.Add("zh-Hans");
            cmbCultures.Items.Add("zh-Hant");
            CultureInfo currCulture = CultureInfo.CurrentUICulture;
            object o = cmbCultures.Items.Where(x => 
                (!((string)x).Equals("default") &&
                ((currCulture.TwoLetterISOLanguageName == "no" && ((string)x).Equals("nb-NO")) || ((string)x).StartsWith(currCulture.TwoLetterISOLanguageName)))).FirstOrDefault();
            if (o != null)
            {
                cmbCultures.SelectedItem = o;
            }
            else
            {
                cmbCultures.SelectedIndex = 0;
            }

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // update toolbar buttons state
            sched1_StyleChanged(null, null);
        }
        
        private void btnToday_Click(object sender, RoutedEventArgs e)
        {
            sched1.VisibleDates.BeginUpdate();
            sched1.VisibleDates.Clear();
            sched1.VisibleDates.Add(DateTime.Today);
            sched1.VisibleDates.EndUpdate();
        }

        private void sched1_StyleChanged(object sender, RoutedEventArgs e)
        {
            // update toolbar buttons state according to the current C1Scheduler view
            if (sched1.Style == sched1.WorkingWeekStyle)
            {
                btnWorkWeek.IsChecked = true;
            }
            else if (sched1.Style == sched1.WeekStyle)
            {
                btnWeek.IsChecked = true;
            }
            else if (sched1.Style == sched1.MonthStyle)
            {
                btnMonth.IsChecked = true;
            }
            else if (sched1.Style == sched1.OneDayStyle)
            {
                btnDay.IsChecked = true;
            }
            else
            {
                btnTimeLine.IsChecked = true;
            }
        }

        private void cmbCultures_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string cultureName = cmbCultures.SelectedIndex == 0 ? "en" : (string)cmbCultures.SelectedItem;
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
            Language = System.Windows.Markup.XmlLanguage.GetLanguage(cultureName);
        }
    }
}
