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 C1.Silverlight.Schedule
Imports C1.C1Schedule
Imports C1.Silverlight
Imports System.Windows.Data
Imports System.Globalization

Namespace CustomGroupingView
	Partial Public Class MainPage
		Inherits UserControl
		Implements IDisposable
		Public Sub New()
			System.Threading.Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US")
			System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture
			InitializeComponent()
			' add some contacts
			Dim cnt As New Contact()
			cnt.Text = "Andy Garcia"
			sched1.DataStorage.ContactStorage.Contacts.Add(cnt)
			cnt = New Contact()
			cnt.Text = "Nancy Drew"
			sched1.DataStorage.ContactStorage.Contacts.Add(cnt)
			cnt = New Contact()
			cnt.Text = "Robert Clark"
			sched1.DataStorage.ContactStorage.Contacts.Add(cnt)
			cnt = New Contact()
			cnt.Text = "James Doe"
			sched1.DataStorage.ContactStorage.Contacts.Add(cnt)
			sched1.GroupBy = "Contact"
			AddHandler sched1.VisibleGroupItems.CollectionChanged, AddressOf VisibleGroupItems_CollectionChanged
		End Sub

		Private Sub VisibleGroupItems_CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
			' update CanExecute state for buttons
			C1.Silverlight.CommandManager.InvalidateRequerySuggested()
		End Sub

		#Region "IDisposable Members"

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

		#End Region

	End Class

	''' <summary>
	''' Represents a Binding converter that returns a <see cref="Boolean"/> value 
	''' indicating whether a source value is today's date.
	''' </summary>
	''' <remarks>
	''' <see cref="IsTodayConverter"/> is a one-way converter. 
	''' </remarks>
	Public Class IsTodayVisibilityConverter
		Implements IValueConverter
		''' <summary>
		''' Converts a value. The data binding engine calls this method 
		''' when it propagates a value from the binding source to the binding target. 
		''' </summary>
		''' <param name="value">The value produced by the binding source.</param>
		''' <param name="targetType">The type of the binding target property.</param>
		''' <param name="parameter">The converter parameter to use.</param>
		''' <param name="culture">The culture to use in the converter.</param>
		''' <returns>A converted value.</returns>
        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
            Dim result As Visibility = Visibility.Collapsed
            If value IsNot Nothing AndAlso (TypeOf value Is Date) AndAlso (CDate(value)).Date = Date.Today Then
                result = Visibility.Visible
            End If

            If Not String.IsNullOrEmpty(CStr(parameter)) Then
                ' invert result
                result = If(result = Visibility.Visible, Visibility.Collapsed, Visibility.Visible)
            End If
            Return result
        End Function
		''' <summary>
		''' Returns Binding.DoNothing. 
		''' </summary>
		''' <param name="value">The value that is produced by the binding target.</param>
		''' <param name="targetType">The type to convert to.</param>
		''' <param name="parameter">The converter parameter to use.</param>
		''' <param name="culture">The culture to use in the converter.</param>
		''' <returns>A converted value.</returns>
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return DependencyProperty.UnsetValue
        End Function
	End Class

	''' <summary>
	''' Returns VisualIntervalCollection for the specified day and specified SchedulerGroup which can be used
	''' as an ItemsSource for VisualIntervalsPresenter control.
	''' </summary>
	''' <remarks>
	''' If converter Parameter property is "Self", returns list of a single VisualIntervalGroup, to use it as ItemsSource for representing all-day area.
	''' In all other cases returns VisualItervalCollection containing time slots for the single day.
	''' </remarks>
	Public Class ItemsSourceRetriever
		Inherits FrameworkElement
		Public Sub New()
			Visibility = System.Windows.Visibility.Collapsed

		End Sub

		Public Property Parameter() As String

		Public Property ItemsSource() As Object
			Get
				Return CObj(GetValue(ItemsSourceProperty))
			End Get
			Set(ByVal value As Object)
				SetValue(ItemsSourceProperty, value)
			End Set
		End Property

		' Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
		Public Shared ReadOnly ItemsSourceProperty As DependencyProperty = DependencyProperty.Register("ItemsSource", GetType(Object), GetType(ItemsSourceRetriever), Nothing)

		Public Property SchedulerGroup() As Object
			Get
				Return CObj(GetValue(SchedulerGroupProperty))
			End Get
			Set(ByVal value As Object)
				SetValue(SchedulerGroupProperty, value)
			End Set
		End Property

		' Using a DependencyProperty as the backing store for SchedulerGroup.  This enables animation, styling, binding, etc...
		Public Shared ReadOnly SchedulerGroupProperty As DependencyProperty = DependencyProperty.Register("SchedulerGroup", GetType(Object), GetType(ItemsSourceRetriever), New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnSchedulerGroupChanged)))

		Private Shared Sub OnSchedulerGroupChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
			Dim r As ItemsSourceRetriever = TryCast(d, ItemsSourceRetriever)
			r.OnSchedulerGroupChanged()

		End Sub
		Private Sub OnSchedulerGroupChanged()
			Dim group As SchedulerGroupItem = TryCast(SchedulerGroup, SchedulerGroupItem)
			Dim index As Integer = -1
			If group IsNot Nothing Then
				Dim itm As ItemsControl = TryCast(VTreeHelper.GetParentOfType(Me, GetType(ItemsControl)), ItemsControl)
				If itm IsNot Nothing Then
					Dim data As Object = itm.DataContext

					Dim itmParent As ItemsControl = TryCast(VTreeHelper.GetParentOfType(itm, GetType(ItemsControl)), ItemsControl)
					If itmParent IsNot Nothing Then
						index = itmParent.Items.IndexOf(data)
						Dim visualIntervalGroup As VisualIntervalGroup = TryCast(group.VisualIntervalGroups(index), VisualIntervalGroup)
						If Parameter.ToLower() = "self" Then
							' create list of a single VisualIntervalGroup
							' (we need list to use it as ItemsSource)
							Dim list As New List(Of Object)()
							list.Add(visualIntervalGroup)
							ItemsSource = list
						Else
							ItemsSource = visualIntervalGroup.VisualIntervals
						End If
					End If
				End If
			End If
		End Sub
	End Class
End Namespace
