package org.jabref.gui.preferences;

import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

import org.jabref.gui.DialogService;
import org.jabref.gui.util.ControlHelper;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;

class AppearancePrefsTab extends Pane implements PrefsTab {

    public static final String BASE_CSS = "Base.css";
    public static final String DARK_CSS = "Dark.css";
    private final JabRefPreferences prefs;
    private final CheckBox fontTweaksLAF;
    private final TextField fontSize;
    private final CheckBox overrideFonts;
    private final VBox container = new VBox();
    private final DialogService dialogService;
    private final RadioButton lightTheme;
    private final RadioButton darkTheme;

    /**
     * Customization of appearance parameters.
     *
     * @param prefs a <code>JabRefPreferences</code> value
     */
    public AppearancePrefsTab(DialogService dialogService, JabRefPreferences prefs) {
        this.dialogService = dialogService;
        this.prefs = prefs;

        overrideFonts = new CheckBox(Localization.lang("Override default font settings"));
        fontSize = new TextField();
        fontSize.setTextFormatter(ControlHelper.getIntegerTextFormatter());
        Label fontSizeLabel = new Label(Localization.lang("Font size:"));
        HBox fontSizeContainer = new HBox(fontSizeLabel, fontSize);
        VBox.setMargin(fontSizeContainer, new Insets(0, 0, 0, 35));
        fontSizeContainer.disableProperty().bind(overrideFonts.selectedProperty().not());
        fontTweaksLAF = new CheckBox(Localization.lang("Tweak font rendering for entry editor on Linux"));

        ToggleGroup themeGroup = new ToggleGroup();
        lightTheme = new RadioButton("Light theme");
        lightTheme.setToggleGroup(themeGroup);
        darkTheme = new RadioButton("Dark theme");
        darkTheme.setToggleGroup(themeGroup);

        if (prefs.get(JabRefPreferences.FX_THEME).equals(BASE_CSS))
            lightTheme.setSelected(true);
        else if (prefs.get(JabRefPreferences.FX_THEME).equals(DARK_CSS))
            darkTheme.setSelected(true);

        container.getChildren().addAll(overrideFonts, fontSizeContainer, fontTweaksLAF, lightTheme, darkTheme);

    }

    public Node getBuilder() {
        return container;
    }

    @Override
    public void setValues() {
        fontTweaksLAF.setSelected(prefs.getBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK));
        overrideFonts.setSelected(prefs.getBoolean(JabRefPreferences.OVERRIDE_DEFAULT_FONT_SIZE));
        fontSize.setText(String.valueOf(prefs.getInt(JabRefPreferences.MAIN_FONT_SIZE)));
    }

    @Override
    public void storeSettings() {
        // Java FX font rendering tweak
        final boolean oldFxTweakValue = prefs.getBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK);
        prefs.putBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK, fontTweaksLAF.isSelected());

        final boolean oldOverrideDefaultFontSize = prefs.getBoolean(JabRefPreferences.OVERRIDE_DEFAULT_FONT_SIZE);
        final int oldFontSize = prefs.getInt(JabRefPreferences.MAIN_FONT_SIZE);
        prefs.putBoolean(JabRefPreferences.OVERRIDE_DEFAULT_FONT_SIZE, overrideFonts.isSelected());
        int newFontSize = Integer.parseInt(fontSize.getText());
        prefs.putInt(JabRefPreferences.MAIN_FONT_SIZE, newFontSize);

        boolean isThemeChanged = false;

        if (lightTheme.isSelected() && !prefs.get(JabRefPreferences.FX_THEME).equals(BASE_CSS)) {
            prefs.put(JabRefPreferences.FX_THEME, BASE_CSS);
            isThemeChanged = true;
        } else if (darkTheme.isSelected() && !prefs.get(JabRefPreferences.FX_THEME).equals(DARK_CSS)) {
            prefs.put(JabRefPreferences.FX_THEME, DARK_CSS);
            isThemeChanged = true;
        }

        boolean isRestartRequired =
                (oldFxTweakValue != fontTweaksLAF.isSelected())
                        || (oldOverrideDefaultFontSize != overrideFonts.isSelected())
                        || (oldFontSize != newFontSize)
                        || isThemeChanged;
        if (isRestartRequired) {
            dialogService.showWarningDialogAndWait(Localization.lang("Settings"),
                    Localization.lang("Some appearance settings you changed require to restart JabRef to come into effect."));
        }
    }

    @Override
    public boolean validateSettings() {
        return true;
    }

    @Override
    public String getTabName() {
        return Localization.lang("Appearance");
    }
}

@@ -499,24 +499,68 @@
 calization;%0A
+import org.jabref.model.strings.StringUtil;%0A
 import org.j
@@ -2266,20 +2266,37 @@
         
-if (
+String cssFileName = 
 prefs.ge
@@ -2324,34 +2324,96 @@
 X_THEME)
-.equals(BASE_CSS))
+;%0A        if (StringUtil.isBlank(cssFileName) %7C%7C BASE_CSS.equals(cssFileName)) %7B
 %0A       
@@ -2454,16 +2454,18 @@
 %0A       
+ %7D
  else if
@@ -2470,63 +2470,39 @@
 if (
-prefs.get(JabRefPreferences.FX_THEME).equals(DARK_CSS))
+DARK_CSS.equals(cssFileName)) %7B
 %0A   
@@ -2538,16 +2538,26 @@
 d(true);
+%0A        %7D
 %0A%0A      

fix NPE in AppearancePreferenceTab (#4386)