package com.marvinelsen.willow.ui.services

import com.marvinelsen.willow.domain.Dictionary
import com.marvinelsen.willow.ui.DictionaryEntryFx
import com.marvinelsen.willow.ui.toDomain
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 FindWordsService(private val dictionary: Dictionary) : Service<ObservableList<DictionaryEntryFx>>() {
    lateinit var entry: DictionaryEntryFx

    override fun createTask() = task {
        if (!this::entry.isInitialized) error("Entry is not initialized")

        FXCollections.observableList(dictionary.findWordsContaining(entry.toDomain()).map { it.toFx() })
    }
}
