Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.Globalization
Imports System.Threading
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Threading

Imports C1.C1Schedule
Imports C1.Silverlight
Imports C1.Silverlight.Schedule
Imports System.Windows.Input

Namespace CustomDialogs
	''' <summary>
	''' The <see cref="ShowRemindersControl"/> displays currently active reminders and allows to control them.
	''' </summary>
	Partial Public Class ShowRemindersControl
		Inherits UserControl
		#Region "dependency properties"
		''' <summary>
		''' Gets or sets reference to the <see cref="C1Scheduler"/> control 
		''' which is an owner of displayed <see cref="Reminder"/> objects.
		''' </summary>
		Public Property Scheduler() As C1Scheduler
			Get
				Return CType(GetValue(SchedulerProperty), C1Scheduler)
			End Get
			Set(ByVal value As C1Scheduler)
				SetValue(SchedulerProperty, value)
			End Set
		End Property

		''' <summary>
		''' Identifies the <see cref="Scheduler"/> dependency property.
		''' </summary>
		Public Shared ReadOnly SchedulerProperty As DependencyProperty = DependencyProperty.Register("Scheduler", GetType(C1Scheduler), GetType(ShowRemindersControl), New PropertyMetadata(AddressOf OnSchedulerChanged))

		Private Shared Sub OnSchedulerChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
			CType(d, ShowRemindersControl).OnSchedulerChanged(e)
		End Sub
		Private Sub OnSchedulerChanged(ByVal e As DependencyPropertyChangedEventArgs)
			Dim oldSch As C1Scheduler = TryCast(e.OldValue, C1Scheduler)
			If oldSch IsNot Nothing Then
				RemoveHandler (CType(oldSch.ActiveReminders, INotifyCollectionChanged)).CollectionChanged, AddressOf ShowRemindersControl_CollectionChanged
			End If
			remList.Scheduler = Scheduler
			If Scheduler IsNot Nothing Then
				AddHandler (CType(Scheduler.ActiveReminders, INotifyCollectionChanged)).CollectionChanged, AddressOf ShowRemindersControl_CollectionChanged
			End If
			UpdateTitle()
		End Sub
		#End Region

		#Region "** fields"
		Private _parentWindow As ContentControl = Nothing
		Private ReadOnly _timer As DispatcherTimer
		Private _isLoaded As Boolean = False
		#End Region

		#Region "** initializing"
		''' <summary>
		''' Initializes a new instance of the <see cref="ShowRemindersControl"/> class.
		''' </summary>
		Public Sub New()
			InitializeComponent()
			snoozeTimeSpansCombo.Value = TimeSpan.FromMinutes(5)
			_timer = New DispatcherTimer()
			AddHandler _timer.Tick, AddressOf Timer_Tick
		End Sub

		Private Sub root_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
			If Not _isLoaded Then
				_parentWindow = CType(VTreeHelper.GetParentOfType(Me, GetType(C1Window)), ContentControl)
				If _parentWindow IsNot Nothing Then
					If Scheduler Is Nothing Then
						Scheduler = TryCast(_parentWindow.DataContext, C1Scheduler)
					End If
					AddHandler (CType(_parentWindow, C1Window)).Closed, AddressOf _parentWindow_Closed
					AddHandler (CType(_parentWindow, C1Window)).WindowStateChanged, AddressOf _parentWindow_WindowStateChanged
				End If
				UpdateTitle()
				AddHandler remList.SelectionChanged, AddressOf remList_SelectionChanged
				If _parentWindow IsNot Nothing Then
					AddHandler remList.Loaded, AddressOf remList_Loaded
				End If
				UpdateTimer(1)
				_isLoaded = True
            End If
            Dim [rem] As Reminder = TryCast(_parentWindow.Tag, Reminder)
            If [rem] IsNot Nothing And [rem].ParentAppointment.ReminderPlaySound And Not [rem].ParentAppointment.ReminderOverrideDefault Then
                PlaySound()
            End If

            ShowRemindersControl_CollectionChanged(Nothing, Nothing)
        End Sub
		Private Sub remList_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
			RemoveHandler remList.Loaded, AddressOf remList_Loaded
			If remList.ActualWidth >= 7 Then
				listHeader.ColumnDefinitions(1).Width = New GridLength(remList.ActualWidth / 2 - 13)
				listHeader.ColumnDefinitions(0).Width = listHeader.ColumnDefinitions(1).Width
			End If
			remList.Focus()
		End Sub

		Private Sub _parentWindow_Closed(ByVal sender As Object, ByVal e As EventArgs)
			RemoveHandler _timer.Tick, AddressOf Timer_Tick
			_timer.Stop()
			RemoveHandler (CType(_parentWindow, C1Window)).Closed, AddressOf _parentWindow_Closed
			RemoveHandler (CType(_parentWindow, C1Window)).WindowStateChanged, AddressOf _parentWindow_WindowStateChanged
			If Scheduler IsNot Nothing Then
				RemoveHandler (CType(Scheduler.ActiveReminders, INotifyCollectionChanged)).CollectionChanged, AddressOf ShowRemindersControl_CollectionChanged
			End If
			RemoveHandler remList.Loaded, AddressOf remList_Loaded
			RemoveHandler remList.SelectionChanged, AddressOf remList_SelectionChanged
		End Sub

		' update reminders due is time on timer ticks
		Private Sub UpdateTimer(ByVal seconds As Integer)
			_timer.Stop()
			_timer.Interval = TimeSpan.FromSeconds(seconds)
			_timer.Start()
		End Sub
		Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
			_timer.Stop()
			remList.RefreshData()
			If _parentWindow IsNot Nothing Then
				Dim [rem] As Reminder = TryCast(_parentWindow.Tag, Reminder)
				If [rem] IsNot Nothing AndAlso (_needChangeSelection OrElse remList.SelectedItems.Count = 0) Then
					_needChangeSelection = False
					remList.SelectedItem = [rem]
				End If
			End If
			UpdateTimer(20)
		End Sub
		Private Sub _parentWindow_WindowStateChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs(Of C1WindowState))
			If e.NewValue <> C1WindowState.Minimized Then
				UpdateTimer(1)
			End If
		End Sub
		#End Region

		#Region "** interface"
        Private Sub PlaySound()
            media.Position = TimeSpan.Zero
            media.Play()
        End Sub
		#End Region

		#Region "** private stuff"
		Private _needChangeSelection As Boolean = False
		Private Sub ShowRemindersControl_CollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
			UpdateTimer(1)
			_needChangeSelection = True
			If _parentWindow IsNot Nothing Then
				Dim [rem] As Reminder = TryCast(_parentWindow.Tag, Reminder)
				If [rem] IsNot Nothing Then
					remList.SelectedItem = [rem]
				End If
			End If
			UpdateTitle()
		End Sub

		Private Sub UpdateTitle()
			If _parentWindow IsNot Nothing Then
				Dim title As String = String.Empty
				Dim culture As CultureInfo = Thread.CurrentThread.CurrentUICulture
				Dim rems As ReadOnlyObservableCollection(Of Reminder) = Scheduler.ActiveReminders
				If rems Is Nothing OrElse rems.Count = 0 Then
					title = C1.Silverlight.Schedule.Resources.C1_Schedule_ShowReminders.Title
				Else
					If rems.Count = 1 Then
						title = C1.Silverlight.Schedule.Resources.C1_Schedule_ShowReminders.OneReminder
					Else
						title = String.Format(culture, C1.Silverlight.Schedule.Resources.C1_Schedule_ShowReminders.RemindersNumber, rems.Count)
					End If
				End If
				CType(_parentWindow, C1Window).Header = title
			End If
		End Sub
		Private Sub colSubject_SizeChanged(ByVal sender As Object, ByVal e As SizeChangedEventArgs)
			Me.remList.FirstColumnWidth = e.NewSize.Width
		End Sub

		Private Sub colDueIn_SizeChanged(ByVal sender As Object, ByVal e As SizeChangedEventArgs)
			Me.remList.SecondColumnWidth = e.NewSize.Width
		End Sub

		Private Sub snoozeButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim ar As Array = New Object(1) { snoozeTimeSpansCombo.Value.Value, remList.SelectedItems }
			C1Scheduler.SnoozeCommand.Execute(ar, Scheduler)
		End Sub

		Private Sub remList_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
			If remList.Items.Count > 0 AndAlso remList.SelectedItems.Count = 0 Then
				remList.SelectedIndex = 0
			End If
			UpdateSelectedInfo()
		End Sub

		Private Sub UpdateSelectedInfo()
			DataContext = remList
			Dim count As Integer = remList.SelectedItems.Count
			If count = 1 Then
				subject.Visibility = Visibility.Visible
				info.Visibility = subject.Visibility
				selectedReminders.Visibility = Visibility.Collapsed
				snoozeButton.IsEnabled = True
				dismissButton.IsEnabled = True
				openButton.IsEnabled = True
			ElseIf count > 1 Then
				subject.Visibility = Visibility.Collapsed
				info.Visibility = subject.Visibility
				selectedReminders.Visibility = Visibility.Visible
				snoozeButton.IsEnabled = True
				dismissButton.IsEnabled = True
				openButton.IsEnabled = True
			Else
				subject.Visibility = Visibility.Collapsed
				info.Visibility = subject.Visibility
				selectedReminders.Visibility = Visibility.Visible
				snoozeButton.IsEnabled = False
				dismissButton.IsEnabled = False
				openButton.IsEnabled = False
			End If
		End Sub
'INSTANT VB TODO TASK: There is no equivalent to #pragma directives in VB:
'#pragma warning disable 1591
		Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
			If e.Key = Key.Escape Then
				CType(_parentWindow, C1Window).Close()
			End If
			MyBase.OnKeyDown(e)
		End Sub
'INSTANT VB TODO TASK: There is no equivalent to #pragma directives in VB:
'#pragma warning restore 1591
		#End Region
	End Class
End Namespace
