package util.componentInfo;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.ArrayList;

public class ComponentInfo {
	private final String beanName;
	private String description;
	private String attribute;
	private String property;
	private boolean print;

	public ComponentInfo(String beanName) {
		this.beanName = beanName;
		this.attribute = "";
		this.property = "";
		print = true;
	}

	public void setComponentInfo(String attribute, String property) {
		this.attribute = attribute;
		this.property = property;
		this.description = "";
		if(print) printInfo();
	}
	
	public void setComponentInfo(String attribute, String property, String description) {
		this.attribute = attribute;
		this.property = property;
		this.description = description;
		if(print) printInfo();
	}
	
	public static String toString(Object obj) {
		Class cl = obj.getClass();
		String r = cl.getName() + "[";
		Class sc = cl.getSuperclass();
		if (!sc.equals(Object.class))
			r += sc + ",";
		Field[] fields = cl.getDeclaredFields();
		try {
			AccessibleObject.setAccessible(fields, true);
		} catch (SecurityException e) {
		}
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			r += f.getName() + "=";
			try {
				Object val = f.get(obj);
				r += val.toString();
			} catch (IllegalAccessException e) {
				r += "???";
			}
			if (i < fields.length - 1)
				r += ",";
			else
				r += "]";
		}
		return r;
	}

	public boolean isPrint() {
		return print;
	}

	public void setPrint(boolean print) {
		this.print = print;
	}

	public String [] getComponentInfo() {
		String [] str = {attribute, property};
		return str;
	}
	
	public String getAttribute() {
		return attribute;
	}

	public String getProperty() {
		return property;
	}
	
	private void printInfo() {
		System.out.println("beanName: " + beanName + "[attribute: " +  attribute + "; property: " + property + "; description: " + description + "];");
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
