package info.bukova.isspst.filters;

import info.bukova.isspst.BooleanUtils;
import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.JoinedItem;
import info.bukova.isspst.data.MUnitEmb;
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 JoinedItemFilter implements Filter<JoinedItem>
{
	private JoinedItem condition;

	public JoinedItemFilter(JoinedItem cond)
	{
		this.condition = cond;
		this.condition.setMunit(new MUnitEmb());
		this.condition.setWorkgroup(new Workgroup());
		this.condition.setCentre(new Workgroup());
		this.condition.setOwnedBy(new User());
	}

	private static class JoinedItemMatcher extends TypeSafeMatcher<JoinedItem>
	{
		private JoinedItem condition;

		public JoinedItemMatcher(JoinedItem cond)
		{
			this.condition = cond;
		}

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

		@Override
		public boolean matchesSafely(JoinedItem item)
		{
			boolean foundItemMaterial = BooleanUtils.isEqualFilterByBoolean(item.isItemMaterialReal(), condition.getItemMaterial());
			boolean foundCode = StringUtils.isEqualForFilter(item.getCode(), condition.getCode());
			boolean foundName = StringUtils.isEqualForFilter(item.getName(), condition.getName());
			boolean foundTextItem = StringUtils.isEqualForFilter(item.getTextItem(), condition.getTextItem());
			boolean foundQuantity = true;// BigDecimalUtils.isEqualByDecimalForFilter(item.getQuantity(),
											// condition.getQuantity());
			boolean foundUnitPrice = true; // BigDecimalUtils.isEqualByDecimalForFilter(item.getUnitPrice(),
											// condition.getUnitPrice());
			boolean foundMUnit = MUnitEmb.isEqualMUnitEmbForFilter(item.getMunit(), condition.getMunit());
			boolean foundTotal = true;// BigDecimalUtils.isEqualByDecimalForFilter(item.getTotal(),
										// condition.getTotal());
			boolean foundWorkgroup = Workgroup.isEqualByWorkgroupForFilter(item.getWorkgroup(), condition.getWorkgroup());
			boolean foundCenter = Workgroup.isEqualByWorkgroupForFilter(item.getCentre(), condition.getCentre());
			boolean foundOwner = User.isEqualByUserForFilter(item.getOwnedBy(), condition.getOwnedBy());
			boolean foundDescription = StringUtils.isEqualForFilter(item.getDescription(), condition.getDescription());
			return (foundItemMaterial
					&& foundCode
					&& foundName
					&& foundTextItem
					&& foundDescription
					&& foundQuantity
					&& foundUnitPrice
					&& foundMUnit
					&& foundTotal
					&& foundWorkgroup
					&& foundCenter
					&& foundOwner && foundDescription);
		}

		@Factory
		public static Matcher<JoinedItem> matchBuilding(JoinedItem item)
		{
			return new JoinedItemMatcher(item);
		}
	}

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

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