package hmggirlfront.block;

import java.util.Random;

import javax.annotation.Nullable;

import gvclib.world.GVCExplosionBase;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.block.tile.TileEntity_ExpBlock;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAreaEffectCloud;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IProjectile;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.init.PotionTypes;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Explosion;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class Block_ExpBlock extends BlockContainer
{
	
	protected static final AxisAlignedBB drum = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D);
	protected static final AxisAlignedBB ammobox = new AxisAlignedBB(0.2D, 0.0D, 0.2D, 0.8D, 0.8D, 0.8D);
	
	private int id;
	
    public Block_ExpBlock(int i)
    {
    	super(Material.IRON);
    	this.id = i;
        //setBlockUnbreakable();
        //setLightOpacity(1);
        //setLightLevel(1.0F);
    }

	@Override
	public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
		if (id == 0) {
			for (int y = 0; y < 16; ++y) {
				spawnAsEntity(worldIn, pos, new ItemStack(mod_GirlFront.idoll_bullet));
			}
		}
		else if (id == 3) {
			for (int y = 0; y < 9; ++y) {
				spawnAsEntity(worldIn, pos, new ItemStack(mod_GirlFront.idoll_bullet_40));
			}
		}
		else if (id == 4) {
			for (int y = 0; y < 8; ++y) {
				spawnAsEntity(worldIn, pos, new ItemStack(mod_GirlFront.idoll_bullet));
			}
		}
	}
	
	/**
     * Returns the quantity of items to drop on block destruction.
     */
    public int quantityDropped(Random random)
    {
        return 0;
    }
	
    @Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileEntity_ExpBlock();
	}
    
    /**
     * Called when this Block is destroyed by an Explosion
     */
   // public void onBlockDestroyedByExplosion(World worldIn, BlockPos pos, Explosion explosionIn)
    public void onBlockExploded(World worldIn, BlockPos pos, Explosion explosion)
    {
        /*if (!worldIn.isRemote)
        {
            EntityTNTPrimed entitytntprimed = new EntityTNTPrimed(worldIn, (double)((float)pos.getX() + 0.5F), (double)pos.getY(), (double)((float)pos.getZ() + 0.5F), explosionIn.getExplosivePlacedBy());
            entitytntprimed.setFuse((short)(worldIn.rand.nextInt(entitytntprimed.getFuse() / 4) + entitytntprimed.getFuse() / 8));
            worldIn.spawnEntity(entitytntprimed);
        }*/
    	if(explosion.getExplosivePlacedBy() != null) {
    		EntityLivingBase exbase = explosion.getExplosivePlacedBy();
    		double x = pos.getX();
        	double y = pos.getY();
        	double z = pos.getZ();
        	 if(worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum1_df
        			 || worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum2_df) {
     			if (!worldIn.isRemote) 
     		    	{
     					GVCExplosionBase.ExplosionKai(exbase, exbase, x, y + 0.5, z, 3.5F, true,  false);
     					//entityIn.attackEntityFrom(DamageSource.IN_FIRE, 100.0F);
     					worldIn.setBlockToAir(pos);
     				}
     		 }else if(worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_40mmgr_ammo_df) {
     			if (!worldIn.isRemote) 
     		    	{
     					GVCExplosionBase.ExplosionKai(exbase, exbase, x, y + 0.5, z, 3.0F, false,  false);
     					//entityIn.attackEntityFrom(DamageSource.IN_FIRE, 100.0F);
     					worldIn.setBlockToAir(pos);
     				}
     		 }
        	 else {
     			if (!worldIn.isRemote) 
     		    	{
     					GVCExplosionBase.ExplosionKai(exbase, exbase, x, y + 0.5, z, 2.0F, false,  false);
     					//entityIn.attackEntityFrom(DamageSource.IN_FIRE, 20.0F);
     					worldIn.setBlockToAir(pos);
     				}
     		 }
    	}
    }
    
    /**
     * Called When an Entity Collided with the Block
     */
    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
    {
    	if(entityIn instanceof IProjectile){
    		double x = pos.getX();
        	double y = pos.getY();
        	double z = pos.getZ();
        	 if(worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum1_df
        			 || worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum2_df) {
     			if (!worldIn.isRemote) 
     		    	{
     					GVCExplosionBase.ExplosionKai(entityIn, entityIn, x, y + 0.5, z, 3.5F, true,  false);
     					//entityIn.attackEntityFrom(DamageSource.IN_FIRE, 100.0F);
     					worldIn.setBlockToAir(pos);
     				}
     		 }else if(worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_40mmgr_ammo_df) {
     			if (!worldIn.isRemote) 
     		    	{
     					GVCExplosionBase.ExplosionKai(entityIn, entityIn, x, y + 0.5, z, 3.5F, false,  false);
     					//entityIn.attackEntityFrom(DamageSource.IN_FIRE, 100.0F);
     					worldIn.setBlockToAir(pos);
     				}
     		 }
        	 else {
     			if (!worldIn.isRemote) 
     		    	{
     					GVCExplosionBase.ExplosionKai(entityIn, entityIn, x, y + 0.5, z, 2.0F, false,  false);
     					//entityIn.attackEntityFrom(DamageSource.IN_FIRE, 20.0F);
     					worldIn.setBlockToAir(pos);
     				}
     		 }
    	}
    }
    
    @SideOnly(Side.CLIENT)
    public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
    {
       /* if (rand.nextInt(24) == 0)
        {
            worldIn.playSound((double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), 
            		SoundEvents.BLOCK_FIRE_AMBIENT, SoundCategory.BLOCKS, 1.0F + rand.nextFloat(), rand.nextFloat() * 0.7F + 0.3F, false);
        }

        for (int i = 0; i < 3; ++i)
        {
            double d0 = (double)pos.getX() + rand.nextDouble();
            double d1 = (double)pos.getY() + rand.nextDouble() * 0.5D + 0.5D;
            double d2 = (double)pos.getZ() + rand.nextDouble();
            worldIn.spawnParticle(EnumParticleTypes.SMOKE_LARGE, d0, d1, d2, 0.0D, 0.0D, 0.0D);
        }*/
    }
    
    @Nullable
    public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos)
    {
        //return NULL_AABB;
    	if(worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum1_df
    			|| worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum1_df) {
    		return drum;
    	}
		 else {
			 return ammobox;
		 }
    }
    /**
     * Return an AABB (in world coords!) that should be highlighted when the player is targeting this Block
     */
    @SideOnly(Side.CLIENT)
    public AxisAlignedBB getSelectedBoundingBox(IBlockState state, World worldIn, BlockPos pos)
    {
    	if(worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum1_df
    			|| worldIn.getBlockState(pos).getBlock() == mod_GirlFront.b_block_barrel_drum1_df) {
    		return drum.offset(pos);
    	}
		 else {
			 return ammobox.offset(pos);
		 }
    }
    public boolean isFullCube(IBlockState state)
    {
        return false;
    }

    @SideOnly(Side.CLIENT)
    public BlockRenderLayer getBlockLayer()
    {
        return BlockRenderLayer.CUTOUT;
    }

    protected boolean canSilkHarvest()
    {
        return false;
    }
    /**
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
     */
    public boolean isOpaqueCube(IBlockState state)
    {
        return false;
    }
    /**
     * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
     */
    public EnumBlockRenderType getRenderType(IBlockState state)
    {
        return EnumBlockRenderType.MODEL;
    }
}