package be.iminds.ilabt.jfed.highlevel.history;

import be.iminds.ilabt.jfed.call_log_output.SerializableApiCallDetails;
import be.iminds.ilabt.jfed.call_log_output.SerializableApiCallDetailsFactory;
import be.iminds.ilabt.jfed.log.ApiCallDetails;
import be.iminds.ilabt.jfed.log.Logger;
import be.iminds.ilabt.jfed.log.ResultListener;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

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

/**
 * ApiCallHistory
 */
@Singleton
public class ApiCallHistory implements ResultListener {
    private final SerializableApiCallDetailsFactory serializableApiCallDetailsFactory;

    @Inject
    public ApiCallHistory(Logger logger,
                          SerializableApiCallDetailsFactory serializableApiCallDetailsFactory) {
        logger.addResultListener(this);
        this.serializableApiCallDetailsFactory = serializableApiCallDetailsFactory;
    }

    final ObservableList<SerializableApiCallDetails> history = FXCollections.observableArrayList();

    public ObservableList<SerializableApiCallDetails> getHistory() {
        return history;
    }

    @Override
    public void onResult(final ApiCallDetails result) {
        //this may be added asynchronously
        Platform.runLater(() -> history.add(
                serializableApiCallDetailsFactory.createFromApiCallDetails(result)
        ));
    }
}
