/**
 * 
 */
package org.ajax4jsf.builder.generator;

import java.util.HashMap;
import java.util.Map;

import org.ajax4jsf.builder.config.PropertyBean;

/**
 * @author Nick Belaevski - nbelaevski@exadel.com
 * created 13.04.2007
 * 
 */
public class TestDataGenerator {

	private ClassLoader loader;
	private Logger logger;

	private static Map<String, String> testData = new HashMap<String, String>();
	static {
		testData.put(Boolean.TYPE.getName(), "true");
		testData.put(Character.TYPE.getName(), "'#'");
		testData.put(Byte.TYPE.getName(), "17");
		testData.put(Short.TYPE.getName(), "18");
		testData.put(Integer.TYPE.getName(), "20");
		testData.put(Long.TYPE.getName(), "26");
		testData.put(Float.TYPE.getName(), "2.05f");
		testData.put(Double.TYPE.getName(), "1.34d");

		testData.put(Boolean.class.getName(), "new Boolean(true)");
		testData.put(Character.class.getName(), "new Character('#')");
		testData.put(Byte.class.getName(), "new Byte(17)");
		testData.put(Short.class.getName(), "new Short(18)");
		testData.put(Integer.class.getName(), "new Integer(20)");
		testData.put(Long.class.getName(), "new Long(26)");
		testData.put(Float.class.getName(), "new Float(2.05f)");
		testData.put(Double.class.getName(), "new Double(1.34d)");

	}

	private static Map<String, String> testData1 = new HashMap<String, String>();
	static {
		testData1.put(Boolean.TYPE.getName(), "false");
		testData1.put(Character.TYPE.getName(), "'*'");
		testData1.put(Byte.TYPE.getName(), "8");
		testData1.put(Short.TYPE.getName(), "26");
		testData1.put(Integer.TYPE.getName(), "15");
		testData1.put(Long.TYPE.getName(), "13");
		testData1.put(Float.TYPE.getName(), "1.05f");
		testData1.put(Double.TYPE.getName(), "1.44d");

		testData1.put(Boolean.class.getName(), "new Boolean(false)");
		testData1.put(Character.class.getName(), "new Character('*')");
		testData1.put(Byte.class.getName(), "new Byte(8)");
		testData1.put(Short.class.getName(), "new Short(26)");
		testData1.put(Integer.class.getName(), "new Integer(15)");
		testData1.put(Long.class.getName(), "new Long(13)");
		testData1.put(Float.class.getName(), "new Float(1.05f)");
		testData1.put(Double.class.getName(), "new Double(1.44d)");

	}
	
	private static Map<String, Class> primitiveToWrapper = new HashMap<String, Class>();
	static {
		primitiveToWrapper.put(Boolean.TYPE.getName(), Boolean.class);
		primitiveToWrapper.put(Character.TYPE.getName(), Character.class);
		primitiveToWrapper.put(Byte.TYPE.getName(), Byte.class);
		primitiveToWrapper.put(Short.TYPE.getName(), Short.class);
		primitiveToWrapper.put(Integer.TYPE.getName(), Integer.class);
		primitiveToWrapper.put(Long.TYPE.getName(), Long.class);
		primitiveToWrapper.put(Float.TYPE.getName(), Float.class);
		primitiveToWrapper.put(Double.TYPE.getName(), Double.class);
	}

	public TestDataGenerator(ClassLoader loader, Logger logger) {
		super();
		this.loader = loader;
		this.logger = logger;
	}


	public String getTestVeClass(PropertyBean propertyBean) {
		String type = propertyBean.getClassname();
		Class clazz = primitiveToWrapper.get(propertyBean.getClassname());
		if (clazz == null) {
			try {
				clazz = loader.loadClass(propertyBean.getClassname());
			} catch (ClassNotFoundException e) {
				logger.error(e);

				return null;
			}
		}

		return clazz.getName();
	}
	
	public String getTestVeData(PropertyBean propertyBean) {
		String veClass = getTestVeClass(propertyBean);
		if (veClass != null) {
			return getTestData(propertyBean.getName(), veClass, 0);
		}
		
		return "";
	}

	public String getTestVeData1(PropertyBean propertyBean) {
		String veClass = getTestVeClass(propertyBean);
		if (veClass != null) {
			return getTestData(propertyBean.getName(), veClass, 1);
		}
		
		return "";
	}

	private String getTestData(String propertyName, String className, int number) {
		
		String string = number == 0 ? testData.get(className) : testData1.get(className);
		
		if (string != null) {
			return string;
		}
		
		try {
			Class<?> clazz = Class.forName(className);
			if (clazz.isAssignableFrom(String.class)) {
				if (number == 0) {
					return "\"" + propertyName + "\"";
				} else {
					return "\"" + propertyName + "_" + propertyName + "\"";
				}
			}
 			
		} catch(Exception e) {
			
		}
		
		return "createTestData_" + propertyName + "()";
	}	
	
	public String getTestData(PropertyBean propertyBean) {
		return getTestData(propertyBean.getName(), propertyBean.getClassname(), 0);
	}

	public String getTestData1(PropertyBean propertyBean) {
		return getTestData(propertyBean.getName(), propertyBean.getClassname(), 1);
	}
}
