package com.marvinelsen.willow.ui.controllers.dialogs

import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.config.Pronunciation
import com.marvinelsen.willow.config.Script
import com.marvinelsen.willow.config.Theme
import com.marvinelsen.willow.ui.formatters.FontSizeTextFormatter
import javafx.fxml.FXML
import javafx.scene.control.CheckBox
import javafx.scene.control.ComboBox
import javafx.scene.control.Spinner
import java.util.Locale

@Suppress("UnusedPrivateMember")
class PreferencesDialogController(private val config: Config) {
    @FXML
    private lateinit var comboBoxLocale: ComboBox<Locale>

    @FXML
    private lateinit var comboBoxTheme: ComboBox<Theme>

    @FXML
    private lateinit var comboBoxScript: ComboBox<Script>

    @FXML
    private lateinit var comboBoxPronunciationSearchResults: ComboBox<Pronunciation>

    @FXML
    private lateinit var comboBoxPronunciationDetails: ComboBox<Pronunciation>

    @FXML
    private lateinit var checkBoxShowPronunciationSearchResults: CheckBox

    @FXML
    private lateinit var checkBoxShowDefinitionSearchResults: CheckBox

    @FXML
    private lateinit var spinnerHeadwordFontSizeDetails: Spinner<Int>

    @FXML
    private lateinit var spinnerPronunciationFontSizeDetails: Spinner<Int>

    @FXML
    private lateinit var spinnerHeadwordFontSizeSearchResults: Spinner<Int>

    @FXML
    private lateinit var spinnerPronunciationFontSizeSearchResults: Spinner<Int>

    @FXML
    private lateinit var spinnerDefinitionFontSizeSearchResults: Spinner<Int>

    private val entryHeadwordFontSizeObjectProperty = config.details.headwordFontSize.asObject()
    private val entryPronunciationFontSizeObjectProperty = config.details.pronunciationFontSize.asObject()

    private val searchResultHeadwordFontSizeObjectProperty = config.searchResults.headwordFontSize.asObject()
    private val searchResultPronunciationFontSizeObjectProperty = config.searchResults.pronunciationFontSize.asObject()
    private val searchResultDefinitionFontSizeObjectProperty = config.searchResults.definitionFontSize.asObject()

    @FXML
    private fun initialize() {
        comboBoxLocale.items.addAll(
            setOf(
                Locale.ENGLISH,
                Locale.GERMAN,
                Locale.CHINA,
                Locale.TAIWAN
            )
        )

        comboBoxTheme.valueProperty().bindBidirectional(config.theme)
        comboBoxScript.valueProperty().bindBidirectional(config.script)
        comboBoxLocale.valueProperty().bindBidirectional(config.locale)
        comboBoxPronunciationSearchResults.valueProperty().bindBidirectional(config.searchResults.pronunciation)
        comboBoxPronunciationDetails.valueProperty().bindBidirectional(config.details.pronunciation)

        checkBoxShowDefinitionSearchResults
            .selectedProperty()
            .bindBidirectional(config.searchResults.shouldShowDefinition)
        checkBoxShowPronunciationSearchResults
            .selectedProperty()
            .bindBidirectional(config.searchResults.shouldShowPronunciation)

        with(spinnerHeadwordFontSizeDetails) {
            editor.textFormatter = FontSizeTextFormatter()
            valueFactory.valueProperty().bindBidirectional(entryHeadwordFontSizeObjectProperty)
        }

        with(spinnerPronunciationFontSizeDetails) {
            editor.textFormatter = FontSizeTextFormatter()
            valueFactory.valueProperty().bindBidirectional(entryPronunciationFontSizeObjectProperty)
        }

        with(spinnerHeadwordFontSizeSearchResults) {
            editor.textFormatter = FontSizeTextFormatter()
            valueFactory.valueProperty().bindBidirectional(searchResultHeadwordFontSizeObjectProperty)
        }

        with(spinnerPronunciationFontSizeSearchResults) {
            editor.textFormatter = FontSizeTextFormatter()
            valueFactory.valueProperty().bindBidirectional(searchResultPronunciationFontSizeObjectProperty)
        }

        with(spinnerDefinitionFontSizeSearchResults) {
            editor.textFormatter = FontSizeTextFormatter()
            valueFactory.valueProperty().bindBidirectional(searchResultDefinitionFontSizeObjectProperty)
        }
    }
}
