package info.bukova.isspst.data;

import info.bukova.isspst.storage.EntityWithAttachment;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
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;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
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 = "TRIPREQUIREMENT")
@Indexed
public class TripRequirement extends RequirementBase implements EntityWithAttachment {
	
	@Column(name = "TRIP_FROM")
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String from;
	@Column(name = "TRIP_TO")
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String to;
	@Column(name = "TRIP_DATE")
	private Date tripDate;
	@Column(name = "END")
	private String end;
	@Column(name = "END_DATE")
	private Date endDate;
	@ManyToMany
	@LazyCollection(LazyCollectionOption.TRUE)
	@JoinTable(name="TRIPREQUIREMENT_PASSANGER", joinColumns={@JoinColumn(name="TRIPREQUIREMENT_ID")}, inverseJoinColumns={@JoinColumn(name="USER_ID")})
	private List<User> passengers;

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

	@Embedded
	private Vehicle vehicle;
	@Column(name = "REQUIRE_DOWN_PAYMENT")
	private Boolean requireDownPayment;
	@Column(name = "DOWN_PAYMENT", precision = 15, scale = 4)
	private BigDecimal downPayment;
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
	@LazyCollection(LazyCollectionOption.TRUE)
	@IndexedEmbedded
	private List<FileMetainfo> attachedFiles;
	@Column(name = "BILL_FOR_PASSENGERS")
	private Boolean billForPassengers;

	public TripRequirement() {
		this.setOwnedBy(new User());
		passengers = new ArrayList<User>();
		attachedFiles = new ArrayList<FileMetainfo>();
	}
	
	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public Date getTripDate() {
		return tripDate;
	}

	public void setTripDate(Date tripDate) {
		this.tripDate = tripDate;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public List<User> getPassengers() {
		return passengers;
	}

	public void setPassengers(List<User> passengers) {
		this.passengers = passengers;
	}

	public String getForeignPersons()
	{
		return foreignPersons;
	}

	public void setForeignPersons(String foreignPersons)
	{
		this.foreignPersons = foreignPersons;
	}

	public Vehicle getVehicle() {
		return vehicle;
	}

	public void setVehicle(Vehicle vehicle) {
		this.vehicle = vehicle;
	}

	public Boolean getRequireDownPayment() {
		return requireDownPayment;
	}

	public void setRequireDownPayment(Boolean requireDownPayment) {
		this.requireDownPayment = requireDownPayment;
	}

	public BigDecimal getDownPayment() {
		return downPayment;
	}

	public void setDownPayment(BigDecimal downPayment) {
		this.downPayment = downPayment;
	}

	@Override
	public List<FileMetainfo> getAttachedFiles() {
		return attachedFiles;
	}

	public void setAttachedFiles(List<FileMetainfo> attachedFiles) {
		this.attachedFiles = attachedFiles;
	}

	@Override
	public void addAttachment(FileMetainfo metaInfo) {
		attachedFiles.add(metaInfo);
	}

	@Override
	public void removeAttachment(FileMetainfo metainfo) {
		attachedFiles.remove(metainfo);
	}

	public Boolean getBillForPassengers() {
		return billForPassengers;
	}

	public void setBillForPassengers(Boolean billForPassengers) {
		this.billForPassengers = billForPassengers;
	}
}
