package common.block.natural;

import common.block.Block;
import common.block.Material;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.rng.Random;
import common.util.BlockPos;
import common.vars.Vars;
import common.world.State;
import common.world.World;

public class BlockOre extends Block
{
	private ItemStack smeltItem;
	private ItemStack dropItem;
	private int dropChance;
	private int minExperience;
	private int maxExperience;

    public BlockOre()
    {
        super(Material.SOLID);
        this.setTab(CheatTab.GEMS);
    }
    
    public void setDropItem(ItemStack item, int chance, int minXp) {
    	if(this.dropItem != null) {
    		throw new IllegalStateException("Drop item already set");
    	}
    	this.dropItem = item;
    	this.dropChance = chance;
    	this.minExperience = minXp;
    	this.maxExperience = minXp == 0 ? 2 : (minXp * 2 + 1);
    }
    
    public void setDropItem(ItemStack item, int minXp) {
    	this.setDropItem(item, 0, minXp);
    }
    
    public void setSmeltItem(ItemStack item) {
    	if(this.smeltItem != null) {
    		throw new IllegalStateException("Smelt item already set");
    	}
    	this.smeltItem = item;
    }
    
    public Item getItemDropped(State state, Random rand, int fortune)
    {
        return this.dropItem == null ? this.getItem() : this.dropItem.getItem();
//        		this == Blocks.coal_ore ? Items.coal : 
//        		(this == Blocks.diamond_ore ? Items.diamond : 
//        		(this == Blocks.lapis_ore ? Items.dye : 
//        		(this == Blocks.emerald_ore ? Items.emerald : 
//        		(this == Blocks.quartz_ore ? Items.quartz : 
//        		this.getItem()))));
    }
    
    public int quantityDropped(Random random)
    {
        return this.dropItem == null ? 1 : (this.dropItem.size + (this.dropChance > 0 ? random.zrange(this.dropChance + 1) : 0)); 
        // this == Blocks.lapis_ore ? 4 + random.nextInt(5) : 1;
    }
    
    public int quantityDroppedWithBonus(int fortune, Random random)
    {
        if (fortune > 0 && 
        		this.getItem() != this.getItemDropped((State)this.getValidStates().iterator().next(), random, fortune))
        {
            int i = random.zrange(fortune + 2) - 1;

            if (i < 0)
            {
                i = 0;
            }

            return this.quantityDropped(random) * (i + 1);
        }
        else
        {
            return this.quantityDropped(random);
        }
    }
    
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, State state, float chance, int fortune)
    {
    	if(!worldIn.client && this.smeltItem != null && Vars.cleanCut) {
            if (worldIn.rand.floatv() <= chance)
            {
                spawnAsEntity(worldIn, pos, this.smeltItem.copy());
            }
            return;
    	}
    	
        super.dropBlockAsItemWithChance(worldIn, pos, state, chance, fortune);

        if (this.getItemDropped(state, worldIn.rand, fortune) != this.getItem())
        {
            int i = worldIn.rand.range(this.minExperience, this.maxExperience);

//            if (this == Blocks.coal_ore)
//            {
//                i = MathHelper.getRandomIntegerInRange(worldIn.rand, 0, 2);
//            }
//            else if (this == Blocks.diamond_ore)
//            {
//                i = MathHelper.getRandomIntegerInRange(worldIn.rand, 3, 7);
//            }
//            else if (this == Blocks.emerald_ore)
//            {
//                i = MathHelper.getRandomIntegerInRange(worldIn.rand, 3, 7);
//            }
//            else if (this == Blocks.lapis_ore)
//            {
//                i = MathHelper.getRandomIntegerInRange(worldIn.rand, 2, 5);
//            }
//            else if (this == Blocks.quartz_ore)
//            {
//                i = MathHelper.getRandomIntegerInRange(worldIn.rand, 2, 5);
//            }

            this.dropXpOnBlockBreak(worldIn, pos, i);
        }
    }
    
    public boolean isXrayVisible()
    {
        return true;
    }
}
