package eu.dnetlib.domain.functionality;

import java.util.Date;

import eu.dnetlib.domain.SearchCriteria;
import eu.dnetlib.domain.SearchCriteriaImpl;


public class RecommendationSearchCriteria extends SearchCriteriaImpl implements
		SearchCriteria {
	
	private Date expiredBefore = null;
	private Date expiredAfter = null;
	private String recommendationType = null;
	private String communityId = null;
	private String userId = null;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getCommunityId() {
		return communityId;
	}

	public void setCommunityId(String communityId) {
		this.communityId = communityId;
	}

	public String getRecommendationType() {
		return recommendationType;
	}

	public void setRecommendationType(String recommendationType) {
		this.recommendationType = recommendationType;
	}

	public Date getExpiredAfter() {
		return expiredAfter;
	}

	public void setExpiredAfter(Date expiredAfter) {
		this.expiredAfter = expiredAfter;
	}

	public Date getExpiredBefore() {
		return expiredBefore;
	}

	public void setExpiredBefore(Date expiredBefore) {
		this.expiredBefore = expiredBefore;
	}

	public boolean matches(Object o) {
		Recommendation recommendation = (Recommendation) o;
		
		if (this.getCommunityId() != null) {
			if (recommendation.getCommunityIds() == null || 
					recommendation.getCommunityIds().size() == 0)
				return false;
			else {
				boolean found = false;
				
				for (String id:recommendation.getCommunityIds()) {
					if (id.equals(this.getCommunityId())) {
						found = true;
						
						break;
					}
				}
				
				if (!found)
					return false;
			}
		}
		
		if (this.getUserId() != null) {
			if (recommendation.getUserIds() == null || 
					recommendation.getUserIds().size() == 0)
				return false;
			else {
				boolean found = false;
				
				for (String id:recommendation.getUserIds()) {
					if (id.equals(this.getUserId())) {
						found = true;
						
						break;
					}
				}
				
				if (!found)
					return false;
			}
		}
		
		if (this.getExpiredAfter() != null) {
			if (recommendation.getExpirationDate() == null || 
					recommendation.getExpirationDate().before(this.getExpiredAfter()))
				return false;
		}
		
		if (this.getExpiredBefore() != null) {
			if (recommendation.getExpirationDate() == null || 
					recommendation.getExpirationDate().after(this.getExpiredBefore()))
				return false;
		}
		
		if (this.getRecommendationType() != null) {
			if (recommendation.getType() == null || 
					!recommendation.getType().toLowerCase().equals(this.getRecommendationType().toLowerCase()))
				return false;
		}
		
		
		return true;
	}

	public boolean equals(Object o) {
		return this.equals((RecommendationSearchCriteria) o);
	}
	
	public boolean equals(RecommendationSearchCriteria crit) {
		if (!super.equals(crit))
			return false;
		
		if (expiredBefore != null && crit.expiredBefore != null) {
			if (!expiredBefore.equals(crit.expiredBefore))
				return false;
		} else if ( (expiredBefore == null && crit.expiredBefore != null) || (expiredBefore != null && crit.expiredBefore == null)) {
			return false;
		}
		
		if (expiredAfter != null && crit.expiredAfter != null) {
			if (!expiredAfter.equals(crit.expiredAfter))
				return false;
		} else if ( (expiredAfter == null && crit.expiredAfter != null) || (expiredAfter != null && crit.expiredAfter == null)) {
			return false;
		}
		
		if (recommendationType != null && crit.recommendationType != null) {
			if (!recommendationType.equals(crit.recommendationType))
				return false;
		} else if ( (recommendationType == null && crit.recommendationType != null) || (recommendationType != null && crit.recommendationType == null)) {
			return false;
		}
		
		if (communityId != null && crit.communityId != null) {
			if (!communityId.equals(crit.communityId))
				return false;
		} else if ( (communityId == null && crit.communityId != null) || (communityId != null && crit.communityId == null)) {
			return false;
		}
		
		if (userId != null && crit.userId != null) {
			if (!userId.equals(crit.userId))
				return false;
		} else if ( (userId == null && crit.userId != null) || (userId != null && crit.userId == null)) {
			return false;
		}
		
		return true;
	}
	
	public int hashCode() {
		int code = super.hashCode();
		
		if (expiredBefore != null)
			code |= expiredBefore.hashCode();
		
		if (expiredAfter != null)
			code |= expiredAfter.hashCode();
		
		if (recommendationType != null)
			code |= recommendationType.hashCode();
		
		if (communityId != null)
			code |= communityId.hashCode();
		
		if (userId != null)
			code |= userId.hashCode();
		
		return code;
	}
}

