package projectzombie.inventory;

import bdf.classes.IBdfClassManager;
import bdf.types.BdfArray;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import projectzombie.items.Item;
import projectzombie.util.math.ItemStack;

public class Inventory implements IInventory, IBdfClassManager
{
	protected ItemStack[] items;
	
	public Inventory(int size)
	{
		items = new ItemStack[size];
		
		for(int i=0;i<size;i++) {
			items[i] = ItemStack.getEmpty();
		}
	}

	@Override
	public ItemStack getItem(int slot) {
		return items[slot];
	}

	@Override
	public int getSlotCount() {
		return items.length;
	}

	@Override
	public void setItem(ItemStack stack, int slot) {
		items[slot] = stack;
	}

	@Override
	public ItemStack removeItem(int slot) {
		ItemStack stack = items[slot];
		items[slot] = ItemStack.getEmpty();
		return stack;
	}
	
	public int getItemCount(ItemStack item)
	{
		int count = 0;
		
		for(ItemStack check : items) {
			if(item.stackEquals(check) && !check.isEmpty()) {
				count += check.count;
			}
		}
		
		return count;
	}
	
	public int getItemCount(Item item)
	{
		int count = 0;
		
		for(ItemStack check : items) {
			if(item == check.item && !check.isEmpty()) {
				count += 1;
			}
		}
		
		return count;
	}
	
	public void addItem(ItemStack stack)
	{
		if(stack.isEmpty()) {
			return;
		}
		
		for(ItemStack check : items)
		{
			if(stack.stackEquals(check) && !check.isEmpty())
			{
				if(check.count + stack.count > check.item.max) {
					stack.count = (check.count + stack.count) - check.item.max;
					check.count = check.item.max;
				}
				
				else {
					check.count += stack.count;
					stack.count = 0;
					return;
				}
			}
		}
		
		for(ItemStack check : items)
		{
			if(check.isEmpty()) {
				check.item = stack.item;
				check.count = stack.count;
				check.meta = stack.meta;
				stack.count = 0;
				return;
			}
		}
	}
	
	public void removeItem(ItemStack stack)
	{
		for(ItemStack check : items) {
			if(stack.stackEquals(check) && !stack.isEmpty())
			{
				if(check.count < stack.count) {
					stack.count -= check.count;
					check.count = 0;
				}
				
				else {
					check.count -= stack.count;
					stack.count = 0;
					return;
				}
			}
		}
	}
	
	public void removeItem(Item item, int count)
	{
		for(ItemStack check : items) {
			if(item == check.item)
			{
				if(check.count < count) {
					count -= check.count;
					check.count = 0;
				}
				
				else {
					check.count -= count;
					return;
				}
			}
		}
	}

	@Override
	public void BdfClassLoad(BdfObject bdf)
	{
		BdfNamedList nl = bdf.getNamedList();
		items = new ItemStack[nl.get("size").getInteger()];
		
		BdfArray array = nl.get("items").getArray();
		
		for(int i=0;i<items.length;i++) {
			items[i] = new ItemStack(array.get(i));
		}
	}

	@Override
	public void BdfClassSave(BdfObject bdf)
	{
		BdfNamedList nl = bdf.getNamedList();
		
		nl.set("size", BdfObject.withInteger(items.length));
		BdfArray array = nl.get("items").getArray();
		
		for(ItemStack stack : items) {
			BdfObject stack_bdf = new BdfObject();
			stack.BdfClassSave(stack_bdf);
			array.add(stack_bdf);
		}
	}
	
	public Inventory(BdfObject bdf) {
		BdfClassLoad(bdf);
	}
	
}
