/*
 * P3ListImpl
 *
 * Created on 17.12.2008
 * Copyright (c) 2008 Kiesraad
 */
package de.ivu.wahl.result.builder;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import de.ivu.wahl.result.CandidateList;
import de.ivu.wahl.result.Util;
import de.ivu.wahl.result.determination.P2List;
import de.ivu.wahl.result.determination.P3List;
import de.ivu.wahl.result.drawlots.DrawingLotsIdentifier;
import de.ivu.wahl.result.result.GeneralListKey;

/**
 * Immutable implementation of the P3List interface
 */
public class P3ListImpl implements P3List {
  private final Object externalKey;
  private final String name;
  private final List<CandidateList> candidateLists;

  public P3ListImpl(Object externalKey, String name, List<CandidateList> candidateLists) {
    this.externalKey = externalKey;
    this.name = name;
    this.candidateLists = Util.createUnmodifiableCopy(candidateLists);
  }

  @Override
  public Collection<CandidateList> getCandidateLists() {
    return candidateLists;
  }

  @Override
  public Set<P2List> getP2Lists() {
    Set<P2List> p2Lists = new HashSet<>();
    for (CandidateList candidateList : candidateLists) {
      p2Lists.add(candidateList.getP2List());
    }
    return p2Lists;
  }

  @Override
  public List<P2List> getP2ListsSorted() {
    List<P2List> result = Util.asList(getP2Lists());
    result.sort(P2List.SORT_BY_ELECTORAL_DISTRICT_NUMBER);
    return result;
  }

  /**
   * @return Name of the P3-list, typically the name of the political grouping.
   * @see de.ivu.wahl.result.NamedObject#getName()
   */
  @Override
  public String getName() {
    if (name.length() == 0) {
      if (candidateLists == null || candidateLists.isEmpty()) {
        return "Empty blanko P3-List";
      } else {
        return candidateLists.iterator().next().getListNumberOrName();
      }
    } else {
      return name;
    }
  }

  @Override
  public String getListNumberWithName() {
    return getCandidateLists().iterator().next().getListNumberWithName();
  }

  public int getNumber() {
    return getCandidateLists().isEmpty() ? -1 : getCandidateLists().iterator().next().getListNumber();
  }

  public int compareTo(P3List other) {
    return Integer.compare(this.getNumber(), other.getNumber());
  }

  @Override
  public Object getExternalKey() {
    return this.externalKey;
  }

  @Override
  public DrawingLotsIdentifier getIdentifier() {
    return new DrawingLotsIdentifier(this);
  }

  @Override
  public String toString() {
    return getName();
  }

  @Override
  public String getKey() {
    return new GeneralListKey(this).getKey();
  }

}
