Imports System.Reflection
Imports System.ComponentModel
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

Namespace MainTestApplication
	Partial Public Class SearchBox
		Inherits UserControl
		Private _propertyInfo As New List(Of PropertyInfo)()
		Private _timer As New C1.Util.Timer()

		Public Sub New()
			InitializeComponent()
			_timer.Interval = TimeSpan.FromMilliseconds(800)
			AddHandler _timer.Tick, AddressOf _timer_Tick
		End Sub
		Private privateView As ICollectionView
		Public Property View() As ICollectionView
			Get
				Return privateView
			End Get
			Set(ByVal value As ICollectionView)
				privateView = value
			End Set
		End Property
		Public ReadOnly Property FilterProperties() As List(Of PropertyInfo)
			Get
				Return _propertyInfo
			End Get
		End Property
		Private Sub _txtSearch_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
			If _imgClear IsNot Nothing Then
				' update clear button visibility
				_imgClear.Visibility = If(String.IsNullOrEmpty(_txtSearch.Text), Visibility.Collapsed, Visibility.Visible)

				' start ticking
				_timer.Stop()
				_timer.Start()
			End If
		End Sub
		Private Sub _imgClear_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			_txtSearch.Text = String.Empty
		End Sub
		Private Sub _timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
			_timer.Stop()
			If View IsNot Nothing AndAlso _propertyInfo.Count > 0 Then
				View.Filter = Nothing
				View.Filter = Function(item) AnonymousMethod1(item)
			End If
		End Sub
		
		'INSTANT VB TODO TASK: The return type of this anonymous method could not be determined by Instant VB:
		Private Function AnonymousMethod1(ByVal item As Object) As Object
			Dim srch = _txtSearch.Text
			If String.IsNullOrEmpty(srch) Then
				Return True
			End If
			For Each pi As PropertyInfo In _propertyInfo
				Dim value = TryCast(pi.GetValue(item, Nothing), String)
				If value IsNot Nothing AndAlso value.IndexOf(srch, StringComparison.OrdinalIgnoreCase) > -1 Then
					Return True
				End If
			Next pi
			Return False
		End Function
	End Class
	''' <summary>
	''' TextBox control without focus/mouseover effects.
	''' </summary>
	Public Class TextBoxNoFocus
		Inherits TextBox
		Public Overrides Sub OnApplyTemplate()
			For Each s As String In "FocusVisualElement,MouseOverBorder".Split(","c)
				Dim bdr = TryCast(GetTemplateChild(s), Border)
				If bdr IsNot Nothing Then
					bdr.BorderThickness = New Thickness(0)
				End If
			Next s
			MyBase.OnApplyTemplate()
		End Sub
	End Class
End Namespace
