package info.bukova.isspst.data;

import info.bukova.isspst.Constants;
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 javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@MappedSuperclass
@Indexed
public class RequirementBase extends BaseData implements FilterableRequirement, SeasonsAware {
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "TYPE_ID")
	private RequirementType type;
	@OneToMany(cascade = CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.TRUE)
	@OrderBy("CENTRE, WORDER")
	private List<Workflow> workflow;
	@Column(name = "NUMSER", unique = true)
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String numser;
	@Column(name = "REQ_DATE")
	private Date reqDate;
	@Column(name = "DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	@Field(index = Index.YES, analyze = Analyze.YES)
	private String description;
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CENTRE_ID")
	private Workgroup centre;
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "WORKGROUP_ID")
	private Workgroup workgroup;
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
	@LazyCollection(LazyCollectionOption.TRUE)
	@OrderBy("ID")
	private List<AuthItem> authorization;
	@Column(name = "STATE")
	@Enumerated(EnumType.ORDINAL)
	private RequirementState state;
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "SEASON_ID")
	private Season season;
	
	public RequirementBase() {
		authorization = new ArrayList<AuthItem>();
		workflow = new ArrayList<Workflow>();
	}

	public RequirementType getType() {
		return type;
	}

	public void setType(RequirementType type) {
		this.type = type;
	}

	public String getNumser() {
		return numser;
	}

	public void setNumser(String number) {
		this.numser = number;
	}

	public Date getReqDate() {
		return reqDate;
	}

	public void setReqDate(Date reqDate) {
		this.reqDate = reqDate;
	}

	public String getDescription()
	{
		return description;
	}

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

	public Workgroup getWorkgroup() {
		return workgroup;
	}

	public void setWorkgroup(Workgroup workgroup) {
		this.workgroup = workgroup;
	}

	public List<AuthItem> getAuthorization() {
		return authorization;
	}

	public void setAuthorization(List<AuthItem> authorization) {
		this.authorization = authorization;
	}

	public RequirementState getState() {
		return state;
	}

	public void setState(RequirementState state) {
		this.state = state;
	}

	public Workgroup getCentre() {
		return centre;
	}

	public void setCentre(Workgroup centre) {
		this.centre = centre;
	}

	public List<Workflow> getWorkflow() {
		return workflow;
	}

	public void setWorkflow(List<Workflow> workflow) {
		this.workflow = workflow;
	}
	
	public User getLastApprover() {
		if (authorization.isEmpty()) {
			return null;
		}
		
		return authorization.get(authorization.size() - 1).getApprover();
	}
	
	public Date getLastApproveDate() {
		if (authorization.isEmpty()) {
			return null;
		}
		
		return authorization.get(authorization.size() - 1).getAuthDate();
	}

	public Season getSeason() {
		return season;
	}

	public void setSeason(Season season) {
		this.season = season;
	}
}
