package bl4ckscor3.mod.globalxp.blocks;

import bl4ckscor3.mod.globalxp.Configuration;
import bl4ckscor3.mod.globalxp.GlobalXP;
import bl4ckscor3.mod.globalxp.imc.top.ITOPInfoProvider;
import bl4ckscor3.mod.globalxp.tileentity.TileEntityXPBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class XPBlock extends Block implements ITOPInfoProvider
{
	public XPBlock()
	{
		super(Block.Properties.create(Material.IRON).hardnessAndResistance(12.5F, 2000.0F).sound(SoundType.METAL));

		setRegistryName(GlobalXP.MOD_ID + ":xp_block");
	}

	@Override
	public boolean isNormalCube(BlockState state, IBlockReader world, BlockPos pos)
	{
		return false;
	}

	@Override
	public boolean hasComparatorInputOverride(BlockState state)
	{
		return true;
	}

	@Override
	public int getComparatorInputOverride(BlockState state, World world, BlockPos pos)
	{
		TileEntity te = world.getTileEntity(pos);

		if(te instanceof TileEntityXPBlock)
			return Math.min(15, Math.floorDiv(((TileEntityXPBlock)te).getStoredXP(), Configuration.CONFIG.xpForComparator.get()));
		else return 0;
	}

	@Override
	public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack)
	{
		if(world.isRemote || !stack.hasTag())
			return;

		TileEntity te = world.getTileEntity(pos);

		if(te instanceof TileEntityXPBlock)
		{
			CompoundNBT tag = stack.getTag().getCompound("BlockEntityTag");

			tag.putInt("x", pos.getX());
			tag.putInt("y", pos.getY());
			tag.putInt("z", pos.getZ());
			((TileEntityXPBlock)te).read(tag);
			((TileEntityXPBlock)te).markDirty();
			world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2);
		}
	}

	@Override
	public void onBlockHarvested(World world, BlockPos pos, BlockState state, PlayerEntity player)
	{
		TileEntity te = world.getTileEntity(pos);

		if(world.getTileEntity(pos) instanceof TileEntityXPBlock)
			((TileEntityXPBlock)te).setDestroyedByCreativePlayer(player.isCreative());

		super.onBlockHarvested(world, pos, state, player);
	}

	@Override
	public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving)
	{
		TileEntity te = world.getTileEntity(pos);

		if(te instanceof TileEntityXPBlock)
		{
			ItemStack stack = new ItemStack(asItem());

			if(((TileEntityXPBlock)te).getStoredLevels() != 0)
			{
				CompoundNBT stackTag = new CompoundNBT();

				stackTag.put("BlockEntityTag", ((TileEntityXPBlock)te).write(new CompoundNBT()));
				stack.setTag(stackTag);
				spawnAsEntity(world, pos, stack);
			}
			else if(!((TileEntityXPBlock)te).isDestroyedByCreativePlayer())
				spawnAsEntity(world, pos, stack);
		}

		super.onReplaced(state, world, pos, newState, isMoving);
	}

	@Override
	public boolean hasTileEntity(BlockState state)
	{
		return true;
	}

	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world)
	{
		return new TileEntityXPBlock();
	}
	//
	//	@Override
	//	public void addProbeInfo(ProbeMode mode, IProbeInfo probeInfo, PlayerEntity player, World world, BlockState blockState, IProbeHitData data)
	//	{
	//		TileEntity te = world.getTileEntity(data.getPos());
	//
	//		if(te instanceof TileEntityXPBlock)
	//		{
	//			probeInfo.horizontal().text(new TranslationTextComponent("info.globalxp.levels", String.format("%.2f", ((TileEntityXPBlock)te).getStoredLevels())).getFormattedText());
	//
	//			if(mode == ProbeMode.EXTENDED)
	//				probeInfo.horizontal().text(new TranslationTextComponent("info.globalxp.xp", ((TileEntityXPBlock)te).getStoredXP()).getFormattedText());
	//		}
	//	}
}

@@ -663,16 +663,125 @@
 ckPos;%0D%0A
+import net.minecraft.util.math.shapes.ISelectionContext;%0D%0Aimport net.minecraft.util.math.shapes.VoxelShape;%0D%0A
 import n
@@ -918,16 +918,137 @@
 der%0D%0A%7B%0D%0A
+%09private static final VoxelShape SHAPE = Block.makeCuboidShape(0.0001D, 0.0001D, 0.0001D, 15.999D, 15.999D, 15.999D);%0D%0A%0D%0A
 %09public 
@@ -1256,27 +1256,26 @@
 lic 
-boolean isNormalCub
+VoxelShape getShap
 e(Bl
@@ -1304,24 +1304,26 @@
 Reader world
+In
 , BlockPos p
@@ -1316,32 +1316,59 @@
 In, BlockPos pos
+, ISelectionContext context
 )%0D%0A%09%7B%0D%0A%09%09return 
@@ -1371,13 +1371,13 @@
 urn 
-false
+SHAPE
 ;%0D%0A%09

fix face culling/lighting issues