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

import be.iminds.ilabt.jfed.experimenter_gui.preferences.AbstractPreferencesSubPane;
import be.iminds.ilabt.jfed.preferences.JFedGuiPreferences;
import be.iminds.jfed.gts_highlevel.GtsModule;
import be.iminds.jfed.gts_highlevel.GtsPreferenceKey;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.net.MalformedURLException;
import java.net.URL;

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

    @FXML
    private RadioButton productionRadioButton;
    @FXML
    private RadioButton lab1RadioButton;
    @FXML
    private RadioButton lab2RadioButton;
    @FXML
    private RadioButton customRadioButton;

    @FXML
    private TextField customEndpointTextfield;

    @FXML
    private BorderPane root;


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

    private final JFedGuiPreferences jFedPreferences;

    @Inject
    GtsPane(JFedGuiPreferences jFedPreferences) {
        super("GTS Settings", true/*advanced*/);
        this.jFedPreferences = jFedPreferences;
    }

    @FXML
    private void initialize() {
        customEndpointTextfield.visibleProperty().bind(customRadioButton.selectedProperty());

        //load settings
        String gtsEndpointPref = jFedPreferences.getString(GtsPreferenceKey.PREF_GTS_ENDPOINT);

        if(gtsEndpointPref != null) {
            switch (gtsEndpointPref) {
                case GtsModule.PRODUCTION_GTS:
                    productionRadioButton.setSelected(true);
                    break;
                case GtsModule.LAB1_GTS:
                    lab1RadioButton.setSelected(true);
                    break;
                case GtsModule.LAB2_GTS:
                    lab2RadioButton.setSelected(true);
                    break;
                default:
                    customRadioButton.setSelected(true);
                    customEndpointTextfield.setText(gtsEndpointPref);
                    break;
            }
        }else{
            productionRadioButton.setSelected(true);
        }

    }


    @Override
    public boolean check() {
        if (customRadioButton.isSelected()) {
            try {
                new URL(customEndpointTextfield.getText());
            } catch (MalformedURLException e) {
                new Alert(Alert.AlertType.ERROR, "The custom GTS endpoint is not a valid URL!").showAndWait();
                return false;
            }
        }

        return true;
    }

    public boolean save() {
        if (!check()) return false;

        if (productionRadioButton.isSelected()) {
            jFedPreferences.setString(GtsPreferenceKey.PREF_GTS_ENDPOINT, GtsModule.PRODUCTION_GTS);
        } else if (lab1RadioButton.isSelected()) {
            jFedPreferences.setString(GtsPreferenceKey.PREF_GTS_ENDPOINT, GtsModule.LAB1_GTS);
        }else if (lab2RadioButton.isSelected()) {
            jFedPreferences.setString(GtsPreferenceKey.PREF_GTS_ENDPOINT, GtsModule.LAB2_GTS);
        }else  {
            assert customRadioButton.isSelected();
            jFedPreferences.setString(GtsPreferenceKey.PREF_GTS_ENDPOINT, customEndpointTextfield.getText());
        }

        return true;
    }
}
