package common.block.tech;

import common.block.Block;
import common.block.Rotatable;
import common.block.Material;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.init.Items;
import common.inventory.InventoryWarpChest;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.model.ParticleType;
import common.properties.Property;
import common.rng.Random;
import common.util.BlockPos;
import common.util.Facing;
import common.world.AWorldClient;
import common.world.State;
import common.world.World;

public class BlockWarpChest extends Block implements Rotatable
{
    public BlockWarpChest()
    {
        super(Material.SOLID);
        this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH));
        this.setTab(CheatTab.TECHNOLOGY);
        this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
    }

    /**
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
     */
    public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean isFullCube()
    {
        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 2;
//    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return provider.getModel("warp_chest_side").add(1, 0, 1, 15, 14, 15).d("warp_chest_top").u("warp_chest_top").noCull()
    			.n("warp_chest_front").noCull().s().noCull().we().noCull().rotate(ModelRotation.getNorthRot(state.getValue(FACING)));
    }

    /**
     * Get the Item that this Block should drop when harvested.
     */
    public Item getItemDropped(State state, Random rand, int fortune)
    {
        return Items.obsidian;
    }

    /**
     * Returns the quantity of items to drop on block destruction.
     */
    public int quantityDropped(Random random)
    {
        return 8;
    }

    public boolean canSilkHarvest()
    {
        return true;
    }

    /**
     * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
     * IBlockstate
     */
    public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, EntityLiving placer)
    {
        return this.getState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
    }

    /**
     * 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)
    {
        worldIn.setState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
    }

    public boolean onBlockActivated(World worldIn, BlockPos pos, State state, EntityNPC playerIn, Facing side, float hitX, float hitY, float hitZ)
    {
        InventoryWarpChest inv = playerIn.getWarpChest();
        if(inv != null && !worldIn.client) {
            inv.setChestTileEntity(pos);
            playerIn.displayGUIChest(inv);
//            playerIn.triggerAchievement(StatRegistry.warpChestStat);
        }
        return true;
    }

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

    public void randomDisplayTick(AWorldClient worldIn, BlockPos pos, State state, Random rand)
    {
        for (int i = 0; i < 3; ++i)
        {
            int j = rand.zrange(2) * 2 - 1;
            int k = rand.zrange(2) * 2 - 1;
            double d0 = (double)pos.getX() + 0.5D + 0.25D * (double)j;
            double d1 = (double)((float)pos.getY() + rand.floatv());
            double d2 = (double)pos.getZ() + 0.5D + 0.25D * (double)k;
            double d3 = (double)(rand.floatv() * (float)j);
            double d4 = ((double)rand.floatv() - 0.5D) * 0.125D;
            double d5 = (double)(rand.floatv() * (float)k);
            worldIn.spawnParticle(ParticleType.PORTAL, d0, d1, d2, d3, d4, d5);
        }
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        Facing enumfacing = Facing.getFront(meta);

        if (enumfacing.getAxis() == Facing.Axis.Y)
        {
            enumfacing = Facing.NORTH;
        }

        return this.getState().withProperty(FACING, enumfacing);
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((Facing)state.getValue(FACING)).getIndex();
    }

    protected Property[] getProperties()
    {
        return new Property[] {FACING};
    }
    
    public boolean isXrayVisible()
    {
        return true;
    }
}
