/*
 * DrawingLotsIdentifier
 *
 * Created on 26.08.2009
 * Copyright (c) 2009 Kiesraad
 */
package de.ivu.wahl.result.drawlots;

import de.ivu.wahl.result.Candidate;
import de.ivu.wahl.result.P3ListCandidate;
import de.ivu.wahl.result.determination.CandidateForSorting;
import de.ivu.wahl.result.determination.P2List;
import de.ivu.wahl.result.determination.P3List;

/**
 * A DrawingLotsIdentifier is a view on an DrawingLotsAlternative by showing Objects that are
 * identifyable from external (from the persistency layer).
 *
 */
public class DrawingLotsIdentifier {
  private final P3List p3List;
  private final P2List p2List;
  private final Candidate candidate;

  public DrawingLotsIdentifier(P3List p3List) {
    this.p3List = p3List;
    this.p2List = null;
    this.candidate = null;
  }

  public DrawingLotsIdentifier(P2List p2List) {
    this.p3List = null;
    this.p2List = p2List;
    this.candidate = null;
  }

  public DrawingLotsIdentifier(CandidateForSorting candidateForSorting) {
    this.p3List = candidateForSorting.getP3List();
    this.p2List = null;
    this.candidate = candidateForSorting.getCandidate();
  }

  public DrawingLotsIdentifier(P3ListCandidate p3ListCandidate) {
    this.p3List = p3ListCandidate.getP3List();
    this.p2List = null;
    this.candidate = p3ListCandidate.getCandidate();
  }

  public Object getExternalP2ListKey() {
    return p2List == null ? null : p2List.getExternalKey();
  }

  public Object getExternalP3ListKey() {
    return p3List == null ? null : p3List.getExternalKey();
  }

  public Object getExternalCandidateKey() {
    return candidate == null ? null : candidate.getExternalKey();
  }

  @Override
  public String toString() {
    String comma = ", "; //$NON-NLS-1$
    return "DrawingLotsIdentifier(" + p3List + comma + p2List + comma //$NON-NLS-1$
            + candidate + ")"; //$NON-NLS-1$
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((candidate == null) ? 0 : candidate.hashCode());
    result = prime * result + ((p2List == null) ? 0 : p2List.hashCode());
    result = prime * result + ((p3List == null) ? 0 : p3List.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    DrawingLotsIdentifier other = (DrawingLotsIdentifier) obj;
    if (candidate == null) {
      if (other.candidate != null) {
        return false;
      }
    } else if (!candidate.equals(other.candidate)) {
      return false;
    }
    if (p2List == null) {
      if (other.p2List != null) {
        return false;
      }
    } else if (!p2List.equals(other.p2List)) {
      return false;
    }
    if (p3List == null) {
      if (other.p3List != null) {
        return false;
      }
    } else if (!p3List.equals(other.p3List)) {
      return false;
    }
    return true;
  }

  public String getId() {
    String emptyString = ""; //$NON-NLS-1$
    if (candidate != null) {
      return candidate.getName();
    } else if (p3List != null) {
      return p3List.getKey();
    } else if (p2List != null) {
      return p2List.getKey();
    } else {
      return emptyString;
    }
  }
}
