package info.bukova.isspst.data;

import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "REQUIREMENT")
@Indexed
public class Requirement extends RequirementBase
{
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "requirement", cascade = CascadeType.ALL, orphanRemoval = true)
	@IndexedEmbedded
	private List<RequirementItem> items;

	@Column(name = "DELIVERYDATE")
	private Date deliveryDate;

	@Column(name = "SUMTOTAL")
	private BigDecimal sumTotal;

	@Column(name = "KIND")
	private Long kind;

	@Column(name = "PROJECT")
	private Boolean project;

	public Requirement()
	{
		this.setItems(new ArrayList<RequirementItem>());
	}

	public List<RequirementItem> getItems()
	{
		return items;
	}
	
	public void addItem(RequirementItem item)
	{
		item.setRequirement(this); // nutné nastavit kvůli obousměrné vazbě. Na položce v DB se jinak nenastaví ID hlavního záznamu
		items.add(item);
	}

	public void setItems(List<RequirementItem> items)
	{
		this.items = items;
	}

	public Date getDeliveryDate()
	{
		return deliveryDate;
	}

	public void setDeliveryDate(Date deliveryDate)
	{
		this.deliveryDate = deliveryDate;
	}

	public BigDecimal getSumTotal()
	{
		return sumTotal;
	}

	public void setSumTotal(BigDecimal sumTotal)
	{
		this.sumTotal = sumTotal;
	}

	public Long getKind()
	{
		return kind;
	}

	public void setKind(Long kind)
	{
		this.kind = kind;
	}

	public Boolean getProject()
	{
		return project;
	}

	public void setProject(Boolean project)
	{
		this.project = project;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj != null)
		{
			if (obj instanceof Requirement)
			{
				Requirement item = (Requirement) obj;

				int thisId = this.getId();
				int itemId = item.getId();
				boolean equalsId = (thisId == itemId);

				if (equalsId && (thisId == 0))
				{
					return super.equals(obj);
				}

				return equalsId;
			}
		}

		return false;
	}
}
