package com.marvinelsen.willow.ui.controllers

import com.marvinelsen.willow.Model
import com.marvinelsen.willow.ui.DictionaryEntryFx
import javafx.beans.binding.Bindings
import javafx.fxml.FXML
import javafx.scene.control.Label
import javafx.scene.control.ListView
import javafx.scene.control.ProgressIndicator

class MainController(private val model: Model) {
    @FXML
    private lateinit var progressIndicatorEntries: ProgressIndicator

    @FXML
    @Suppress("UnusedPrivateProperty")
    private lateinit var labelNoEntriesFound: Label

    @FXML
    private lateinit var listViewSearchResults: ListView<DictionaryEntryFx>

    @FXML
    @Suppress("UnusedPrivateMember")
    private fun initialize() {
        listViewSearchResults.items = model.searchResults
        listViewSearchResults
            .disableProperty()
            .bind(Bindings.or(model.isSearching, Bindings.isEmpty(model.searchResults)))

        progressIndicatorEntries.visibleProperty().bind(model.isSearching)

        listViewSearchResults.selectionModel.selectedItemProperty().addListener { _, _, newValue: DictionaryEntryFx? ->
            if (newValue == null) {
                return@addListener
            }
            model.selectEntry(newValue)
        }
    }
}
