package cz.muni.sutralog.sorter

import cz.muni.sutralog.data.RockPresentableListItem

class RockSorter {
    var selectedColumn: SortColumns = SortColumns.Name
    var order: SortOrder = SortOrder.ASC

    fun sortRockList(rockList: List<RockPresentableListItem>): List<RockPresentableListItem> {
        for (rock in rockList) {
            return when (selectedColumn) {
                SortColumns.Name -> sortBy(rockList, RockPresentableListItem::name)
                SortColumns.Date -> sortBy(rockList, RockPresentableListItem::date)
                SortColumns.Locality -> sortBy(rockList, RockPresentableListItem::locality)
                SortColumns.Where -> sortBy(rockList, RockPresentableListItem::where)
                SortColumns.From -> sortBy(rockList, RockPresentableListItem::from)
            }
        }

        return rockList
    }

    private fun <T : Comparable<T>> sortBy(
        rockList: List<RockPresentableListItem>,
        property: RockPresentableListItem.() -> T?
    ): List<RockPresentableListItem> {
        return if (order == SortOrder.ASC) {
            rockList.sortedBy { it.property() }
        } else {
            rockList.sortedByDescending { it.property() }
        }
    }
}