package no.valg.valgnatt.database.hibernate;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import no.valg.valgnatt.database.domene.ValgType;
import org.hibernate.annotations.Immutable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
@Immutable
@NoArgsConstructor //Hibernate-krav
@AllArgsConstructor //Builder-krav
@Builder
public class PartilisteId implements java.io.Serializable, Comparable<PartilisteId> {

	@Column(name = "VALGTYPE", nullable = false)
	private String valgtype;

	@Getter
	@Column(name = "PARTIKODE", nullable = false, length = 60)
	private String partikode;

	public PartilisteId(ValgType valgtype, String partikode) {
		this.valgtype = valgtype.name();
		this.partikode = partikode;
	}

	@Override
	public boolean equals(Object other) {
		return other != null && other.getClass() == this.getClass() && id().equals(((PartilisteId) other).id());
	}

	public String id() {
		return valgtype + "-" + partikode;
	}

	@Override
	public int hashCode() {
		return id().hashCode();
	}

	@Override
	public int compareTo(PartilisteId o) {
		int i = valgtype.compareTo(o.getValgtype().name());
		return i != 0 ? i : partikode.compareTo(o.getPartikode());
	}

	public ValgType getValgtype() {
		return ValgType.valueOf(this.valgtype);
	}

}
