package info.bukova.isspst.filters;

import info.bukova.isspst.DateTimeUtils;
import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.AddressEmb;
import info.bukova.isspst.data.Order;
import info.bukova.isspst.data.User;

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

public class OrderFilter implements Filter<Order>
{
	private Order condition;

	public OrderFilter(Order cond)
	{
		this.condition = cond;
		// this.condition.setSuplier(new AddressEmb());
		// this.condition.setDeliveryAddress(new AddressEmb());
		// this.condition.setAddress(new AddressEmb());
		// this.condition.setOwnedBy(new User());
	}

	private static class OrderMatcher extends TypeSafeMatcher<Order>
	{
		private Order condition;

		public OrderMatcher(Order cond)
		{
			this.condition = cond;
		}

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

		@Override
		public boolean matchesSafely(Order item)
		{
			boolean foundOrdered = (item.isOrdered() == condition.isOrdered());
			boolean foundNumSer = StringUtils.isEqualForFilter(item.getNumser(), condition.getNumser());
			boolean foundOrderDate = DateTimeUtils.isEqualByDateForFilter(item.getOrderDate(), condition.getOrderDate());
			boolean foundTotal = true;// BigDecimalUtils.isEqualByDecimalForFilter(item.getTotal(),
										// condition.getTotal());
			boolean foundDeliveryDate = DateTimeUtils.isEqualByDateForFilter(item.getDeliveryDate(), condition.getDeliveryDate());
			boolean foundDeliveredDate = DateTimeUtils.isEqualByDateForFilter(item.getDeliveredDate(), condition.getDeliveredDate());
			boolean foundSupplierAddr = AddressEmb.isEqualByAddressEmbForFilter(item.getSuplier(), condition.getSuplier());
			boolean foundDeliveryAddr = AddressEmb.isEqualByAddressEmbForFilter(item.getDeliveryAddress(), condition.getDeliveryAddress());
			boolean foundInvoiceAddr = AddressEmb.isEqualByAddressEmbForFilter(item.getAddress(), condition.getAddress());
			boolean foundOwnedBy = User.isEqualByUserForFilter(item.getOwnedBy(), condition.getOwnedBy());
			boolean foundDescription = StringUtils.isEqualForFilter(item.getDescription(), condition.getDescription());
			return (foundOrdered
					&& foundNumSer
					&& foundOrderDate
					&& foundTotal
					&& foundDeliveryDate
					&& foundDeliveredDate
					&& foundSupplierAddr
					&& foundDeliveryAddr
					&& foundInvoiceAddr
					&& foundOwnedBy && foundDescription);
		}

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

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

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