package info.bukova.isspst.filters;

import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.Material;

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

public class MaterialFilter implements Filter<Material> {
	
	private Material condMaterial;
	
	public MaterialFilter(Material condMaterial) {
		this.condMaterial = condMaterial;
	}

	private static class MaterialMatcher extends TypeSafeMatcher<Material> {

		private Material condMaterial;
		
		public MaterialMatcher(Material cond) {
			this.condMaterial = cond;
		}
		
		@Override
		public void describeTo(Description desc) {
			desc.appendText("material matches");
		}

		@Override
		public boolean matchesSafely(Material item)
		{
			boolean isEqualCode = StringUtils.isEqualForFilter(item.getCode(), this.condMaterial.getCode());
			boolean isEqualName = StringUtils.isEqualForFilter(item.getName(), this.condMaterial.getName());
			boolean isEqualMUnitName = (this.condMaterial.getMunit() == null) || (item.getMunit().getId() == this.condMaterial.getMunit().getId());
			boolean isEqualDescription = StringUtils.isEqualForFilter(item.getDescription(), this.condMaterial.getDescription());

			return isEqualCode && isEqualName && isEqualDescription && isEqualMUnitName;
		}
		
		@Factory
		public static Matcher<Material> matchBuilding(Material material) {
			return new MaterialMatcher(material);
		}
	}
	
	@Override
	public MaterialMatcher matcher() {
		return new MaterialMatcher(condMaterial);
	}

	@Override
	public String queryString() {
		// TODO query string
		return "";
	}

}
