package projectzombie.items.modifier;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import projectzombie.util.ClassBdf;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import projectzombie.entity.Entity;
import projectzombie.init.ItemModifiers;
import projectzombie.init.Tasks;
import projectzombie.task.Task;

public abstract class ItemModifier implements ClassBdf
{
	public abstract boolean isEqual(ItemModifier other);
	public abstract ItemModifier copy();
	
	public int getID() {
		for(int i=0;i<ItemModifiers.MODIFIERS.size();i++) {
			Class<? extends ItemModifier> tc = ItemModifiers.MODIFIERS.get(i);
			if(tc == this.getClass()) {
				return i;
			}
		}
		return -1;
	}
	
	public static ItemModifier loadModifier(BdfObject bdf)
	{
		try
		{
			// Load the task id
			BdfNamedList nl = bdf.getNamedList();
			int id = nl.get("id").getInteger();
			
			// Send back null if the id is out of range
			if(id < 0 || id >= ItemModifiers.MODIFIERS.size()) {
				System.out.println("Invalid modifier ID found: " + id);
				return null;
			}
			
			// Get the class and the constructor
			Class<? extends ItemModifier> tcl = ItemModifiers.MODIFIERS.get(id);
			Constructor<? extends ItemModifier> tcon = tcl.getConstructor(BdfObject.class);
			
			// Send back the new entity
			return tcon.newInstance(bdf);
		}
		
		catch(
				NoSuchMethodException |
				SecurityException |
				InstantiationException |
				IllegalAccessException |
				IllegalArgumentException |
				InvocationTargetException e)
		{
			e.printStackTrace();
			
			// Send null if there was an issue
			return null;
		}
	}
}
