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.C1Schedule;

namespace Grouping
{
	public partial class MainPage : UserControl, IDisposable
	{
		public MainPage()
		{
			InitializeComponent();
			cmbGroup.Items.Add("None");
			cmbGroup.Items.Add("Category");
			cmbGroup.Items.Add("Resource");
			cmbGroup.Items.Add("Contact");
			cmbGroup.SelectedIndex = 3;

            cmbView.Items.Add("Day");
            cmbView.Items.Add("Working Week");
            cmbView.Items.Add("Week");
            cmbView.Items.Add("Month");
            cmbView.Items.Add("Time Line");
            cmbView.SelectedIndex = 4;

            // add some resources
			Resource res = new Resource();
			res.Text = "Meeting room";
			res.Color = Color.FromArgb(255, 218, 186, 198);
			sched1.DataStorage.ResourceStorage.Resources.Add(res);
			Resource res1 = new Resource();
			res1.Text = "Conference hall";
			res1.Color = Color.FromArgb(255, 220, 236, 201);
			sched1.DataStorage.ResourceStorage.Resources.Add(res1);

            // add some contacts
            Contact cnt = new Contact();
            cnt.Text = "Andy Garcia";
            sched1.DataStorage.ContactStorage.Contacts.Add(cnt);
            Contact cnt1 = new Contact();
            cnt1.Text = "Nancy Drew";
            sched1.DataStorage.ContactStorage.Contacts.Add(cnt1);
            Contact cnt2 = new Contact();
            cnt2.Text = "Robert Clark";
            sched1.DataStorage.ContactStorage.Contacts.Add(cnt2);

            // add sample appointments
            Appointment app = new Appointment(DateTime.Today.AddHours(12), TimeSpan.FromHours(1));
            app.Subject = "Sales meeting";
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);
            app.Resources.Add(res);
            app.Links.Add(cnt1);
            app.Links.Add(cnt2);

            app = new Appointment(DateTime.Today.AddHours(14), TimeSpan.FromHours(3));
            app.Subject = "Retirement Planning Session";
            app.Body = "A retirement planning education session. Please attend if possible.";
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);
            app.Resources.Add(res1);
            app.Links.Add(cnt1);
            app.Links.Add(cnt2);
            app.Links.Add(cnt);

            app = new Appointment(DateTime.Today.AddHours(10), TimeSpan.FromMinutes(15));
            app.Subject = "Conference call";
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);
            app.Links.Add(cnt);

			sched1.VisibleGroupItems.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(VisibleGroupItems_CollectionChanged);
		}

		void VisibleGroupItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			C1.Silverlight.CommandManager.InvalidateRequerySuggested();
		}

		private void ComboBoxGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			string str = (string)cmbGroup.SelectedItem;
			if (str == "None")
			{
				sched1.GroupBy = string.Empty;
			}
			else
			{
				sched1.GroupBy = str;
			}
		}

        private void ComboBoxView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch ((string)cmbView.SelectedItem)
            {
                case "Month":
                    sched1.ChangeStyle(sched1.MonthStyle);
                    break;
                case "Day":
                    sched1.ChangeStyle(sched1.OneDayStyle);
                    break;
                case "Working Week":
                    sched1.ChangeStyle(sched1.WorkingWeekStyle);
                    break;
                case "Week":
                    sched1.ChangeStyle(sched1.WeekStyle);
                    break;
                case "Time Line":
                    sched1.ChangeStyle(sched1.TimeLineStyle);
                    break;
            }
        }

        private void sched1_StyleChanged(object sender, RoutedEventArgs e)
        {
            // update toolbar buttons state according to the current C1Scheduler view
            if (sched1.Style == sched1.WorkingWeekStyle)
            {
                cmbView.SelectedIndex = 1;
            }
            else if (sched1.Style == sched1.WeekStyle)
            {
                cmbView.SelectedIndex = 2;
            }
            else if (sched1.Style == sched1.MonthStyle)
            {
                cmbView.SelectedIndex = 3;
            }
            else if (sched1.Style == sched1.TimeLineStyle)
            {
                cmbView.SelectedIndex = 4;
            }
            else
            {
                cmbView.SelectedIndex = 0;
            }
        }


		#region IDisposable Members

		public void Dispose()
		{
			sched1.Dispose();
		}

		#endregion

	}
}
