package com.init.octo;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.init.octo.query.Column;

public class ObjectUtil {

	public static Object castValue(Object value, String typeCon, Date dateFormat) throws ParseException {
		if (value == null) {
			return ("");
		}
		if (typeCon == null || typeCon.equals("string") || typeCon.equals("")) {
			return (value);
		} else if (typeCon.equals("integer")) {
			Integer ivalue = new Integer((Integer) value);
			return (ivalue);
		} else if (typeCon.equals("long")) {
			Long ivalue = new Long((Long) value);
			return (ivalue);
		} else if (typeCon.equals("short")) {
			Short ivalue = new Short((Short) value);
			return (ivalue);
		} else if (typeCon.equals("float")) {
			Float ivalue = new Float((Float) value);
			return (ivalue);
		} else if (typeCon.equals("double")) {
			Double ivalue = new Double((Double) value);
			return (ivalue);
		} else if (typeCon.equals("date")) {
			Date ivalue = Constants.dateFormat.parse((String) value);
			return (ivalue);
		}
		return (null);

	} // end getResultValueCol()
	
	

	public static boolean compare(Column column, String typeCon, String compareStr, Object lhand, Object rhand) throws ParseException {
		int comparResult = 0;
		boolean result = false;

		if (typeCon.equals("string")) {
			if (compareStr.equals("like")) {
				Pattern p = Pattern.compile((String) rhand);
				Matcher m = p.matcher((String) lhand);
				if (m.matches()) {
					comparResult = 0;
				} else {
					comparResult = -1;
				}
			} else {
				comparResult = ((String) lhand).compareTo((String) rhand);
			}
		} else if (typeCon.equals("integer")) {
			rhand = new Integer((String) rhand);
			comparResult = ((Integer) lhand).compareTo((Integer) rhand);
		} else if (typeCon.equals("long")) {
			rhand = new Long((String) rhand);
			comparResult = ((Long) lhand).compareTo((Long) rhand);
		} else if (typeCon.equals("short")) {
			rhand = new Short((String) rhand);
			comparResult = ((Short) lhand).compareTo((Short) rhand);
		} else if (typeCon.equals("float")) {
			rhand = new Float((String) rhand);
			comparResult = ((Float) lhand).compareTo((Float) rhand);
		} else if (typeCon.equals("double")) {
			rhand = new Double((String) rhand);
			comparResult = ((Double) lhand).compareTo((Double) rhand);
		} else if (typeCon.equals("date")) {
			rhand = Constants.dateFormat.parse((String) rhand);
			comparResult = ((java.util.Date) lhand).compareTo((java.util.Date) rhand);
		} else {
			//log.warn("Unknown type conversion [" + typeCon + "] - returning NOT EQUAL");
			return (false);
		}

		if (compareStr.equals("eq")) {
			result = ((comparResult == 0) ? true : false);
		} else if (compareStr.equals("ne")) {
			result = ((comparResult != 0) ? true : false);
		} else if (compareStr.equals("lt")) {
			result = ((comparResult < 0) ? true : false);
		} else if (compareStr.equals("le")) {
			result = ((comparResult <= 0) ? true : false);
		} else if (compareStr.equals("gt")) {
			result = ((comparResult > 0) ? true : false);
		} else if (compareStr.equals("ge")) {
			result = ((comparResult >= 0) ? true : false);
		} else if (compareStr.equals("like")) {
			result = ((comparResult == 0) ? true : false);
		} else if (compareStr.equals("newValue")) {
			result = (column.hasChanged()); // , row));
		}
		return result;
	}
	
	
	
	
}
