package de.hsadmin.mods.pac;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.LAZY;
import static javax.persistence.GenerationType.SEQUENCE;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity(name = "BasePacs")
@Table(name = "basepacket")
@SequenceGenerator(name = "BasePacsSeqGen", sequenceName = "basepacket_basepacket_id_seq")
public class BasePac implements Serializable {
	
	private static final long serialVersionUID = 1491121619195513435L;

	public BasePac() {
	}

	public BasePac(String name, String desc, int sortPos) {
		this.name = name;
		this.description = desc;
		this.sorting = sortPos;
		this.valid = true;
	}

	@Id
	@GeneratedValue(strategy = SEQUENCE, generator = "BasePacsSeqGen")
	@Column(name = "basepacket_id", columnDefinition = "integer")
	private long basePacId;

	@Column(name = "basepacket_code", columnDefinition = "character varying(10)")
	private String name;

	@Column(name = "description", columnDefinition = "character varying(100)")
	private String description;

	@Column(name = "sorting", columnDefinition = "integer")
	private int sorting;

	@Column(name = "valid", columnDefinition = "boolean")
	private boolean valid;

	@OneToMany(fetch = LAZY, cascade = ALL)
	private Set<Component> components;

	@OneToMany(fetch = LAZY, cascade = ALL)
	@JoinTable(name = "packet_component", joinColumns = @JoinColumn(name = "packet_id"), inverseJoinColumns = @JoinColumn(name = "basepacket_id"))
	private Set<Pac> pacs;

	public long id() {
		return basePacId;
	}

	public long getBasePacId() {
		return basePacId;
	}

	public void setBasePacId(long id) {
		this.basePacId = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getSorting() {
		return sorting;
	}

	public void setSorting(int sorting) {
		this.sorting = sorting;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public Set<Component> getComponents() {
		return components != null ? components
				: (components = new HashSet<Component>());
	}

	public void setComponents(Set<Component> components) {
		this.components = components;
	}

	public Component getComponent(String feature) {
		for (Component comp : getComponents())
			if (feature.equals(comp.getBaseComponent().getFeature()))
				return comp;
		return null;
	}

	public void addComponent(Component comp) {
		getComponents().add(comp);
	}

	public void removeComponent(Component comp) {
		getComponents().remove(comp);
	}

	public Set<Pac> getPacs() {
		return pacs;
	}

	public void setPacs(Set<Pac> pacs) {
		this.pacs = pacs;
	}

	// generic
	public boolean equals(Object aOther) {
		if (aOther == null || aOther.getClass() != getClass())
			return false;
		BasePac aOtherBP = (BasePac) aOther;
		if (basePacId != aOtherBP.basePacId)
			return false;
		if (name != null && !name.equals(aOtherBP.name))
			return false;
		if (description != null && description.equals(aOtherBP.description))
			return false;
		if (sorting != aOtherBP.sorting)
			return false;
		if (valid != aOtherBP.valid)
			return false;
		return true;
	}
}
