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

namespace Navigation
{
    public partial class MainPage : UserControl, IDisposable
    {
        #region ** fields
        bool _isLoaded = false;
        #endregion

        #region ** initializing
        public MainPage()
        {
            InitializeComponent();

            // initialize time scale combo
            cmbTimeScale.Items.Add(TimeSpan.FromMinutes(10));
            cmbTimeScale.Items.Add(TimeSpan.FromMinutes(15));
            cmbTimeScale.Items.Add(TimeSpan.FromMinutes(20));
            cmbTimeScale.Items.Add(TimeSpan.FromMinutes(30));
            cmbTimeScale.Items.Add(TimeSpan.FromMinutes(60));
            cmbTimeScale.Items.Add(TimeSpan.FromHours(2));
            cmbTimeScale.SelectedIndex = 3;

            // set calendar tooltip
            calendar1.SetValue(ToolTipService.ToolTipProperty,
                "Use calendar for navigation.\nSelect full week for switching to the Week View.\nSelect several full weeks for switching to the Month View.");
            
            // add some appointments
            Appointment app = new Appointment(DateTime.Today.AddDays(-1), TimeSpan.FromDays(1));
            app.AllDayEvent = true;
            app.Private = true;
            app.Subject = "Test All-Day Event";
            app.Label = sched1.DataStorage.LabelStorage.Labels[8];
            app.Location = "Room 15";
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);
            app = new Appointment(DateTime.Today, TimeSpan.FromDays(1));
            app.AllDayEvent = true;
            app.BusyStatus = sched1.DataStorage.StatusStorage.Statuses[StatusTypeEnum.Tentative];
            app.Subject = "Another test All-Day Event";
            app.Label = sched1.DataStorage.LabelStorage.Labels[7];
            app.Location = "Room 22";
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);
            app = new Appointment();
            app.Duration = TimeSpan.FromMinutes(60);
            app.Subject = "Test Appointment";
            app.Location = "Room 327";
            app.Label = sched1.DataStorage.LabelStorage.Labels[5];
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);
            app = new Appointment();
            app.Duration = TimeSpan.FromMinutes(20);
            app.Subject = "Another test Appointment";
            app.Location = "Room 327";
            app.BusyStatus = sched1.DataStorage.StatusStorage.Statuses[StatusTypeEnum.OutOfOffice];
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);
            app = new Appointment(DateTime.Today.AddDays(-3).AddHours(11), TimeSpan.FromMinutes(60));
            app.Subject = "Silverlight lesson";
            app.Location = "Conference room";
            app.Label = sched1.DataStorage.LabelStorage.Labels[8];
            RecurrencePattern pattern = app.GetRecurrencePattern();
            pattern.RecurrenceType = RecurrenceTypeEnum.Weekly;
            pattern.DayOfWeekMask = WeekDaysEnum.Monday;
            pattern.Occurrences = 5;
            sched1.DataStorage.AppointmentStorage.Appointments.Add(app);

            sched1.StyleChanged += new EventHandler<RoutedEventArgs>(sched1_StyleChanged);
        }

        void sched1_StyleChanged(object sender, RoutedEventArgs e)
        {
            // update UI
            if (_isLoaded)
            {
                cmbTimeScale.IsEnabled = sched1.Style != sched1.MonthStyle;
                
                // 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 UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _isLoaded = true;
            // subscribe on VisibleDates changes
            sched1.VisibleDates.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(VisibleDates_CollectionChanged);
        }
        #endregion

        #region ** navigation
        // when user clicks week tab or day header, or when user scrolls scheduler in a MonthView mode,
        // update curent calendar selection
        void VisibleDates_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UpdateText();
        }

        // switch to the current date
        private void btnToday_Click(object sender, RoutedEventArgs e)
        {
            sched1.BeginUpdate();
            sched1.ChangeStyle(sched1.OneDayStyle);
            sched1.VisualStartTime = DateTime.Today;
            sched1.EndUpdate();
        }

        // navigate the current view back
        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            DateTime newStart = sched1.VisibleDates[0];
            int count = sched1.VisibleDates.Count;
            if (sched1.Style == sched1.OneDayStyle || sched1.Style == sched1.TimeLineStyle)
            {
                newStart = newStart.AddDays(-count);
            }
            else if (sched1.Style == sched1.MonthStyle)
            {
                newStart = newStart.AddDays(-count + 7);
            }
            else
            {
                // Week and WorkingWeek styles
                newStart = newStart.AddDays(-7); 
            }
            sched1.VisualStartTime = newStart;
        }

        // navigate the current view forward
        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            DateTime newStart = sched1.VisibleDates[0];
            int count = sched1.VisibleDates.Count;
            if (sched1.Style == sched1.OneDayStyle || sched1.Style == sched1.TimeLineStyle)
            {
                newStart = newStart.AddDays(count);
            }
            else if (sched1.Style == sched1.MonthStyle)
            {
                newStart = newStart.AddDays(count - 7);
            }
            else
            {
                // Week and WorkingWeek styles
                newStart = newStart.AddDays(7); ;
            }
            sched1.VisualStartTime = newStart;
        }
        #endregion

        #region ** helpers
        // update date range description
        private void UpdateText()
        {
            DateTime start = sched1.VisibleDates[0];
            DateTime end = sched1.VisibleDates[sched1.VisibleDates.Count - 1];
            string text;
            if (start == end)
            {
                text = start.ToString("MMMM d, yyy", Thread.CurrentThread.CurrentCulture);
            }
            else if (start.Year == end.Year)
            {
                if (start.Month == end.Month)
                {
                    text = start.ToString("MMMM d", Thread.CurrentThread.CurrentCulture)
                        + " - " + end.ToString("d, yyy", Thread.CurrentThread.CurrentCulture);
                }
                else
                {
                    text = start.ToString("MMMM d", Thread.CurrentThread.CurrentCulture)
                        + " - " + end.ToString("MMMM d, yyy", Thread.CurrentThread.CurrentCulture);
                }
            }
            else
            {
                text = start.ToString("MMMM d, yyy", Thread.CurrentThread.CurrentCulture)
                    + " - " + end.ToString("MMMM d, yyy", Thread.CurrentThread.CurrentCulture);
            }
            txtRange.Text = text;
        }
        #endregion

        #region IDisposable Members

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