package no.valg.valgnatt.database.hibernate;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
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 StemmekretsId implements java.io.Serializable {

	@Getter
	@Column(name = "valgaar", nullable = false)
	private Integer valgaar;

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

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

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

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

	public void setValgtype(ValgType valgtype) {
		this.valgtype = valgtype.name();
	}

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

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

	public String id() {
		return valgtype + "-" + valgaar + "-" + kommnr + "-" + kretsnr;
	}

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

}
