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

import be.iminds.ilabt.jfed.call_log_output.SerializableApiCallDetails;
import be.iminds.ilabt.jfed.ui.javafx.util.StageUtils;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.log.cache.ApiCallDetailsRef;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;

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

/**
 * User: twalcari
 * Date: 3/11/14
 * Time: 2:16 PM
 */
@Singleton
public class TasksWindow {
    private static final String TASKS_PANEL_FXML = "Calls.fxml";
    private final Stage callsStage;
    private final TasksPanelController controller;

    @Inject
    TasksWindow(FXMLLoader loader) {

        //setup stage
        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        callsStage = new Stage();
        StageUtils.setJFedStageIcons(callsStage);
        StageUtils.fillPrimaryScreen(callsStage);
        callsStage.setTitle("jFed Calls Overview");

        TasksPanelController controller = null;

        //load TasksPanel
        try {
            loader.setLocation(TasksWindow.class.getResource(TASKS_PANEL_FXML));
            callsStage.setScene(new Scene(loader.load()));
            controller = loader.getController();
        } catch (IOException ex) {
            throw new RuntimeException("Error while loading TasksWindow", ex);
        }

        this.controller = controller;
    }

    public synchronized void showTasks() {
        assert callsStage != null;
        callsStage.show();
    }

    public void showTaskExecution(TaskExecution taskExecution) {
        controller.showTaskExecution(taskExecution);
        callsStage.show();
    }

    public void showApiCall(SerializableApiCallDetails apiCallDetails) {
        controller.showApiCall(apiCallDetails.getId());
        callsStage.show();
    }

    public void showApiCall(ApiCallDetailsRef apiCallDetailsRef) {
        controller.showApiCall(apiCallDetailsRef.getId());
        callsStage.show();
    }
}
