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

namespace C1Extended_Demo
{
    public partial class CalendarBook : UserControl
    {
        public CalendarBook()
        {
            InitializeComponent();

            foreach (var month in MonthData.Months) 
            {
                var top = new CalendarBookTop();
                top.DataContext = month;
                top.MonthRequested += new EventHandler<MonthEventArgs>(MonthRequested);
                calendar2010.Items.Add(top);

                var bottom = new CalendarBookBottom();
                bottom.DataContext = month;
                bottom.MonthDayRequested += new EventHandler<MonthEventArgs>(MonthDayRequested);
                bottom.RemoveAllOtherSelectedDay += new EventHandler<MonthEventArgs>(RemoveAllOtherSelectedDay);
                calendar2010.Items.Add(bottom);
            }
        }

        void RemoveAllOtherSelectedDay(object sender, MonthEventArgs e)
        {
            var index = e.Month.Index * 2;
            for (int i = 2; i < calendar2010.Items.Count; i = i + 2)
            {
                if (i != index)
                {
                    ((CalendarBookBottom)calendar2010.Items[i]).calendar.SelectedDate = null;
                }
            }
        }

        bool isPreviousTimerStopped = true;
        void MonthDayRequested(object sender, MonthEventArgs e)
        {
            if (isPreviousTimerStopped)
            {
                isPreviousTimerStopped = false;
                var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.05) };
                timer.Tick += (s, e2) =>
                {
                    var index = e.Month.Index * 2;
                    if (calendar2010.CurrentPage == index || calendar2010.CurrentPage == index - 1)
                    {
                        timer.Stop();
                        isPreviousTimerStopped = true;
                        ((CalendarBookBottom)calendar2010.Items[index]).calendar.SelectedDate = e.Month.Day;
                        RemoveAllOtherSelectedDay(this, e);
                    }
                    else
                    {
                        calendar2010.TurnPage(index > calendar2010.CurrentPage);
                    }
                };
                timer.Start();
            }
        }

        void MonthRequested(object sender, MonthEventArgs e)
        {
            if (isPreviousTimerStopped)
            {
                isPreviousTimerStopped = false;
                var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.05) };
                timer.Tick += (s, e2) =>
                {
                    var index = e.Month.Index * 2;
                    if (calendar2010.CurrentPage == index || calendar2010.CurrentPage == index - 1)
                    {
                        timer.Stop();
                        isPreviousTimerStopped = true;
                    }
                    else
                    {
                        calendar2010.TurnPage(index > calendar2010.CurrentPage);
                    }
                };
                timer.Start();
            }
        }
    }

    public class MonthData 
    {
        public MonthData(int index) 
        {
            Day = new DateTime(2010, index, 1);
            Index = index;

            var text = Day.ToString("MMMM");
            Name = text[0].ToString().ToUpper() + text.Substring(1).ToLower();
        }

        public DateTime Day { get; set; }
        public string Name { get; set; }    
        public int Index { get; set; }

        public static List<MonthData> _months = new List<MonthData>();
        public static List<MonthData> Months 
        {
            get 
            {
                if (_months.Count == 0)
                {
                    for (int i = 1; i <= 12; i++)
                    {
                        _months.Add(new MonthData(i));
                    }
                }
                return _months;
            }
        }
    }
}
