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

import be.iminds.ilabt.jfed.experiment.ExperimentControllerListener;
import be.iminds.ilabt.jfed.highlevel.jobs.Job;
import be.iminds.ilabt.jfed.highlevel.jobs.AllocateExperimentJob;
import be.iminds.ilabt.jfed.highlevel.jobs.State;
import be.iminds.ilabt.jfed.highlevel.jobs.report.StitchingJobReport;
import com.google.common.collect.EvictingQueue;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Collection;
import java.util.Queue;

/**
 * User: twalcari
 * Date: 12/15/2015
 * Time: 17:55
 */
@Singleton
public class BugReportExperimentControllerListener implements ExperimentControllerListener {

    /**
     * Only keep the last two stitching reports in memory
     */
    private final Queue<StitchingJobReport> stitchingJobReports = EvictingQueue.create(2);


    @Inject
    public BugReportExperimentControllerListener() {
    }


    @Override
    public void onJobSubmitted(Job<?> job) {
        synchronized (stitchingJobReports) {
            if (job instanceof AllocateExperimentJob) {
                stitchingJobReports.add(((AllocateExperimentJob) job).getStitchingJobReport());
            }
        }

    }

    @Override
    public void onJobStateChanged(Job<?> job, State state) {

    }

    @Override
    public void onJobFinished(Job<?> job) {

    }

    public Collection<StitchingJobReport> getStitchingJobReports() {
        return stitchingJobReports;
    }
}
