package be.iminds.ilabt.jfed.ui.javafx.probe_gui.command_arguments;

import be.iminds.ilabt.jfed.highlevel.model.ParameterHistoryModel;
import be.iminds.ilabt.jfed.lowlevel.AnyCredential;
import be.iminds.ilabt.jfed.ui.javafx.probe_gui.dialog.CredentialEditDialog;
import be.iminds.ilabt.jfed.ui.javafx.probe_gui.dialog.CredentialViewDialog;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ComboBox;
import javafx.util.StringConverter;

import javax.inject.Inject;
import java.io.IOException;

/**
 * StringArgumentChooser
 */
public class CredentialArgumentChooser extends CommandArgumentChooser<AnyCredential> {
    @FXML
    private ComboBox<AnyCredential> comboBox;

    @Inject
    private final ParameterHistoryModel parameterHistoryModel;

    private final CredentialSubject credentialSubject;

    public CredentialArgumentChooser(ParameterHistoryModel parameterHistoryModel, CredentialSubject credentialSubject) {
        this.parameterHistoryModel = parameterHistoryModel;
        this.credentialSubject = credentialSubject;

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("CredentialArgumentChooser.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();

            assert parameterHistoryModel != null;
            assert comboBox != null;

            comboBox.setConverter(
                    new StringConverter<AnyCredential>() {
                        @Override
                        public String toString(AnyCredential credential) {
                            return credential.getName();
                        }

                        @Override
                        public AnyCredential fromString(String string) {
                            throw new RuntimeException("not supported and not supposed to be used");
                        }
                    });

            switch (credentialSubject) {
                case USER: {
                    comboBox.setItems(parameterHistoryModel.getUserCredentialsList());
                    break;
                }
                case SLICE: {
                    comboBox.setItems(parameterHistoryModel.getSliceCredentialsList());
                    break;
                }
                case ANY: {
                    comboBox.setItems(parameterHistoryModel.getAnyCredentialsList());
                    break;
                }
            }

            if (!comboBox.getItems().isEmpty())
                comboBox.getSelectionModel().selectLast();

            value = comboBox.getSelectionModel().selectedItemProperty();
            assert value != null;
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }

    @FXML
    public void add() {
        ObservableList<AnyCredential> creds = null;
        switch (credentialSubject) {
            case USER: {
                creds = parameterHistoryModel.getUserCredentialsList();
                break;
            }
            case SLICE: {
                creds = parameterHistoryModel.getSliceCredentialsList();
                break;
            }
            case ANY: {
                creds = parameterHistoryModel.getUncategorizedCredentialsList();
                break;
            }
        }
        AnyCredential newCredentialInfo = CredentialEditDialog.showAddDialog();
        if (newCredentialInfo != null)
            creds.add(newCredentialInfo);
    }

    @FXML
    public void view() {
        AnyCredential selected = comboBox.getSelectionModel().selectedItemProperty().get();
        if (selected != null)
            CredentialViewDialog.showDialog(selected);
    }
}
