Imports Pilz.Configuration
Imports Pilz.GeneralEventArgs

Public Class ConfigurationManagerList
    Implements IList(Of ConfigurationManager)

    Public Event GettingParentManager(sender As Object, e As GetValueEventArgs(Of SimpleConfiguration))

    Private ReadOnly myList As New List(Of ConfigurationManager)

    Private Function GetParentManager()
        Dim args As New GetValueEventArgs(Of SimpleConfiguration)
        RaiseEvent GettingParentManager(Me, args)
        Return args.Value
    End Function

    Default Public Property Item(index As Integer) As ConfigurationManager Implements IList(Of ConfigurationManager).Item
        Get
            Return myList(index)
        End Get
        Set(value As ConfigurationManager)
            myList(index) = value
        End Set
    End Property

    Public ReadOnly Property Count As Integer Implements ICollection(Of ConfigurationManager).Count
        Get
            Return myList.Count
        End Get
    End Property

    Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of ConfigurationManager).IsReadOnly
        Get
            Return False
        End Get
    End Property

    Public Sub Insert(index As Integer, item As ConfigurationManager) Implements IList(Of ConfigurationManager).Insert
        myList.Insert(index, item)
        item.SetConfiguration(GetParentManager)
    End Sub

    Public Sub RemoveAt(index As Integer) Implements IList(Of ConfigurationManager).RemoveAt

    End Sub

    Public Sub Add(item As ConfigurationManager) Implements ICollection(Of ConfigurationManager).Add
        item.SetConfiguration(GetParentManager)
    End Sub

    Public Sub Clear() Implements ICollection(Of ConfigurationManager).Clear
        For Each item As ConfigurationManager In myList
            item.SetConfiguration(Nothing)
        Next
        myList.Clear()
    End Sub

    Public Sub CopyTo(array() As ConfigurationManager, arrayIndex As Integer) Implements ICollection(Of ConfigurationManager).CopyTo
        myList.CopyTo(array, arrayIndex)
    End Sub

    Public Function IndexOf(item As ConfigurationManager) As Integer Implements IList(Of ConfigurationManager).IndexOf
        Return myList.IndexOf(item)
    End Function

    Public Function Contains(item As ConfigurationManager) As Boolean Implements ICollection(Of ConfigurationManager).Contains
        Return myList.Contains(item)
    End Function

    Public Function Remove(item As ConfigurationManager) As Boolean Implements ICollection(Of ConfigurationManager).Remove
        item.SetConfiguration(Nothing)
        Return myList.Remove(item)
    End Function

    Public Function GetEnumerator() As IEnumerator(Of ConfigurationManager) Implements IEnumerable(Of ConfigurationManager).GetEnumerator
        Return IEnumerable_GetEnumerator()
    End Function

    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Return myList.GetEnumerator
    End Function

End Class
