package common.item.tool;

import common.block.natural.BlockFire;
import common.block.tech.BlockTNT;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.SoundEvent;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.Facing;
import common.util.Vec3;
import common.world.World;

public class ItemFlintAndSteel extends Item
{
	private final BlockFire fireBlock;
	
    public ItemFlintAndSteel(BlockFire fireBlock)
    {
    	this.fireBlock = fireBlock;
        this.setMaxDamage(64);
        this.setTab(CheatTab.TOOLS);
    }

    /**
     * Called when a Block is right-clicked with this Item
     */
    public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
    {
        pos = pos.offset(side);

        if (!playerIn.canPlayerEdit(pos, side, stack))
        {
            return false;
        }
        else
        {
            if (worldIn.getState(pos).getBlock() == Blocks.air)
            {
                worldIn.playSound(SoundEvent.IGNITE, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, 1.0F);
                worldIn.setState(pos, this.fireBlock.getState());
            }

            stack.damageItem(1, playerIn);
            return true;
        }
    }
    
    public boolean isMagnetic() {
    	return true;
    }
    
    public ItemStack dispenseStack(World world, TileEntity source, Vec3 position, BlockPos blockpos, Facing facing, ItemStack stack) {
        BlockPos pos = blockpos = blockpos.offset(facing);

        if (world.isAirBlock(pos))
        {
            world.setState(pos, this.fireBlock.getState());

            if (stack.attemptDamageItem(1, world.rand))
            {
                stack.size = 0;
            }
            world.playAuxSFX(1000, blockpos, 0);
        }
        else if (world.getState(pos).getBlock() instanceof BlockTNT tnt)
        {
            tnt.onBlockDestroyedByPlayer(world, pos, tnt.getState().withProperty(BlockTNT.EXPLODE, true));
            world.setBlockToAir(pos);
            world.playAuxSFX(1000, blockpos, 0);
        }
        else
        {
            world.playAuxSFX(1001, blockpos, 0);
        }

        return stack;
	}

	public int getDispenseSoundId() {
		return 0;
	}
}
