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;

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

        public event EventHandler<MonthEventArgs> MonthDayRequested;

        public event EventHandler<MonthEventArgs> RemoveAllOtherSelectedDay;

        private void calendar_DisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            DateTime? selectedDate = this.calendar.SelectedDate;
            if (selectedDate != null)
            {
                DateTime newDay = selectedDate.Value;
                MonthData current = this.DataContext as MonthData;
                if (newDay.Month != current.Index && MonthDayRequested != null)
                {
                    this.calendar.DisplayDate = current.Day;
                    MonthDayRequested(this, new MonthEventArgs() { Month = new MonthData(newDay.Month) { Day = newDay.Date } });
                }
            }
            
        }

        private void calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            var items = e.AddedItems;
            if (items.Count > 0)
            {
                DateTime selectedDate = (DateTime)items[items.Count - 1];
                MonthData current = this.DataContext as MonthData;
                if (selectedDate.Month == current.Index && RemoveAllOtherSelectedDay != null)
                {
                    RemoveAllOtherSelectedDay(this, new MonthEventArgs() { Month = new MonthData(selectedDate.Month) { Day = selectedDate.Date } });
                }
            }
        }
    }
}
