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 MandaterId implements java.io.Serializable {

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

	@Getter
	@Column(name = "valgdistriktnr", nullable = false, length = 16)
	private String valgdistriktnr;

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

	@Getter
	@Column(name = "prognose", length = 4)
	private String prognose;
	
	public ValgType getValgtype() {
		return ValgType.valueOf(this.valgtype);
	}

	public void setPrognose(boolean prognose) {
		this.prognose = prognose ? "1" : "0";
	}

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

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

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

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

}
