package common.block.tech;

import common.block.BlockContainer;
import common.block.Material;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.init.Blocks;
import common.item.CheatTab;
import common.item.ItemStack;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ParticleType;
import common.rng.Random;
import common.tileentity.TileEntity;
import common.tileentity.TileEntityEnchantmentTable;
import common.util.BlockPos;
import common.util.Facing;
import common.world.AWorldClient;
import common.world.State;
import common.world.World;

public class BlockEnchantmentTable extends BlockContainer
{
	private static final Model enchanting_table_base = ModelProvider.getModelProvider().getModel("enchanting_table_bottom")
			.add(0, 0, 0, 16, 12, 16)
				.d().uv(0, 0, 16, 16)
				.u("enchanting_table_top").uv(0, 0, 16, 16).noCull()
				.n("enchanting_table_side").uv(0, 4, 16, 16)
				.s("enchanting_table_side").uv(0, 4, 16, 16)
				.w("enchanting_table_side").uv(0, 4, 16, 16)
				.e("enchanting_table_side").uv(0, 4, 16, 16)
	;
	
	public BlockEnchantmentTable()
    {
        super(Material.SOLID);
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.75F, 1.0F);
        this.setLightOpacity(0);
        this.setTab(CheatTab.TECHNOLOGY);
    }

    public boolean isFullCube()
    {
        return false;
    }

    public void randomDisplayTick(AWorldClient worldIn, BlockPos pos, State state, Random rand)
    {
        super.randomDisplayTick(worldIn, pos, state, rand);

        for (int i = -2; i <= 2; ++i)
        {
            for (int j = -2; j <= 2; ++j)
            {
                if (i > -2 && i < 2 && j == -1)
                {
                    j = 2;
                }

                if (rand.chance(16))
                {
                    for (int k = 0; k <= 1; ++k)
                    {
                        BlockPos blockpos = pos.add(i, k, j);

                        if (worldIn.getState(blockpos).getBlock() == Blocks.bookshelf)
                        {
                            if (!worldIn.isAirBlock(pos.add(i / 2, 0, j / 2)))
                            {
                                break;
                            }

                            worldIn.spawnParticle(ParticleType.ENCHANTMENT_TABLE, (double)pos.getX() + 0.5D, (double)pos.getY() + 2.0D, (double)pos.getZ() + 0.5D, (double)((float)i + rand.floatv()) - 0.5D, (double)((float)k - rand.floatv() - 1.0F), (double)((float)j + rand.floatv()) - 0.5D);
                        }
                    }
                }
            }
        }
    }

    /**
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
     */
    public boolean isOpaqueCube()
    {
        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 int getRenderType()
    {
        return 3;
    }

    /**
     * Returns a new instance of a block's tile entity class. Called on placing the block.
     */
    public TileEntity createNewTileEntity(World worldIn)
    {
        return new TileEntityEnchantmentTable();
    }

    public boolean onBlockActivated(World worldIn, BlockPos pos, State state, EntityNPC playerIn, Facing side, float hitX, float hitY, float hitZ)
    {
        if (worldIn.client)
        {
            return true;
        }
        else
        {
            TileEntity tileentity = worldIn.getTileEntity(pos);

            if (tileentity instanceof TileEntityEnchantmentTable)
            {
                playerIn.displayGui((TileEntityEnchantmentTable)tileentity);
            }

            return true;
        }
    }

    /**
     * Called by ItemBlocks after a block is set in the world, to allow post-place logic
     */
    public void onBlockPlacedBy(World worldIn, BlockPos pos, State state, EntityLiving placer, ItemStack stack)
    {
        super.onBlockPlacedBy(worldIn, pos, state, placer, stack);

        if (stack.hasDisplayName())
        {
            TileEntity tileentity = worldIn.getTileEntity(pos);

            if (tileentity instanceof TileEntityEnchantmentTable)
            {
                ((TileEntityEnchantmentTable)tileentity).setCustomName(stack.getDisplayName());
            }
        }
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return enchanting_table_base;
    }
}
