package projectzombie.items.modifier;

import bdf.types.BdfNamedList;
import bdf.types.BdfObject;

public class ItemModifierDamage extends ItemModifier
{
	private int damage;
	private int max;

	public ItemModifierDamage(int damage, int max) {
		this.damage = damage;
		this.max = max;
	}
	
	public ItemModifierDamage(BdfObject bdf) {
		BdfClassLoad(bdf);
	}
	
	public int getDamage() {
		return damage;
	}
	
	public int getMaxDamage() {
		return max;
	}
	
	public void repair() {
		damage = 0;
	}
	
	public void repair(int amount) {
		damage -= amount;
		if(damage < 0) {
			damage = 0;
		}
	}
	
	public void damage(int amount) {
		damage += amount;
		if(damage > max) {
			damage = max;
		}
	}
	
	public boolean isBroken() {
		return damage == max;
	}
	
	@Override
	public void BdfClassLoad(BdfObject bdf) {
		BdfNamedList nl = bdf.getNamedList();
		damage = nl.get("damage").getInteger();
		max = nl.get("max").getInteger();
	}

	@Override
	public void BdfClassSave(BdfObject bdf) {
		BdfNamedList nl = bdf.getNamedList();
		nl.set("damage", bdf.newObject().setInteger(damage));
		nl.set("max", bdf.newObject().setInteger(max));
	}
	
	@Override
	public boolean isEqual(ItemModifier other) {
		return ((other instanceof ItemModifierDamage) &&
				((ItemModifierDamage)other).damage == damage &&
				((ItemModifierDamage)other).max == max);
	}
	
	@Override
	public ItemModifierDamage copy() {
		return new ItemModifierDamage(damage, max);
	}
}
