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

import be.iminds.ilabt.jfed.experimenter_gui.preferences.AbstractPreferencesSubPane;
import be.iminds.ilabt.jfed.preferences.GuiPreferenceKey;
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.control.TextArea;
import javafx.scene.layout.BorderPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;

/**
 * TerminalCommandPanel
 */
public class SshCommandPane extends AbstractPreferencesSubPane {
    private final static Logger LOG = LoggerFactory.getLogger(SshCommandPane.class);

    @FXML protected CheckBox autoCheckBox;
    @FXML private TextArea sshCommandTextArea;
    @FXML private TextArea sshProxyCommandTextArea;
    @FXML private CheckBox sshagentCheckBox;
    @FXML
    private BorderPane root;

    private final JFedGuiPreferences jFedPreferences;

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

    @Inject
    SshCommandPane(JFedGuiPreferences jFedPreferences) {
        super("SSH Commands", false/*advanced*/);
        this.jFedPreferences = jFedPreferences;
    }

    @FXML
    private void initialize() {
        sshCommandTextArea.disableProperty().bind(autoCheckBox.selectedProperty());
        sshProxyCommandTextArea.disableProperty().bind(autoCheckBox.selectedProperty());

        setFromPreferences();
    }

    private void setFromPreferences() {
        sshagentCheckBox.setSelected(jFedPreferences.getBoolean(GuiPreferenceKey.PREF_SSHAGENT_USE));

        setCommandsFromPreferences();

        boolean auto = jFedPreferences.getBoolean(GuiPreferenceKey.PREF_COMMAND_SSH_AUTO, true);
        autoCheckBox.setSelected(auto);
    }

    private void setCommandsFromPreferences() {
        sshCommandTextArea.setText(jFedPreferences.getString(GuiPreferenceKey.PREF_COMMAND_SSH));
        sshProxyCommandTextArea.setText(jFedPreferences.getString(GuiPreferenceKey.PREF_COMMAND_SSH_PROXY));
    }

    private static boolean containsAll(String t, String ... mustContain) {
        for (String c : mustContain) {
            if (!t.contains(c))
                return false;
        }
        return true;
    }

    @Override
    public boolean check() {
        if (autoCheckBox.isSelected()) return true;

        if (!containsAll(sshCommandTextArea.getText(), "$i", "$h", "$p", "$u")) {
            JFDialogs.create()
                    .owner(root)
                    .message("Your SSH command must contain the placeholders: $i $u $h $p")
                    .showError();
            return false;
        }
        if (!containsAll(sshProxyCommandTextArea.getText(), "$i", "$h", "$p", "$u", "$I", "$H", "$P", "$U")) {
            JFDialogs.create()
                    .owner(root)
                    .message("Your SSH command must contain the placeholders: $i $u $h $p $I $U $H $P")
                    .showError();
            return false;
        }

        return true;
    }
    @Override
    public boolean save() {
        jFedPreferences.setBoolean(GuiPreferenceKey.PREF_COMMAND_SSH_AUTO, autoCheckBox.isSelected());
        jFedPreferences.setBoolean(GuiPreferenceKey.PREF_SSHAGENT_USE, sshagentCheckBox.isSelected());

        if (!autoCheckBox.isSelected()) {
            jFedPreferences.setString(GuiPreferenceKey.PREF_COMMAND_SSH, sshCommandTextArea.getText());
            jFedPreferences.setString(GuiPreferenceKey.PREF_COMMAND_SSH_PROXY, sshProxyCommandTextArea.getText());
        } else
            jFedPreferences.fillinBlanks();

        return true;
    }
}
