package common.item;

import java.util.Collections;

import common.inventory.IInventory;
import common.rng.Random;
import common.rng.RngItem;
import common.rng.WeightedList;
import common.tileentity.TileEntityDispenser;

public class RngLoot extends RngItem
{
    private ItemStack item;
    private int minStackSize;
    private int maxStackSize;

    public RngLoot(Item item, int min, int max, int weight)
    {
        super(weight);
        this.item = new ItemStack(item);
        this.minStackSize = min;
        this.maxStackSize = max;
    }

    public RngLoot(ItemStack stack, int min, int max, int weight)
    {
        super(weight);
        this.item = stack;
        this.minStackSize = min;
        this.maxStackSize = max;
    }
    
    public ItemStack getItem(Random rand) {
    	if(this.item == null)
    		return null;
    	ItemStack stack = this.item.copy();
    	stack.size = this.minStackSize + rand.zrange(this.maxStackSize - this.minStackSize + 1);
    	return stack;
    }

    public static void generateChestContents(Random random, WeightedList<RngLoot> list, IInventory inv, int max)
    {
        for (int i = 0; i < max; ++i)
        {
            RngLoot loot = (RngLoot)list.pick(random);
            int j = loot.minStackSize + random.zrange(loot.maxStackSize - loot.minStackSize + 1);

            if (loot.item.getMaxStackSize() >= j)
            {
                ItemStack itemstack1 = loot.item.copy();
                itemstack1.size = j;
                inv.setInventorySlotContents(random.zrange(inv.getSizeInventory()), itemstack1);
            }
            else
            {
                for (int k = 0; k < j; ++k)
                {
                    ItemStack itemstack = loot.item.copy();
                    itemstack.size = 1;
                    inv.setInventorySlotContents(random.zrange(inv.getSizeInventory()), itemstack);
                }
            }
        }
    }

    public static void generateDispenserContents(Random random, WeightedList<RngLoot> list, TileEntityDispenser dispenser, int max)
    {
        for (int i = 0; i < max; ++i)
        {
            RngLoot loot = (RngLoot)list.pick(random);
            int j = loot.minStackSize + random.zrange(loot.maxStackSize - loot.minStackSize + 1);

            if (loot.item.getMaxStackSize() >= j)
            {
                ItemStack itemstack1 = loot.item.copy();
                itemstack1.size = j;
                dispenser.setInventorySlotContents(random.zrange(dispenser.getSizeInventory()), itemstack1);
            }
            else
            {
                for (int k = 0; k < j; ++k)
                {
                    ItemStack itemstack = loot.item.copy();
                    itemstack.size = 1;
                    dispenser.setInventorySlotContents(random.zrange(dispenser.getSizeInventory()), itemstack);
                }
            }
        }
    }

    public static WeightedList<RngLoot> addToList(WeightedList<RngLoot> items, RngLoot... add)
    {
    	WeightedList<RngLoot> list = new WeightedList(items);
        Collections.addAll(list, add);
        return list;
    }
}
