package info.bukova.isspst.data;

import info.bukova.isspst.Constants;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

@Entity
@Table(name = "REQUIREMENT_ITEMS")
@Indexed
public class RequirementItem
{
	@Id
	@Column(name="ID")
    @GeneratedValue
	private int id;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "REQSUBJECT_ID")
	private RequirementSubject reqSubject;
	
	@ManyToOne
	@JoinColumn(name = "REQUIREMENT_ID")
	private Requirement requirement;

	@Column(name = "CODE")
	private String code;
	
	@Column(name = "NAME")
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String name;

	@Column(name = "TEXTITEM")
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String textItem;

	@Column(name = "QUANTITY", precision=15, scale=4)
	private BigDecimal quantity;
	
	@Embedded
	private MUnitEmb munit;

	@Column(name = "UNITPRICE", precision=15, scale=4)
	private BigDecimal unitPrice;

	@Column(name = "TOTAL", precision=15, scale=4)
	private BigDecimal total;

	@Column(name = "DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String description;
	
	@Column(name = "DELIVERED")
	private Boolean delivered;

	@Column(name = "ORDERNUM")
	private String orderNum;
	
	@Column(name = "PAID")
	private Boolean paid;

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public RequirementSubject getReqSubject()
	{
		return reqSubject;
	}

	public void setReqSubject(RequirementSubject reqSubject)
	{
		this.reqSubject = reqSubject;
	}

	public String getCode()
	{
		return code;
	}

	public void setCode(String code)
	{
		this.code = code;
	}

	public String getName()
	{
		return name;
	}

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

	public String getTextItem()
	{
		return textItem;
	}

	public void setTextItem(String textItem)
	{
		this.textItem = textItem;
	}

	public BigDecimal getQuantity()
	{
		return quantity;
	}

	public void setQuantity(BigDecimal quantity)
	{
		this.quantity = quantity;
	}

	public MUnitEmb getMunit() {
		return munit;
	}

	public void setMunit(MUnitEmb munit) {
		this.munit = munit;
	}

	public BigDecimal getUnitPrice()
	{
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice)
	{
		this.unitPrice = unitPrice;
	}

	public BigDecimal getTotal()
	{
		return total;
	}

	public void setTotal(BigDecimal total)
	{
		this.total = total;
	}

	public String getDescription()
	{
		return description;
	}

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

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

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

				if (equalsId && (thisId == 0))
				{
					// Při zadávání položek do gridu zatím nové položky nemají
					// přiřazeno ID, takže se rozlišují podle reference
					// Při hledání položky v seznamu nebo zjišťování indexu pak
					// byly veškeré položky s ID = 0 a grid nevybíral aktivní
					// položku správně, stejně tak nepřekresloval ani scrollbar
					// a pod.
					return super.equals(obj);
				}
				
				return equalsId;
			}
		}
		
		return false;
	}

	public Requirement getRequirement() 
	{
		return requirement;
	}

	public void setRequirement(Requirement requirement) 
	{
		this.requirement = requirement;
	}

	public Boolean getDelivered() {
		return delivered;
	}

	public void setDelivered(Boolean delivered) {
		this.delivered = delivered;
	}

	public String getOrderNum()
	{
		return orderNum;
	}

	public void setOrderNum(String orderNum)
	{
		this.orderNum = orderNum;
	}

	public Boolean getPaid() {
		return paid;
	}

	public void setPaid(Boolean paid) {
		this.paid = paid;
	}
}
