package cz.muni.sutralog.filter

import cz.muni.sutralog.data.RockPresentableListItem
import java.time.LocalDate

class RockFilter() {
    var nameSearchKey: String = ""
    var fromDate: LocalDate? = null
    var toDate: LocalDate? = null
    var selectedColumn = FilterColumns.None
    var columnSearchKey: String = ""

    fun filterRockList(fullRockList: List<RockPresentableListItem>): List<RockPresentableListItem> {
        return fullRockList.filter { rock ->
            checkName(rock)
                    && checkSelectedColumn(rock)
                    && rock.isInDate(fromDate, toDate)
        }
    }

    private fun checkName(rock: RockPresentableListItem) : Boolean{
        return rock.columnContains(RockPresentableListItem::name, nameSearchKey)
    }

    private fun checkSelectedColumn(rock: RockPresentableListItem) : Boolean {
        return when(selectedColumn){
            FilterColumns.Comment -> rock.columnContains(RockPresentableListItem::comment, columnSearchKey)
            FilterColumns.Count -> rock.columnContains(RockPresentableListItem::count, columnSearchKey)
            FilterColumns.Description -> rock.columnContains(RockPresentableListItem::description, columnSearchKey)
            FilterColumns.From -> rock.columnContains(RockPresentableListItem::from, columnSearchKey)
            FilterColumns.Location -> rock.columnContains(RockPresentableListItem::locality, columnSearchKey)
            FilterColumns.Where -> rock.columnContains(RockPresentableListItem::where, columnSearchKey)
            FilterColumns.All -> anyFilterColumnContains(rock, columnSearchKey)
            else -> {true}
        }
    }

    private fun anyFilterColumnContains(rock: RockPresentableListItem, nameSearchKey: String): Boolean {
        return rock.columnContains(RockPresentableListItem::comment, nameSearchKey)
                || rock.columnContains(RockPresentableListItem::count, nameSearchKey)
                || rock.columnContains(RockPresentableListItem::description, nameSearchKey)
                || rock.columnContains(RockPresentableListItem::from, nameSearchKey)
                || rock.columnContains(RockPresentableListItem::locality, nameSearchKey)
                || rock.columnContains(RockPresentableListItem::where, nameSearchKey)
    }

    fun resetFilter() {
        resetNameSearchKey()
        resetFromDate()
        resetToDate()
        resetSelectedColumn()
        resetColumnSearchKey()
    }

    fun resetNameSearchKey() {
        nameSearchKey = ""
    }

    fun resetSelectedColumn() {
        selectedColumn = FilterColumns.None
    }

    fun resetColumnSearchKey() {
        columnSearchKey = ""
    }

    fun resetFromDate() {
        fromDate = null
    }

    fun resetToDate() {
        toDate = null
    }

}