package com.marvinelsen.willow.ui.controllers

import com.marvinelsen.willow.Model
import com.marvinelsen.willow.domain.SearchMode
import javafx.fxml.FXML
import javafx.scene.control.TextField
import javafx.scene.control.ToggleGroup

class SearchController(private val model: Model) {
    @FXML
    private lateinit var searchModeToggleGroup: ToggleGroup

    @FXML
    private lateinit var textFieldSearch: TextField

    @FXML
    @Suppress("UnusedPrivateMember")
    private fun initialize() {
        textFieldSearch.textProperty().addListener { _, _, newValue ->
            if (newValue.isNullOrBlank()) {
                return@addListener
            }

            val searchMode = searchModeToggleGroup.selectedToggle.userData as SearchMode
            model.search(newValue, searchMode)
        }

        searchModeToggleGroup.selectedToggleProperty().addListener { _, _, newValue ->
            if (textFieldSearch.text.isNullOrBlank()) {
                return@addListener
            }

            val searchMode = newValue.userData as SearchMode
            model.search(textFieldSearch.text, searchMode)
        }
    }
}
