Imports System
Imports System.Collections

Namespace ASPNET.StarterKit.TimeTracker.BusinessLogicLayer
    '*********************************************************************
    '
    ' TimeEntriesCollection Class 
    '
    ' The TimeEntriesCollection is a Custom TimeEntry collection used 
    ' to represet a list of TimeEntry objects.
    '
    '
    '*********************************************************************

    Public Class TimeEntriesCollection
        Inherits ArrayList

        Public Enum TimeEntryFields
            InitValue
            Day
            Category
            Project
            Hours
            Description
        End Enum 'TimeEntryFields

        Public Overloads Sub Sort(ByVal sortField As TimeEntryFields, ByVal isAscending As Boolean)
            Select Case sortField
                Case TimeEntryFields.Day
                    MyBase.Sort(New DayComparer())
                Case TimeEntryFields.Category
                    MyBase.Sort(New CategoryComparer())
                Case TimeEntryFields.Description
                    MyBase.Sort(New DescriptionComparer())
                Case TimeEntryFields.Hours
                    MyBase.Sort(New HoursComparer())
                Case TimeEntryFields.Project
                    MyBase.Sort(New ProjectComparer())
            End Select

            If Not isAscending Then
                MyBase.Reverse()
            End If
        End Sub 'Sort

        Private NotInheritable Class DayComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
                Dim first As TimeEntry = CType(x, TimeEntry)
                Dim second As TimeEntry = CType(y, TimeEntry)
                Return first.EntryDate.CompareTo(second.EntryDate)
            End Function 'Compare
        End Class 'DayComparer

        Private NotInheritable Class CategoryComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
                Dim first As TimeEntry = CType(x, TimeEntry)
                Dim second As TimeEntry = CType(y, TimeEntry)
                Return first.CategoryName.CompareTo(second.CategoryName)
            End Function 'Compare
        End Class 'CategoryComparer

        Private NotInheritable Class ProjectComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
                Dim first As TimeEntry = CType(x, TimeEntry)
                Dim second As TimeEntry = CType(y, TimeEntry)
                Return first.ProjectName.CompareTo(second.ProjectName)
            End Function 'Compare
        End Class 'ProjectComparer

        Private NotInheritable Class DescriptionComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
                Dim first As TimeEntry = CType(x, TimeEntry)
                Dim second As TimeEntry = CType(y, TimeEntry)
                Return first.Description.CompareTo(second.Description)
            End Function 'Compare
        End Class 'DescriptionComparer

        Private NotInheritable Class HoursComparer
            Implements IComparer

            Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
                Dim first As TimeEntry = CType(x, TimeEntry)
                Dim second As TimeEntry = CType(y, TimeEntry)
                Return first.Duration.CompareTo(second.Duration)
            End Function 'Compare
        End Class 'HoursComparer
    End Class 'TimeEntriesCollection
End Namespace 'ASPNET.StarterKit.TimeTracker.BusinessLogicLayer