package com.marvinelsen.willow.ui.controllers

import com.marvinelsen.willow.Model
import com.marvinelsen.willow.config.Config
import com.marvinelsen.willow.ui.dialogs.PreferencesDialog
import javafx.application.Platform
import javafx.beans.binding.Bindings
import javafx.fxml.FXML
import javafx.scene.control.MenuBar
import javafx.scene.control.MenuItem
import java.util.ResourceBundle

@Suppress("UnusedPrivateMember")
class MenuController(private val model: Model, private val config: Config) {
    @FXML
    private lateinit var resources: ResourceBundle

    @FXML
    private lateinit var menuBar: MenuBar

    @FXML
    private lateinit var menuItemCopyHeadword: MenuItem

    @FXML
    private lateinit var menuItemCopyPronunciation: MenuItem

    @FXML
    private fun initialize() {
        menuItemCopyPronunciation.disableProperty().bind(Bindings.isNull(model.selectedEntry))
        menuItemCopyHeadword.disableProperty().bind(Bindings.isNull(model.selectedEntry))
    }

    @FXML
    private fun onMenuItemPreferencesAction() {
        PreferencesDialog(menuBar.scene.window, config, resources).showAndWait().ifPresent { result ->
            when (result) {
                PreferencesDialog.Result.SAVE_CHANGES -> config.save()
                PreferencesDialog.Result.DO_NOT_SAVE_CHANGES -> config.load()
            }
        }
    }

    @FXML
    private fun onMenuItemQuitAction() {
        Platform.exit()
    }

    @FXML
    private fun onMenuItemCopyPronunciationAction() {
        model.copyPronunciationOfSelectedEntry()
    }

    @FXML
    private fun onMenuItemCopyHeadwordAction() {
        model.copyHeadwordOfSelectedEntry()
    }
}
