Imports Mercury.Mobile.Data
Imports System.Collections.Generic
Imports System.Threading

''' <summary>
''' A mock implementation of the IVirtualizingItemProvider(of T) interface.
''' </summary>
''' <typeparam name="T">The type of the item referenced.</typeparam>
''' <remarks></remarks>
Public Class MockItemProvider(Of T)
    Implements IVirtualizingItemProvider(Of T)

    Public Sub New(ByVal items As IList(Of T))
        Me._items = items
    End Sub

    Private _items As IList(Of T)

    Public ReadOnly Property Items() As IList(Of T)
        Get
            Return Me._items
        End Get
    End Property

    Private _delay As TimeSpan = TimeSpan.Zero

    Public Property Delay() As TimeSpan
        Get
            Return Me._delay
        End Get
        Set(ByVal value As TimeSpan)
            Me._delay = value
        End Set
    End Property

    Public Function FetchCount() As Integer Implements Data.IVirtualizingItemProvider(Of T).FetchCount
        Thread.Sleep(Me.Delay)
        Return Me._items.Count
    End Function

    Public Function FetchRange(ByVal offset As Integer, ByVal length As Integer, ByRef count As Integer) As System.Collections.Generic.IList(Of T) Implements Data.IVirtualizingItemProvider(Of T).FetchRange
        Thread.Sleep(Me.Delay)
        count = Me.Items.Count
        If offset > -1 AndAlso offset < Me._items.Count Then
            Dim itemList = New List(Of T)()
            Dim lesser = Math.Min(length, Me._items.Count - offset)
            Dim finalLength = offset + lesser
            For i As Integer = offset To finalLength - 1
                itemList.Add(Me._items(i))
            Next
            Return itemList
        End If
        Return Nothing
    End Function

    Public Function FetchAll() As System.Collections.Generic.IList(Of T) Implements Data.IVirtualizingItemProvider(Of T).FetchAll
        Dim ct As Integer = 0
        Return Me.FetchRange(0, Me.Items.Count, ct)
    End Function

    Private Sub Apply(ByVal info As Data.FilterInfo, ByVal sortExpression As System.Collections.Generic.IList(Of Data.SortField)) Implements Data.IVirtualizingItemProvider(Of T).Apply

    End Sub

    Public Sub ClearFilters() Implements Data.IVirtualizingItemProvider(Of T).ClearFilters

    End Sub

End Class