package be.iminds.ilabt.jfed.experimenter_gui.slice;

import be.iminds.ilabt.jfed.experiment.tasks.ExperimentTaskStatus;
import be.iminds.ilabt.jfed.experimenter_gui.slice.state_listeners.CreateDiskImageStateListener;
import be.iminds.ilabt.jfed.highlevel.jobs.CreateDiskImageJob;
import be.iminds.ilabt.jfed.highlevel.jobs.SingleTaskJob;
import be.iminds.ilabt.jfed.highlevel.jobs.State;
import be.iminds.ilabt.jfed.highlevel.tasks.OpenConsoleTask;
import be.iminds.ilabt.jfed.ui.javafx.dialogs.JFDialogs;
import javafx.scene.control.Alert;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * User: twalcari
 * Date: 11/26/2015
 * Time: 17:39
 */
@Singleton
public class ProgressItemCustomDetailsViews {


    @Inject
    public ProgressItemCustomDetailsViews() {
    }


    public CustomStateDetailsView<? extends State> getCustomDetailsView(State state) {

        if (state instanceof CreateDiskImageJob.CreateImageState) {
            if (state.getStatus() == ExperimentTaskStatus.SUCCESS)
                return (CustomStateDetailsView<CreateDiskImageJob.CreateImageState>) state1 -> CreateDiskImageStateListener.showImageInfoDialog(state1.getTask());
        }

        if (state instanceof SingleTaskJob.ExecuteTaskState &&
                ((SingleTaskJob.ExecuteTaskState) state).getTask() instanceof OpenConsoleTask) {
            if (state.getStatus() == ExperimentTaskStatus.SUCCESS) {
                OpenConsoleTask task = (OpenConsoleTask) ((SingleTaskJob.ExecuteTaskState) state).getTask();

                if (((SingleTaskJob.ExecuteTaskState) state).getTaskExecution().isCompleted()) {
                    if (task.getUrl() != null) {
                        JFDialogs.create()
                                .message("Console has been opened in browser. URL is: " +
                                        task.getUrl().toExternalForm()
                                ).allowCopyMessage()
                                .showWarning();
                    } else {
                        new Alert(Alert.AlertType.ERROR, "No console URL received!").show();
                    }
                } else {
                    new Alert(Alert.AlertType.INFORMATION, "Console URL is being requested").show();
                }
            }
        }


        return null;
    }
}
