Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Threading
Imports System.Globalization
Imports C1.C1Schedule

Namespace Navigation
	Partial Public Class MainPage
		Inherits UserControl
		Implements IDisposable
		#Region "** fields"
		Private _isLoaded As Boolean = False
#End Region

#Region "** initializing"
        Public Sub New()
			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." & vbLf & "Select full week for switching to the Week View." & vbLf & "Select several full weeks for switching to the Month View.")

			' add some appointments
			Dim app As New Appointment(Date.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(Date.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(Date.Today.AddDays(-3).AddHours(11), TimeSpan.FromMinutes(60))
			app.Subject = "Silverlight lesson"
			app.Location = "Conference room"
			app.Label = sched1.DataStorage.LabelStorage.Labels(8)
			Dim pattern As RecurrencePattern = app.GetRecurrencePattern()
			pattern.RecurrenceType = RecurrenceTypeEnum.Weekly
			pattern.DayOfWeekMask = WeekDaysEnum.Monday
			pattern.Occurrences = 5
			sched1.DataStorage.AppointmentStorage.Appointments.Add(app)

            AddHandler sched1.StyleChanged, AddressOf sched1_StyleChanged
        End Sub

		Private Sub sched1_StyleChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
			' update time scale
			If _isLoaded Then
                If sched1.Style Is sched1.MonthStyle Then
                    cmbTimeScale.IsEnabled = False
                Else
                    cmbTimeScale.IsEnabled = True
                End If

                ' update toolbar buttons state according to the current C1Scheduler view
                If sched1.Style Is sched1.WorkingWeekStyle Then
                    btnWorkWeek.IsChecked = True
                ElseIf sched1.Style Is sched1.WeekStyle Then
                    btnWeek.IsChecked = True
                ElseIf sched1.Style Is sched1.MonthStyle Then
                    btnMonth.IsChecked = True
                ElseIf sched1.Style Is sched1.TimeLineStyle Then
                    btnTimeLine.IsChecked = True
                Else
                    btnDay.IsChecked = True
                End If
			End If
		End Sub

        Private Sub UserControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            _isLoaded = True
            ' subscribe on VisibleDates changes
            AddHandler sched1.VisibleDates.CollectionChanged, AddressOf VisibleDates_CollectionChanged
        End Sub
#End Region

#Region "** navigation"
        ' when user clicks week tab or day header, or when user scrolls scheduler in a MonthView mode,
        ' update curent calendar selection
        Private Sub VisibleDates_CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
			UpdateText()
		End Sub

		' switch to the current date
		Private Sub btnToday_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			sched1.BeginUpdate()
			sched1.ChangeStyle(sched1.OneDayStyle)
			sched1.VisualStartTime = Date.Today
			cmbTimeScale.IsEnabled = True
            sched1.EndUpdate()
        End Sub

		' navigate the current view back
		Private Sub btnBack_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim newStart As Date = sched1.VisibleDates(0)
			Dim count As Integer = sched1.VisibleDates.Count
            If sched1.Style Is sched1.OneDayStyle OrElse sched1.Style Is sched1.TimeLineStyle Then
                newStart = newStart.AddDays(-count)
            ElseIf sched1.Style Is sched1.MonthStyle Then
                newStart = newStart.AddDays(-count + 7)
            Else
                ' Week and WorkingWeek styles
                newStart = newStart.AddDays(-7)

            End If
			sched1.VisualStartTime = newStart
		End Sub

		' navigate the current view forward
		Private Sub btnForward_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim newStart As Date = sched1.VisibleDates(0)
			Dim count As Integer = sched1.VisibleDates.Count
            If sched1.Style Is sched1.OneDayStyle OrElse sched1.Style Is sched1.TimeLineStyle Then
                newStart = newStart.AddDays(count)
            ElseIf sched1.Style Is sched1.MonthStyle Then
                newStart = newStart.AddDays(count - 7)
            Else
                ' Week and WorkingWeek styles
                newStart = newStart.AddDays(7)

            End If
			sched1.VisualStartTime = newStart
		End Sub
		#End Region

		#Region "** helpers"
		' update date range description
		Private Sub UpdateText()
			Dim start As Date = sched1.VisibleDates(0)
			Dim [end] As Date = sched1.VisibleDates(sched1.VisibleDates.Count - 1)
			Dim text As String
            If start = [end] Then
                text = start.ToString("MMMM d, yyy", Thread.CurrentThread.CurrentCulture)
            ElseIf start.Year = [end].Year Then
                If start.Month = [end].Month Then
                    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)
                End If
            Else
                text = start.ToString("MMMM d, yyy", Thread.CurrentThread.CurrentCulture) & " - " & [end].ToString("MMMM d, yyy", Thread.CurrentThread.CurrentCulture)
            End If
			txtRange.Text = text
		End Sub
#End Region

#Region "IDisposable Members"

        Public Sub Dispose() Implements IDisposable.Dispose
			sched1.Dispose()
		End Sub

		#End Region

	End Class
End Namespace
