package com.onthegomap.planetiler.collection;

import java.util.Arrays;
import java.util.Comparator;

public record SortableFeature(@Override long key, byte[] value) implements Comparable<SortableFeature>, HasLongSortKey {
  public static final Comparator<SortableFeature> COMPARE_BYTES = (a, b) -> Arrays.compareUnsigned(a.value, b.value);

  @Override
  public int compareTo(SortableFeature o) {
    if (key < o.key) {
      return -1;
    } else if (key == o.key) {
      return Arrays.compareUnsigned(value, o.value);
    } else {
      return 1;
    }
  }

  @Override
  public String toString() {
    return "SortableFeature{" +
      "key=" + key +
      ", value=" + Arrays.toString(value) +
      '}';
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    SortableFeature entry = (SortableFeature) o;

    if (key != entry.key) {
      return false;
    }
    return Arrays.equals(value, entry.value);
  }

  @Override
  public int hashCode() {
    int result = (int) (key ^ (key >>> 32));
    result = 31 * result + Arrays.hashCode(value);
    return result;
  }
}
