/*
 * AssignmentKey
 *
 * Created on 09.03.2009
 * Copyright (c) 2009 IVU Traffic Technologies AG
 */
package de.ivu.wahl.result.result;

import java.util.Objects;

/**
 * Key for storing assignments in a map.
 *
 * @author , IVU Traffic Technologies AG
 */
public class AssignmentKey implements Comparable<AssignmentKey> {
  private final int index;
  private final GeneralListKey listKey;
  private final GeneralListKey assignmentParentKey;

  public AssignmentKey(Assignment assignment) {
    this(assignment.getIndex(), GeneralListKey.from(assignment.getList()), GeneralListKey.from(assignment.getAssignmentParent()));
  }

  public AssignmentKey(int index, GeneralListKey listKey, GeneralListKey assignmentParentKey) {
    this.index = index;
    this.listKey = listKey;
    this.assignmentParentKey = assignmentParentKey;
  }

  public int getIndex() {
    return index;
  }

  public GeneralListKey getListKey() {
    return listKey;
  }

  public GeneralListKey getAssignmentParentKey() {
    return assignmentParentKey;
  }

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

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    AssignmentKey that = (AssignmentKey) o;
    return index == that.index &&
            Objects.equals(listKey, that.listKey) &&
            Objects.equals(assignmentParentKey, that.assignmentParentKey);
  }

  public int compareTo(AssignmentKey other) {
    int result = this.assignmentParentKey.compareTo(other.assignmentParentKey);
    if (result != 0) {
      return result;
    }
    result = Integer.compare(this.index, other.index);
    if (result != 0) {
      return result;
    }
    return this.listKey.compareTo(other.listKey);
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(index);
    builder.append(". assignment");
    if (!assignmentParentKey.isP3Parent()) {
      builder.append(" in List ");
      builder.append(assignmentParentKey.getKey());
    }
    builder.append(" to List ");
    builder.append(listKey.getKey());

    return builder.toString();
  }

  public String print() {
    return index + "\t" + getAssignmentParentKey().getKey();
  }

}
