package be.iminds.ilabt.jfed.highlevel.jobs.report;

import be.iminds.ilabt.jfed.call_log_output.LogOutput;
import be.iminds.ilabt.jfed.experiment.tasks.ExperimentTaskStatus;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;
import be.iminds.ilabt.jfed.highlevel.jobs.State;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.time.Instant;

/**
 * User: twalcari
 * Date: 12/9/2015
 * Time: 16:03
 */
public class JobStateReport {

    private final String stateName;
    private final Instant startTime;
    private final ObjectProperty<Instant> endTime = new SimpleObjectProperty<>();

    private final ObservableList<TaskExecution<?>> taskExecutions = FXCollections.observableArrayList();
    private final ObjectProperty<ExperimentTaskStatus> finalStatus = new SimpleObjectProperty<>();

    private final ObservableList<LogOutput.LogEntry> logLines = FXCollections.synchronizedObservableList(FXCollections.observableArrayList());


    public JobStateReport(State state) {
        this.stateName = state.getName();

        this.startTime = Instant.now();
    }

    public void registerEnd(ExperimentTaskStatus finalStatus) {
        this.endTime.set(Instant.now());
        this.finalStatus.set(finalStatus);

    }

    public void addTaskExecution(TaskExecution<?> taskExecution) {
        taskExecutions.add(taskExecution);
    }

    public void addLogLine(LogOutput.LogEntry logEntry) {
        this.logLines.add(logEntry);
    }

    public String getStateName() {
        return stateName;
    }

    public Instant getStartTime() {
        return startTime;
    }

    public Instant getEndTime() {
        return endTime.get();
    }

    public ObjectProperty<Instant> endTimeProperty() {
        return endTime;
    }

    public ObservableList<TaskExecution<?>> getTaskExecutions() {
        return taskExecutions;
    }

    public ExperimentTaskStatus getFinalStatus() {
        return finalStatus.get();
    }

    public ObjectProperty<ExperimentTaskStatus> finalStatusProperty() {
        return finalStatus;
    }

    public ObservableList<LogOutput.LogEntry> getLogLines() {
        return logLines;
    }
}
