package info.bukova.isspst.filters;

import info.bukova.isspst.DateTimeUtils;
import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.Invoicing;
import info.bukova.isspst.data.Requirement;
import info.bukova.isspst.data.User;
import info.bukova.isspst.data.Workgroup;

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

public class InvoicingFilter implements Filter<Invoicing>
{

	private Invoicing condition;

	public InvoicingFilter(Invoicing cond)
	{
		this.condition = cond;
	}

	private static class InvoicingMatcher extends TypeSafeMatcher<Invoicing>
	{

		private Invoicing condition;

		public InvoicingMatcher(Invoicing cond)
		{
			this.condition = cond;
			
			if (condition.getRequirement() == null) {
				condition.setRequirement(new Requirement());
			}
		}

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

		@Override
		public boolean matchesSafely(Invoicing item)
		{
			boolean foundNumser = StringUtils.isEqualForFilter(item.getRequirement().getNumser(), condition.getRequirement().getNumser());
			boolean foundReqDate = DateTimeUtils.isEqualByDateForFilter(item.getRequirement().getReqDate(), condition.getRequirement().getReqDate());
			boolean foundCenter = Workgroup.isEqualByWorkgroupForFilter(item.getRequirement().getCentre(), condition.getRequirement().getCentre());
			boolean foundWorkgroup = Workgroup.isEqualByWorkgroupForFilter(item.getRequirement().getWorkgroup(), condition.getRequirement().getWorkgroup());
			boolean foundUser = User.isEqualByUserForFilter(item.getRequirement().getOwnedBy(), condition.getRequirement().getOwnedBy());
			boolean foundDescription = StringUtils.isEqualForFilter(item.getRequirement().getDescription(), condition.getRequirement().getDescription());
			return (foundNumser && foundReqDate && foundCenter && foundDescription && foundWorkgroup && foundUser);
		}

		@Factory
		public static Matcher<Invoicing> matchBuilding(Invoicing building)
		{
			return new InvoicingMatcher(building);
		}
	}

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

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

}
