
Namespace Primitives

    ''' <summary>
    ''' Encapsulates a collection of <see cref="Mercury.Mobile.Forms.Primitives.Row"/> objects, each representing a single row in a table.
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class RowCollection
        Implements IEnumerable

        Private _rows As List(Of Row)

        ''' <summary>
        ''' Initializes a default instance of the <see cref="Mercury.Mobile.Forms.Primitives.RowCollection"/> class.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            Me._rows = New List(Of Row)
        End Sub

#Region "Properties"

        ''' <summary>
        ''' Gets the number of elements contained in this collection.
        ''' </summary>
        ''' <returns>The number of elements contained in this collection.</returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Count() As Integer
            Get
                Return Me._rows.Count
            End Get
        End Property

        ''' <summary>
        ''' Gets or sets the value of a row at the specified index.
        ''' </summary>
        ''' <param name="index">The index of the row to get or set.</param>
        ''' <returns>The value of a row at the specified index.</returns>
        Default Public Property Item(ByVal index As Integer) As Row
            Get
                Return Me._rows.Item(index)
            End Get
            Set(ByVal value As Row)
                Me._rows.Item(index) = value
            End Set
        End Property

#End Region

#Region "Methods"

        ''' <summary>
        ''' Adds the specified row to this collection.
        ''' </summary>
        ''' <param name="value">The row to add to this collection.</param>
        ''' <remarks></remarks>
        Public Sub Add(ByVal value As Row)
            Me._rows.Add(value)
        End Sub

        ''' <summary>
        ''' Removes all rows from this collection.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Clear()
            Me._rows.Clear()
        End Sub

        ''' <summary>
        ''' Inserts the specified row at the specified index.
        ''' </summary>
        ''' <param name="value">The row to insert.</param>
        ''' <param name="index">The index at which to insert.</param>
        ''' <remarks></remarks>
        Public Sub InsertAt(ByVal value As Row, ByVal index As Integer)
            Me._rows.Insert(index, value)
        End Sub

        ''' <summary>
        ''' Returns the zero-based index of the first occurrence of the 
        ''' specified row in the collection or -1 if it does not exist.
        ''' </summary>
        ''' <param name="value">The row to search for.</param>
        ''' <returns>The zero-based index of the first occurrence of the 
        ''' specified row in the collection or -1 if it does not exist.</returns>
        Public Function IndexOf(ByVal value As Row) As Integer
            Return Me._rows.IndexOf(value)
        End Function

        ''' <summary>
        ''' Removes the first occurrence of the specified row from this collection.
        ''' </summary>
        ''' <param name="value">The row to remove.</param>
        ''' <remarks></remarks>
        Public Sub Remove(ByVal value As Row)
            Me._rows.Remove(value)
        End Sub

        ''' <summary>
        ''' Removes the row at the specified index.
        ''' </summary>
        ''' <param name="index">The index of the row to remove.</param>
        ''' <remarks></remarks>
        Public Sub RemoveAt(ByVal index As Integer)
            Me._rows.RemoveAt(index)
        End Sub

#End Region

#Region "IEnumerable Interface Members"

        Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
            Return New RowEnumerator(Me)
        End Function

#Region "RowEnumerator Class"

        ''' <summary>
        ''' Private enumerator class.
        ''' </summary>
        ''' <remarks></remarks>
        Private Class RowEnumerator
            Implements IEnumerator

            ' Declared variables
            Private m_iPos As Integer = -1
            Private dataSlices As RowCollection

            ''' <summary>
            ''' Default constructor.
            ''' </summary>
            ''' <remarks></remarks>
            Public Sub New(ByVal slices As RowCollection)
                Me.dataSlices = slices
            End Sub

            Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
                Get
                    Return Me.dataSlices.Item(Me.m_iPos)
                End Get
            End Property

            Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
                If Me.m_iPos < (Me.dataSlices.Count - 1) Then
                    Me.m_iPos += 1
                    Return True
                Else
                    Return False
                End If
            End Function

            Public Sub Reset() Implements System.Collections.IEnumerator.Reset
                Me.m_iPos = -1
            End Sub

        End Class

#End Region

#End Region

    End Class

End Namespace