package com.marvinelsen.willow.ui.services

import com.marvinelsen.willow.domain.Dictionary
import com.marvinelsen.willow.domain.SearchMode
import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.toFx
import com.marvinelsen.willow.ui.util.task
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.concurrent.Service

class SearchService(private val dictionary: Dictionary) : Service<ObservableList<DictionaryEntryFx>>() {
    lateinit var searchQuery: String
    lateinit var searchMode: SearchMode

    override fun createTask() = task {
        if (!this::searchQuery.isInitialized) error("Search query is not initialized")
        if (!this::searchMode.isInitialized) error("Search mode is not initialized")

        FXCollections.observableList(dictionary.search(searchQuery, searchMode).map { it.toFx() })
    }
}
