package pl.bnsit.peaa.temporalobject;


import java.io.Serializable;
import java.sql.Date;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table( name = "orders" )
@NamedQueries({
	@NamedQuery( name="Order.findAll", query="from Order" )
})
public class Order implements Serializable {

	@Id
	@GeneratedValue( strategy=GenerationType.AUTO )
	private Long id;
	
	@Embedded
	private VersionHistory versioningHistory = new VersionHistory();

	@OneToOne(mappedBy="parentOrder")
	private OrderVersion currentVersion;
	
	public void createNewVersion( String productId, String productName, Double price ) {
		OrderVersion orderVersion = new OrderVersion();
		orderVersion.setCustomId( productId );
		orderVersion.setName( productName );
		orderVersion.setPrice( price );
		orderVersion.setParentOrder( this );
		
		versioningHistory.addOrderVersion( orderVersion.getDate(), orderVersion );
		currentVersion = orderVersion;
	}
	//...
	
	public void setCurrentVersionTo( Date when ) {
		currentVersion = versioningHistory.findVersion( when );
	}
	
	protected OrderVersion getCurrentVersion() {
		return currentVersion;
	}
	
	public String getCustomId() {
		return getCurrentVersion().getCustomId();
	}
	
	public void setCustomId( String customId ) {
		getCurrentVersion().setCustomId( customId );
	}

	public String getName() {
		return getCurrentVersion().getName();
	}

	public void setName( String name ) {
		getCurrentVersion().setName( name );
	}
	
	public Double getPrice() {
		return getCurrentVersion().getPrice();
	}
	
	public void setPrice( Double price ) {
		getCurrentVersion().setPrice( price );
	}
	
	public VersionHistory getVersioningHistory() {
		return versioningHistory;
	}
	
	public void setVersioningHistory( VersionHistory versioningHistory ) {
		this.versioningHistory = versioningHistory;
	}
	
	public void setCurrentVersion( OrderVersion currentVersion ) {
		this.currentVersion = currentVersion;
	}

	public Long getId() {
		return id;
	}

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

}
