package info.bukova.isspst.filters;

import info.bukova.isspst.DateTimeUtils;
import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.TripRequirement;
import info.bukova.isspst.data.User;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class TripRequirementFilter implements Filter<TripRequirement>
{

	private TripRequirement condition;

	public TripRequirementFilter(TripRequirement cond)
	{
		this.condition = cond;
	}

	private static class TripRequirementMatcher extends TypeSafeMatcher<TripRequirement>
	{

		private TripRequirement condition;

		public TripRequirementMatcher(TripRequirement cond)
		{
			this.condition = cond;
		}

		@Override
		public void describeTo(Description desc)
		{
			desc.appendText("requirement matches");
		}

		@Override
		public boolean matchesSafely(TripRequirement item)
		{
			boolean foundNumser = StringUtils.isEqualForFilter(item.getNumser(), condition.getNumser());
			boolean foundReqDate = DateTimeUtils.isEqualByDateForFilter(item.getReqDate(), condition.getReqDate());
			boolean foundDescription = StringUtils.isEqualForFilter(item.getDescription(), condition.getDescription());
			boolean foundFrom = StringUtils.isEqualForFilter(item.getFrom(), condition.getFrom());
			boolean foundTo = StringUtils.isEqualForFilter(item.getTo(), condition.getTo());
			boolean foundWorkgroup = (condition.getWorkgroup() == null ||(item.getWorkgroup() != null && item.getWorkgroup().equals(condition.getWorkgroup())));
			boolean foundCentre = (condition.getCentre() == null || (item.getCentre() != null && item.getCentre().equals(condition.getCentre())));
			boolean foundOwner = User.isEqualByUserForFilter(item.getOwnedBy(), condition.getOwnedBy());
			return foundNumser && foundReqDate && foundDescription && foundFrom && foundTo && foundWorkgroup && foundCentre && foundOwner;
		}

		@Factory
		public static Matcher<TripRequirement> matchTripRequirement(TripRequirement building)
		{
			return new TripRequirementMatcher(building);
		}
	}

	@Override
	public TripRequirementMatcher matcher()
	{
		return new TripRequirementMatcher(condition);
	}

	@Override
	public String queryString()
	{
		return "";
	}

}
