package info.bukova.isspst.data;

import info.bukova.isspst.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "WORKGROUP")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Workgroup extends Member implements OwnedDataModel, Serializable { 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5039169434782289101L;
	
	@Column(name = "CODE")
	private String code;
	@Column(name = "NAME")
	private String name;
	@ManyToMany(cascade = {CascadeType.ALL})
	@LazyCollection(LazyCollectionOption.TRUE)
	@JoinTable(name="WORKGROUP_MEMBER", joinColumns={@JoinColumn(name="WORKGROUP_ID")}, inverseJoinColumns={@JoinColumn(name="JOBMAPPING_ID")})
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private List<JobMapping> members;
	@Column(name = "CENTRE")
	private boolean centre;
	@Column(name = "REQ_LIMIT", precision = 15, scale = 4)
	private BigDecimal limit;
	//BaseData
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="OWNED_BY_ID")
	private User ownedBy;
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="MODIFIED_BY_ID")
	private User modifiedBy;
	@Transient
	private List<Member> removed;


	public Workgroup() {
		members = new ArrayList<JobMapping>();
		removed = new ArrayList<Member>();
		setParents(new ArrayList<Workgroup>());
		centre = false;
	}
	
	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 boolean isCentre() {
		return centre;
	}

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

	@Override
	public void setOwnedBy(User user) {
		this.ownedBy = user;
	}

	@Override
	public User getOwnedBy() {
		return ownedBy;
	}

	@Override
	public void setModifiedBy(User user) {
		this.modifiedBy = user;
	}

	@Override
	public User getModifiedBy() {
		return modifiedBy;
	}

	@Override
	public String getFullName()
	{
		List<String> list = new ArrayList<String>();

		if (this.code != null)
		{
			list.add(this.code);
		}

		if (this.name != null)
		{
			list.add(this.name);
		}

		String result = StringUtils.join(list, " ");
		return result;
	}

	@Override
	public boolean isPerson() {
		return false;
	}

	public List<JobMapping> getMembers() {
		return members;
	}

	public void setMembers(List<JobMapping> members) {
		this.members = members;
	}
	
	public void removeMember(JobMapping mapping) {
		members.remove(mapping);
	}
	
	public void removeMember(Member member, Role role) {
		JobMapping mapping = new JobMapping(member, role);
		members.remove(mapping);
	}
	
	public boolean equals(Object o) {
		if (o instanceof Workgroup && ((Workgroup)o).getId() == this.getId()) {
			return true;
		}
		return false;
	}

	public static boolean isEqualByWorkgroupForFilter(Workgroup value, Workgroup search)
	{
		if ((search == null) || search.getFullName().isEmpty())
		{
			return true;
		}
		else if (value != null)
		{
			String valueS = value.getFullName();
			String searchS = search.getFullName();
			return (valueS.compareTo(searchS) == 0);
		}

		return false;
	}

	@Override
	public String toString() {
		return getFullName();
	}

	public List<Member> getRemoved() {
		return removed;
	}

	public BigDecimal getLimit() {
		return limit;
	}

	public void setLimit(BigDecimal limit) {
		this.limit = limit;
	}
	
}
