package info.bukova.isspst.filters;

import info.bukova.isspst.BooleanUtils;
import info.bukova.isspst.DateTimeUtils;
import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.TripBillApproval;
import info.bukova.isspst.data.User;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class TripBillApprovalFilter implements Filter<TripBillApproval>
{

	private TripBillApproval condition;

	public TripBillApprovalFilter(TripBillApproval cond)
	{
		this.condition = cond;
	}

	private static class TripBillApprovalMatcher extends TypeSafeMatcher<TripBillApproval>
	{

		private TripBillApproval condition;

		public TripBillApprovalMatcher(TripBillApproval cond)
		{
			this.condition = cond;
		}

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

		@Override
		public boolean matchesSafely(TripBillApproval item)
		{
			if (item.getBill() == null) {
				return false;
			}

			boolean foundNumser = StringUtils.isEqualForFilter(item.getNumser(), condition.getNumser());
			boolean foundReqDate = DateTimeUtils.isEqualByDateForFilter(item.getBill().getRequirement().getReqDate(), condition.getBill().getRequirement().getReqDate());
			boolean foundDescription = StringUtils.isEqualForFilter(item.getDescription(), condition.getDescription());
			boolean foundFrom = StringUtils.isEqualForFilter(item.getBill().getRequirement().getFrom(), condition.getBill().getRequirement().getFrom());
			boolean foundTo = StringUtils.isEqualForFilter(item.getBill().getRequirement().getTo(), condition.getBill().getRequirement().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.getBill().getOwnedBy(), condition.getBill().getOwnedBy());
			boolean foundPaid = BooleanUtils.isEqualByBooleanValue(item.getBill().getPaid(), condition.getBill().getPaid());
			boolean foundPaidDate = DateTimeUtils.isEqualByDateForFilter(item.getBill().getPaidDate(), condition.getBill().getPaidDate());
			return foundNumser && foundReqDate && foundDescription && foundFrom && foundTo && foundWorkgroup && foundCentre && foundOwner && foundPaid && foundPaidDate;
		}

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

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

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

}
