package com.rjconsultores.ventaboletos.constantes;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

public class CustomEnumTypeHibernate implements UserType, ParameterizedType {
	Properties parameters;

	@Override
	public void setParameterValues(Properties parameters) {
		this.parameters = parameters;
	}

	@Override
	public int[] sqlTypes() {
		return new int[] { Types.STRUCT };
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Class returnedClass() {
		try {
			return Class.forName(parameters.get("class").toString());
		} catch (ClassNotFoundException e) {
		}
		return null;
	}

	@Override
	public boolean equals(Object x, Object y) throws HibernateException {
		return (x == y) || (x != null && y != null && (x.equals(y)));
	}

	@Override
	public int hashCode(Object x) throws HibernateException {
		return x.hashCode();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
		Object result = null;
		try {
			String name = rs.getString(names[0]);
			if (!rs.wasNull()) {
				boolean enumName = Boolean.valueOf(parameters.get("enumName").toString());
				int sqlType = Integer.valueOf(parameters.get("sqlType").toString());
				if (enumName) {
					result = Enum.valueOf(returnedClass(), name);
				} else {
					Class typeClass = null;
					Object parameterType = null;
					if (Types.INTEGER == sqlType) {
						typeClass = Integer.class;
						parameterType = Integer.valueOf(name);
					} else if (Types.VARCHAR == sqlType) {
						typeClass = String.class;
						parameterType = name;
					}
					Method enumMethod = returnedClass().getMethod(parameters.get("enumMethod").toString(),
							new Class[] { typeClass });
					result = enumMethod.invoke(returnedClass(), new Object[] { parameterType });
				}

			}
		} catch (Exception e) {
			result = null;
		}
		return result;
	}

	@Override
	@SuppressWarnings("rawtypes")
	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		int sqlType = Integer.valueOf(parameters.get("sqlType").toString()) ;
		boolean enumName = Boolean.valueOf(parameters.get("enumName").toString()) ;
		if (value == null || StringUtils.isEmpty(value.toString())) {
			st.setNull(index, sqlType);
		} else {
			Enum enumerator = (Enum) value;
			st.setObject(index, enumName ? enumerator.name() : enumerator.ordinal(), sqlType);
		}
	}

	@Override
	@SuppressWarnings("rawtypes")
	public Object deepCopy(Object value) throws HibernateException {
		if (value == null)
			return null;
		else {
			Enum enumerator = (Enum) value;
			return enumerator;
		}
	}

	@Override
	public boolean isMutable() {
		return false;
	}

	@Override
	public Serializable disassemble(Object value) throws HibernateException {
		return (value instanceof Serializable) ? (Serializable) deepCopy(value) : null;
	}

	@Override
	public Object assemble(Serializable cached, Object owner) throws HibernateException {
		return deepCopy(cached);
	}

	@Override
	public Object replace(Object original, Object target, Object owner) throws HibernateException {
		return this.deepCopy(original);
	}

}
