/*************************************************************************************

This file is part of FragMend.

Written by Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.

Copyright (c) 2006, Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.     
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.       
    * Neither the name of Florian Buchholz, Glenn Henderson, David Horvath, Jeff
      Jones, nor the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*************************************************************************************/

public class FilterItem {

	public static final int OP_NOP = -1;
	public static final int OP_EQUALS = 0;
	public static final int OP_MATCHES = 1;
	public static final int OP_SMALLER = 2;
	public static final int OP_GREATER = 3;
	public static final int OP_RANGE = 4;

	
	private int field;
	private int operation;
	private Object value;
	private Object value2;
	
	public FilterItem(int field, int operation, Object value, Object value2) {
		
		this.field = field;
		this.operation = operation;
		this.value = value;
		this.value2 = value2;
		
	}

	
	public boolean matches(Fragment frag) {
		
		switch (field) {
			
			case Fragment.FIELD_NAME:
				if (value instanceof String) {
					if (operation == OP_EQUALS)
						return frag.getName().equals((String)value);
					else if (operation == OP_MATCHES) {
						return frag.getName().matches("(?s).*" + ((String)value).toLowerCase() + ".*");
					}
				}
				return false;
				
			case Fragment.FIELD_PRINT_COUNT:
				if (value instanceof Integer)
					return compareIntValue(frag.getPrintableCount(), (Integer)value, operation);
				return false;
				
			case Fragment.FIELD_ZERO_PADDED:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.isZeroPadded());
				return false;

			case Fragment.FIELD_JPG_HEADER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsJPGHeader());
				return false;

			case Fragment.FIELD_JPG_MARKERS:
				if (value instanceof Integer)
					return compareIntValue(frag.getJPGMarkerCount(), (Integer)value, operation);
				return false;

			case Fragment.FIELD_JPG_FOOTER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsJPGFooter());
				return false;
				
			case Fragment.FIELD_ZIP_HEADER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsZIPHeader());
				return false;
				
			case Fragment.FIELD_ZIP_FOOTER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsZIPFooter());
				return false;

			case Fragment.FIELD_HTML_HEADER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsHTMLHeader());
				return false;
				
			case Fragment.FIELD_HTML_FOOTER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsHTMLFooter());
				return false;
				
			case Fragment.FIELD_BRACKET_COUNT:
				if (value instanceof Integer)
					return compareIntValue(frag.getBracketCount(), (Integer)value, operation);
				return false;

			case Fragment.FIELD_ID:
				if ((value instanceof Integer) && (value2 instanceof Integer))
					return ((frag.getId() >= ((Integer)value).intValue()) &&
						(frag.getId() <= ((Integer)value2).intValue()));
				return false;

			case Fragment.FIELD_OFFICE_HEADER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsOfficeHeader());
				return false;
				
			case Fragment.FIELD_OFFICE_FOOTER:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsOfficeFooter());
				return false;
				
			case Fragment.FIELD_OFFICE_ROOTDIR:
				if (value instanceof Boolean)
					return (((Boolean)value).booleanValue() == frag.containsOfficeRootDirectory());
				return false;
				
			case Fragment.FIELD_SEQUENCE_COUNT:
				if (value instanceof Integer)
					return compareIntValue(frag.getSequenceCount(), (Integer)value, operation);
				return false;

		}
		return false;
		
	}
	
	private boolean compareIntValue(int field_value, Integer value, int op) {
		int val = value.intValue();
		if (op == OP_EQUALS)
			return (field_value == val);
		else if (op == OP_SMALLER)
			return (field_value < val);
		else if (op == OP_GREATER)
			return (field_value > val);
		
		return false;

	}
}

