001package edu.pdx.cs410J.grader.poa;
002
003import com.google.common.eventbus.EventBus;
004import com.google.common.eventbus.Subscribe;
005import com.google.inject.Inject;
006
007import java.util.ArrayList;
008import java.util.List;
009import java.util.stream.Collectors;
010
011public class POASubmissionsPresenter {
012  private final EventBus bus;
013  private final POASubmissionsView view;
014  private final List<POASubmission> submissions = new ArrayList<>();
015
016  private int selectedSubmissionIndex = 0;
017
018  @Inject
019  public POASubmissionsPresenter(EventBus bus, POASubmissionsView view) {
020    this.bus = bus;
021    this.view = view;
022
023    this.view.addSubmissionSelectedListener(this::poaSubmissionSelected);
024    this.view.addDownloadSubmissionsListener(this::fireDownloadSubmissionsEvent);
025
026    this.bus.register(this);
027  }
028
029  private void fireDownloadSubmissionsEvent() {
030    this.bus.post(new DownloadPOASubmissionsRequest());
031  }
032
033  private void poaSubmissionSelected(int index) {
034    this.selectedSubmissionIndex = index;
035    this.bus.post(new POASubmissionSelected(this.submissions.get(index)));
036  }
037
038  @Subscribe
039  public void displaySubmission(POASubmission submission) {
040    this.submissions.add(submission);
041    view.setPOASubmissionsDescriptions(getSubmissionsDescriptions());
042  }
043
044  private List<String> getSubmissionsDescriptions() {
045    return submissions.stream().map(POASubmission::getSubject).collect(Collectors.toList());
046  }
047
048  @Subscribe
049  public void selectNextPOA(SelectNextPOAEvent event) {
050    int nextIndex = this.selectedSubmissionIndex + 1;
051
052    if (nextIndex < this.submissions.size()) {
053      view.selectPOASubmission(nextIndex);
054      poaSubmissionSelected(nextIndex);
055    }
056  }
057}