package be.iminds.ilabt.jfed.experimenter_gui.preferences.subsections;

import be.iminds.ilabt.jfed.experimenter_gui.preferences.AbstractPreferencesSubPane;
import be.iminds.ilabt.jfed.experimenter_gui.util.ui.NumericTextField;
import be.iminds.ilabt.jfed.preferences.JFedGuiPreferences;
import be.iminds.ilabt.jfed.ui.javafx.dialogs.JFDialogs;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.BorderPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;

/**
 * User: twalcari
 * Date: 8/13/2015
 * Time: 16:34
 */
public class GeneralPane extends AbstractPreferencesSubPane {
    private static final Logger LOG = LoggerFactory.getLogger(GeneralPane.class);

    @FXML
    private CheckBox includeProjectMembersSshKeysCheckBox;

    @FXML
    private CheckBox expirationWarningCheckBox;
    @FXML
    private NumericTextField expirationWarningTime;


    @FXML
    private CheckBox recoverExpirationWarningCheckBox;
    @FXML
    private NumericTextField recoverExpirationWarningTime;


    @FXML
    private BorderPane root;

    private final JFedGuiPreferences jFedPreferences;

    @Inject
    protected GeneralPane(JFedGuiPreferences jFedPreferences) {
        super("General", false /*advanced */);
        this.jFedPreferences = jFedPreferences;

    }

    @FXML
    private void initialize() {

        expirationWarningTime.disableProperty().bind(expirationWarningCheckBox.selectedProperty().not());

        expirationWarningCheckBox.setSelected(jFedPreferences.isExpirationWarningEnabled());
        expirationWarningTime.setText(String.format("%02d", jFedPreferences.getExpirationWarningTime()));

        recoverExpirationWarningTime.disableProperty().bind(recoverExpirationWarningCheckBox.selectedProperty().not());

        recoverExpirationWarningCheckBox.setSelected(jFedPreferences.isRecoverExpirationWarningEnabled());
        recoverExpirationWarningTime.setText(String.format("%02d", jFedPreferences.getRecoverExpirationWarningTime()));

        includeProjectMembersSshKeysCheckBox.setSelected(jFedPreferences.getAddProjectMembersSshKeysByDefault());
    }

    @Override
    public boolean check() {
        if (expirationWarningCheckBox.isSelected()) {
            try {
                int value = Integer.parseInt(expirationWarningTime.getText());
                if (value < 1) {
                    JFDialogs.create()
                            .owner(root)
                            .message("Expiration warning time must be 1 or more minutes")
                            .showError();
                }

            } catch (NumberFormatException ex) {
                JFDialogs.create()
                        .owner(root)
                        .message("Please enter a valid number.")
                        .showError();
                return false;
            }

        }

        if (recoverExpirationWarningCheckBox.isSelected()) {
            try {
                int value = Integer.parseInt(recoverExpirationWarningTime.getText());
                if (value < 1) {
                    JFDialogs.create()
                            .owner(root)
                            .message("Recover expiration warning time must be 1 or more minutes")
                            .showError();
                }

            } catch (NumberFormatException ex) {
                JFDialogs.create()
                        .owner(root)
                        .message("Please enter a valid number.")
                        .showError();
                return false;
            }

        }

        return true;
    }

    @Override
    public boolean save() {

        jFedPreferences.setExpirationWarningEnabled(expirationWarningCheckBox.isSelected());
        try {
            jFedPreferences.setExpirationWarningTime(Integer.parseInt(expirationWarningTime.getText()));
        } catch (NumberFormatException ex) {
            LOG.warn("Ignoring expiration time setting because the given time is invalid", ex);
        }

        jFedPreferences.setRecoverExpirationWarningEnabled(recoverExpirationWarningCheckBox.isSelected());
        try {
            jFedPreferences.setRecoverExpirationWarningTime(Integer.parseInt(recoverExpirationWarningTime.getText()));
        } catch (NumberFormatException ex) {
            LOG.warn("Ignoring recover expiration time setting because the given time is invalid", ex);
        }

        jFedPreferences.setAddProjectMembersSshKeysByDefault(includeProjectMembersSshKeysCheckBox.isSelected());
        return true;

    }

    @Override
    public Node getRoot() {
        return root;
    }
}
