/*
 * P2List
 *
 * Created on 02.03.2009
 * Copyright (c) 2009 Kiesraad
 */
package de.ivu.wahl.result.determination;

import de.ivu.wahl.result.Candidate;
import de.ivu.wahl.result.CandidateList;
import de.ivu.wahl.result.drawlots.DrawingLotsAlternative;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * @author , IVU Traffic Technologies AG
 */
public interface P2List extends Comparable<P2List>, GeneralList, DrawingLotsAlternative {
  Comparator<? super P2List> SORT_BY_ELECTORAL_DISTRICT_NUMBER = Comparator.comparing(
          P2List::getElectoralDistrictNumber);

  Set<CandidateList> getCandidateLists();

  default List<Candidate> getCandidates() {
    return getCandidateLists().isEmpty() ? new ArrayList<>() : getFirstCandidateList().getCandidates();
  }

  default CandidateList getFirstCandidateList() {
    return getCandidateLists().iterator().next();
  }

  default int getElectoralDistrictNumber() {
    int min = 999;
    for (CandidateList candidateList : getCandidateLists()) {
      min = Math.min(min, candidateList.getElectoralDistrict().getNumber());
    }
    return min;
  }

  /**
   * @return a String displaying the electoral district numbers like "2" or "3 en 4" or
   * "2, 4, 6 en 8"
   */
  String getElectoralDistrictNumbers();

  default String getListAndDistrictNumber() {
    return getListNumber() + "_" + getElectoralDistrictNumber();
  }

  default int getListNumber() {
    return getCandidateLists().iterator().next().getListNumber();
  }

  default boolean isStel() {
    return getCandidateLists().size() > 1;
  }
}