package org.jabref.gui.importer.fetcher;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

import org.jabref.gui.JabRefFrame;
import org.jabref.gui.SidePaneComponent;
import org.jabref.gui.SidePaneManager;
import org.jabref.gui.SidePaneType;
import org.jabref.gui.actions.Action;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.help.HelpAction;
import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.search.SearchTextField;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.importer.SearchBasedFetcher;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;

import org.fxmisc.easybind.EasyBind;

public class WebSearchPane extends SidePaneComponent {

    private final JabRefPreferences preferences;
    private final WebSearchPaneViewModel viewModel;

    public WebSearchPane(SidePaneManager sidePaneManager, JabRefPreferences preferences, JabRefFrame frame) {
        super(sidePaneManager, IconTheme.JabRefIcons.WWW, Localization.lang("Web search"));
        this.preferences = preferences;
        this.viewModel = new WebSearchPaneViewModel(preferences.getImportFormatPreferences(), frame, preferences);
    }

    @Override
    public Action getToggleAction() {
        return StandardActions.TOGGLE_WEB_SEARCH;
    }

    @Override
    protected Node createContentPane() {
        // Setup combo box for fetchers
        ComboBox<SearchBasedFetcher> fetchers = new ComboBox<>();
        new ViewModelListCellFactory<SearchBasedFetcher>()
                .withText(SearchBasedFetcher::getName)
                .install(fetchers);
        fetchers.itemsProperty().bind(viewModel.fetchersProperty());
        fetchers.valueProperty().bindBidirectional(viewModel.selectedFetcherProperty());
        fetchers.setMaxWidth(Double.POSITIVE_INFINITY);

        // Create help button for currently selected fetcher
        StackPane helpButtonContainer = new StackPane();
        ActionFactory factory = new ActionFactory(preferences.getKeyBindingRepository());
        EasyBind.subscribe(viewModel.selectedFetcherProperty(), fetcher -> {
            if ((fetcher != null) && fetcher.getHelpPage().isPresent()) {
                HelpAction helpCommand = new HelpAction(fetcher.getHelpPage().get());
                Button helpButton = factory.createIconButton(StandardActions.HELP, helpCommand.getCommand());
                helpButtonContainer.getChildren().setAll(helpButton);
            } else {
                helpButtonContainer.getChildren().clear();
            }
        });
        HBox fetcherContainer = new HBox(fetchers, helpButtonContainer);
        HBox.setHgrow(fetchers, Priority.ALWAYS);

        // Create text field for query input
        TextField query = SearchTextField.create();
        viewModel.queryProperty().bind(query.textProperty());

        // Create button that triggers search
        Button search = new Button(Localization.lang("Search"));
        search.setDefaultButton(true);
        search.setOnAction(event -> viewModel.search());

        // Put everything together
        VBox container = new VBox();
        container.setAlignment(Pos.CENTER);
        container.getChildren().addAll(fetcherContainer, query, search);
        return container;
    }

    @Override
    public SidePaneType getType() {
        return SidePaneType.WEB_SEARCH;
    }

    @Override
    public void beforeClosing() {
        preferences.putBoolean(JabRefPreferences.WEB_SEARCH_VISIBLE, Boolean.FALSE);
    }

    @Override
    public void afterOpening() {
        preferences.putBoolean(JabRefPreferences.WEB_SEARCH_VISIBLE, Boolean.TRUE);
    }

    @Override
    public Priority getResizePolicy() {
        return Priority.NEVER;
    }
}

@@ -3090,24 +3090,80 @@
 d.create();%0A
+        query.setOnAction(event -%3E viewModel.search());%0A
         view
@@ -3360,11 +3360,12 @@
 ton(
-tru
+fals
 e);%0A

Fix that web search pane steals input (#4335)