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 C1.Silverlight.Theming;
using C1.Silverlight;

namespace Theming
{
    public partial class MainPage : UserControl
    {
        List<C1Theme> _themes = new List<C1Theme>();

        public MainPage()
        {
            _themes.Add(new C1.Silverlight.Theming.Cosmopolitan.C1ThemeCosmopolitan());
            _themes.Add(new C1.Silverlight.Theming.CosmopolitanDark.C1ThemeCosmopolitanDark());
            _themes.Add(new C1.Silverlight.Theming.WhistlerBlue.C1ThemeWhistlerBlue());
            _themes.Add(new C1.Silverlight.Theming.RainierOrange.C1ThemeRainierOrange());
            _themes.Add(new C1.Silverlight.Theming.BureauBlack.C1ThemeBureauBlack());
            _themes.Add(new C1.Silverlight.Theming.ExpressionDark.C1ThemeExpressionDark());
            _themes.Add(new C1.Silverlight.Theming.ExpressionLight.C1ThemeExpressionLight());
            _themes.Add(new C1.Silverlight.Theming.ShinyBlue.C1ThemeShinyBlue());
            _themes.Add(new C1.Silverlight.Theming.Office2010.C1ThemeOffice2010Blue());
            _themes.Add(new C1.Silverlight.Theming.Office2010.C1ThemeOffice2010Black());
            _themes.Add(new C1.Silverlight.Theming.Office2010.C1ThemeOffice2010Silver());
            _themes.Add(new C1.Silverlight.Theming.Office2007.C1ThemeOffice2007Blue());
            _themes.Add(new C1.Silverlight.Theming.Office2007.C1ThemeOffice2007Black());
            _themes.Add(new C1.Silverlight.Theming.Office2007.C1ThemeOffice2007Silver());
            _themes.Add(new C1.Silverlight.Theming.Office2013.C1ThemeOffice2013White());
            _themes.Add(new C1.Silverlight.Theming.Office2013.C1ThemeOffice2013LightGray());
            _themes.Add(new C1.Silverlight.Theming.Office2013.C1ThemeOffice2013DarkGray());

            InitializeComponent();

            // the next line is required in order to apply themes to all C1Window instances 
            C1Window.AddDefaultPopupToVisualTree(LayoutRoot);
            List<string> themes = new List<string>();
            themes.Add("Cosmopolitan");
            themes.Add("Cosmopolitan Dark");
            themes.Add("Whistler Blue");
            themes.Add("Rainier Orange");
            themes.Add("Bureau Black");
            themes.Add("Expression Dark");
            themes.Add("Expression Light");
            themes.Add("Shiny Blue");
            themes.Add("Office 2010 Blue");
            themes.Add("Office 2010 Black");
            themes.Add("Office 2010 Silver");
            themes.Add("Office 2007 Blue");
            themes.Add("Office 2007 Black");
            themes.Add("Office 2007 Silver");
            themes.Add("Office 2013 White");
            themes.Add("Office 2013 Light Gray");
            themes.Add("Office 2013 Dark Gray");
            ThemesCombo.ItemsSource = themes;
            ThemesCombo.SelectedIndex = 0;
            C1Theme theme = _themes[0];
            theme.Apply(this);
            sched1.Theme = C1Theme.GetCurrentThemeResources(theme);

            Appointment app = sched1.DataStorage.AppointmentStorage.Appointments.Add(DateTime.Today, DateTime.Today.AddDays(1), "test all-day event");
            app.Categories.Add(sched1.DataStorage.CategoryStorage.Categories[0]);
            app.Label = sched1.DataStorage.LabelStorage.Labels[0];
            
            app = sched1.DataStorage.AppointmentStorage.Appointments.Add(DateTime.Now, TimeSpan.FromMinutes(30), "test appointment");
            app.Categories.Add(sched1.DataStorage.CategoryStorage.Categories[1]);
            app.Label = sched1.DataStorage.LabelStorage.Labels[1];
      }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // update toolbar buttons state
            sched1_StyleChanged(null, null);
            ThemesCombo.SelectionChanged += new SelectionChangedEventHandler(ThemesCombo_SelectionChanged); 
        }
        
        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.TimeLineStyle)
            {
                btnTimeLine.IsChecked = true;
            }
            else
            {
                btnDay.IsChecked = true;
            }
        }

        private void ThemesCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            C1Theme theme = _themes[ThemesCombo.SelectedIndex];
            Dispatcher.BeginInvoke(() =>
                {
                    // remove focus from ComboBox to don't break ComboBox theme
                    sched1.Focus();
                    theme.Apply(this);
                    sched1.Theme = C1Theme.GetCurrentThemeResources(theme);
                });
       }

        private void Grouping_Checked(object sender, RoutedEventArgs e)
        {
            sched1.GroupBy = "Category";
        }

        private void Grouping_Unchecked(object sender, RoutedEventArgs e)
        {
            sched1.GroupBy = "";
        }
    }
}
