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.PdfViewer
Imports System.Windows.Data

Namespace PdfViewerSamples
	<TemplatePart(Name:="ScrollViewer", Type:=GetType(ScrollViewer))>
	Public Class Thumbnails
		Inherits ItemsControl
		Private _scrollViewer As ScrollViewer
		Private _lastSelectedChild As ThumbnailItem = Nothing

		Public Sub New()
			Me.DefaultStyleKey = GetType(Thumbnails)
		End Sub


		#Region "** overrides"

		Protected Overrides Function GetContainerForItemOverride() As DependencyObject
			Return New ThumbnailItem()
		End Function

		Protected Overrides Function IsItemItsOwnContainerOverride(ByVal item As Object) As Boolean
			Return (TypeOf item Is ThumbnailItem)
		End Function

		Protected Overrides Sub PrepareContainerForItemOverride(ByVal element As DependencyObject, ByVal item As Object)
			MyBase.PrepareContainerForItemOverride(element, item)
			Dim container = CType(element, ThumbnailItem)
			If element IsNot item Then
				container.SetBinding(ThumbnailItem.ScaleProperty, New System.Windows.Data.Binding("ItemScale") With {.Source = Me})
				container.Content = item

				If _lastSelectedChild Is Nothing Then
					Me.SelectItem(container)
				End If

				RemoveHandler container.MouseLeftButtonDown, AddressOf container_MouseLeftButtonDown
				AddHandler container.MouseLeftButtonDown, AddressOf container_MouseLeftButtonDown
			End If
		End Sub

		Public Overrides Sub OnApplyTemplate()
			MyBase.OnApplyTemplate()

			_scrollViewer = CType(GetTemplateChild("ScrollViewer"), ScrollViewer)
		End Sub


		#End Region

		#Region "ItemScale"

		Public Property ItemScale() As Double
			Get
				Return CDbl(GetValue(ItemScaleProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(ItemScaleProperty, value)
			End Set
		End Property
		Public Shared ReadOnly ItemScaleProperty As DependencyProperty = DependencyProperty.Register("ItemScale", GetType(Double), GetType(Thumbnails), New PropertyMetadata(1.0))

		#End Region

		#Region "PdfViewer"

		Public Property PdfViewer() As C1PdfViewer
			Get
				Return CType(GetValue(PdfViewerProperty), C1PdfViewer)
			End Get
			Set(ByVal value As C1PdfViewer)
				SetValue(PdfViewerProperty, value)
			End Set
		End Property

		' Using a DependencyProperty as the backing store for PdfViewer.  This enables animation, styling, binding, etc...
		Public Shared ReadOnly PdfViewerProperty As DependencyProperty = DependencyProperty.Register("PdfViewer", GetType(C1PdfViewer), GetType(Thumbnails), New PropertyMetadata(Nothing, AddressOf OnPdfViewerChanged))

		Private Shared Sub OnPdfViewerChanged(ByVal sender As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
			CType(sender, Thumbnails).OnPdfViewerChanged(TryCast(args.OldValue, C1PdfViewer), TryCast(args.NewValue, C1PdfViewer))
		End Sub

		#End Region

		#Region "SelectedBrush"

		Public Property SelectedBrush() As Brush
			Get
				Return CType(GetValue(SelectedBrushProperty), Brush)
			End Get
			Set(ByVal value As Brush)
				SetValue(SelectedBrushProperty, value)
			End Set
		End Property

		' Using a DependencyProperty as the backing store for SelectedBrush.  This enables animation, styling, binding, etc...
		Public Shared ReadOnly SelectedBrushProperty As DependencyProperty = DependencyProperty.Register("SelectedBrush", GetType(Brush), GetType(Thumbnails), New PropertyMetadata(Nothing))

		#End Region

		Private Sub OnPdfViewerChanged(ByVal oldPdf As C1PdfViewer, ByVal newPdf As C1PdfViewer)
			If newPdf IsNot Nothing Then
				ItemsSource = newPdf.GetPages()
			Else
				ItemsSource = Nothing
			End If
		End Sub

		Private Sub container_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			' handle selected page
			SelectItem(CType(sender, ThumbnailItem))

			' scroll entirely into view
			Dim childIndex = ItemContainerGenerator.IndexFromContainer(CType(sender, ThumbnailItem))
			Dim pageSize = _scrollViewer.ExtentHeight / PdfViewer.PageCount
			Dim verticalChildStart = pageSize * childIndex
			Dim verticalChildEnd = pageSize * (childIndex + 1)
			If _scrollViewer.VerticalOffset > verticalChildStart Then
				_scrollViewer.ScrollToVerticalOffset(verticalChildStart)
			ElseIf _scrollViewer.VerticalOffset + _scrollViewer.ViewportHeight < verticalChildEnd Then

				_scrollViewer.ScrollToVerticalOffset(verticalChildStart - _scrollViewer.ViewportHeight + pageSize)
			End If

			' Change Page in C1PdfViewer
			Dispatcher.BeginInvoke(Sub() PdfViewer.GoToPage(1 + childIndex))
		End Sub


		Private Sub SelectItem(ByVal newChild As ThumbnailItem)
			If _lastSelectedChild IsNot Nothing Then
				_lastSelectedChild.ClearValue(BackgroundProperty)
			End If

			If newChild IsNot Nothing Then
				newChild.SetBinding(BackgroundProperty, New Binding("SelectedBrush") With {.Source = Me})
			End If
			_lastSelectedChild = newChild
		End Sub
	End Class
End Namespace
