package org.treytomes.dwarfCraft.block.deployer;

import net.minecraft.dispenser.IBlockSource;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraft.block.BlockDispenser;

public class DeployerBehaviorFire extends BehaviorDefaultDeployItem {
	
    private boolean _wasFireStarted = true;

    /**
     * Dispense the specified stack, play the dispense sound and spawn particles.
     */
    protected ItemStack deployStack(IBlockSource block, ItemStack stack) {
    	
        EnumFacing facing = BlockDispenser.func_149937_b(block.getBlockMetadata()); // get facing
        World world = block.getWorld();
        int offsetX = block.getXInt() + facing.getFrontOffsetX();
        int offsetY = block.getYInt() + facing.getFrontOffsetY();
        int offsetZ = block.getZInt() + facing.getFrontOffsetZ();

        if (world.isAirBlock(offsetX, offsetY, offsetZ)) {
            world.setBlock(offsetX, offsetY, offsetZ, Blocks.fire);

            if (stack.attemptDamageItem(1, world.rand)) {
                stack.stackSize = 0;
            }
            
            _wasFireStarted = true;
        } else if (world.getBlock(offsetX, offsetY, offsetZ) == Blocks.tnt) {
            Blocks.tnt.onBlockDestroyedByPlayer(world, offsetX, offsetY, offsetZ, 1);
            world.setBlockToAir(offsetX, offsetY, offsetZ);
            
            _wasFireStarted = true;
        } else {
            _wasFireStarted = false;
        }
 
        return stack;
    }

    /**
     * Play the dispense sound from the specified block.
     */
    protected void playDeploySound(IBlockSource block) {
        if (_wasFireStarted) {
            block.getWorld().playAuxSFX(1000, block.getXInt(), block.getYInt(), block.getZInt(), 0);
        } else {
            block.getWorld().playAuxSFX(1001, block.getXInt(), block.getYInt(), block.getZInt(), 0);
        }
    }
}
