package pl.bnsit.peaa.temporalobject;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;

@Embeddable
public class VersionHistory implements Serializable {
	
	@OneToMany(cascade=CascadeType.ALL)
	@MapKey( name="date" )
//	@JoinTable( 
//			name="join_version_history__order_version",
//			joinColumns=@JoinColumn(name="ref_order_history"),
//			inverseJoinColumns=@JoinColumn(name="ref_order_version"))
    @JoinColumn( name="ref_order_history" )			
	private Map<Date, OrderVersion> orderVersions
		= new HashMap<Date, OrderVersion>();
	
	@OneToMany
	@JoinColumn( name="ref_order_hour_milestone" )
	private List<OrderVersion> orderHourMilestones = new ArrayList<OrderVersion>();
	
	public OrderVersion findTheOldest() {
		
		final Date maxKey = Utils.findMaxDate( orderVersions );
		
		return orderVersions.get( maxKey );
	}

	public OrderVersion findVersion( Date date ) {
		return orderVersions.get( date );
	}
	
	public void addOrderVersion( Date date, OrderVersion version ) {
		orderVersions.put( date , version );
	}
	
	public void createHourMilestone() {
		//...
	}

	
	public Map<Date, OrderVersion> getOrderVersions() {
	
		return orderVersions;
	}

	
	public List<OrderVersion> getOrderHourMilestones() {
	
		return orderHourMilestones;
	}

	
	public void setOrderVersions( Map<Date, OrderVersion> orderVersions ) {
	
		this.orderVersions = orderVersions;
	}

	
	public void setOrderHourMilestones( List<OrderVersion> orderHourMilestones ) {
	
		this.orderHourMilestones = orderHourMilestones;
	}

}
