package common.block.artificial;

import java.util.List;

import common.block.Block;
import common.block.Rotatable;
import common.block.Material;
import common.entity.Entity;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.init.Blocks;
import common.init.Items;
import common.item.Item;
import common.item.ItemStack;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.properties.Property;
import common.properties.PropertyBool;
import common.rng.Random;
import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class BlockPortalFrame extends Block implements Rotatable
{
    public static final PropertyBool ORB = PropertyBool.create("orb");

    public BlockPortalFrame()
    {
        super(Material.SOLID);
        this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH).withProperty(ORB, Boolean.valueOf(false)));
    }

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

    /**
     * Sets the block's bounds for rendering it as an item
     */
    public void setBlockBoundsForItemRender()
    {
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.8125F, 1.0F);
    }

    /**
     * Add all collision boxes of this Block to the list that intersect with the given mask.
     */
    public void addCollisionBoxesToList(World worldIn, BlockPos pos, State state, BoundingBox mask, List<BoundingBox> list, Entity collidingEntity)
    {
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.8125F, 1.0F);
        super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);

        if (((Boolean)worldIn.getState(pos).getValue(ORB)).booleanValue())
        {
            this.setBlockBounds(0.3125F, 0.8125F, 0.3125F, 0.6875F, 1.0F, 0.6875F);
            super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
        }

        this.setBlockBoundsForItemRender();
    }

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

    public int quantityDropped(Random random)
    {
        return 4;
    }

    public void harvestBlock(World worldIn, EntityNPC player, BlockPos pos, State state, TileEntity te)
    {
        super.harvestBlock(worldIn, player, pos, state, te);
        if(state.getValue(ORB))
        	spawnAsEntity(worldIn, pos, new ItemStack(Items.charged_orb));
    }

    /**
     * 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()).withProperty(ORB, Boolean.valueOf(false));
    }

    public boolean hasComparatorInputOverride()
    {
        return true;
    }

    public int getComparatorInputOverride(World worldIn, BlockPos pos)
    {
        return ((Boolean)worldIn.getState(pos).getValue(ORB)).booleanValue() ? 15 : 0;
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(ORB, Boolean.valueOf((meta & 4) != 0)).withProperty(FACING, Facing.getHorizontal(meta & 3));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        int i = 0;
        i = i | ((Facing)state.getValue(FACING)).getHorizontalIndex();

        if (((Boolean)state.getValue(ORB)).booleanValue())
        {
            i |= 4;
        }

        return i;
    }

    protected Property[] getProperties()
    {
        return new Property[] {FACING, ORB};
    }

    public void onBlockDestroyedByPlayer(World worldIn, BlockPos pos, State state)
    {
    	for(int n = 0; n < 4; n++) {
	    	BlockPos ppos = pos.offset(Facing.getHorizontal(n));
	    	State pstate = worldIn.getState(ppos);
	    	if(pstate.getBlock() == Blocks.floor_portal)
	    		Blocks.floor_portal.onBlockDestroyedByPlayer(worldIn, ppos, pstate);
    	}
    }
    
    public boolean isXrayVisible()
    {
        return true;
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return (state.getValue(ORB) ? provider.getModel("portal_frame_side")
			.add(0, 0, 0, 16, 13, 16)
				.d("portal_frame_bottom").uv(0, 0, 16, 16).noCull()
				.u("portal_frame_top").uv(0, 0, 16, 16).noCull()
				.n().uv(0, 3, 16, 16).noCull()
				.s().uv(0, 3, 16, 16).noCull()
				.w().uv(0, 3, 16, 16).noCull()
				.e().uv(0, 3, 16, 16).noCull()
			.add(4, 13, 4, 12, 16, 12)
				.d("portal_frame_orb").uv(4, 4, 12, 12).noCull()
				.u("portal_frame_orb").uv(4, 4, 12, 12).noCull()
				.n("portal_frame_orb").uv(4, 0, 12, 3).noCull()
				.s("portal_frame_orb").uv(4, 0, 12, 3).noCull()
				.w("portal_frame_orb").uv(4, 0, 12, 3).noCull()
				.e("portal_frame_orb").uv(4, 0, 12, 3).noCull() : provider.getModel("portal_frame_side")
			.add(0, 0, 0, 16, 13, 16)
				.d("portal_frame_bottom").uv(0, 0, 16, 16).noCull()
				.u("portal_frame_top").uv(0, 0, 16, 16).noCull()
				.n().uv(0, 3, 16, 16).noCull()
				.s().uv(0, 3, 16, 16).noCull()
				.w().uv(0, 3, 16, 16).noCull()
				.e().uv(0, 3, 16, 16).noCull())
    			.rotate(ModelRotation.getNorthRot(state.getValue(FACING)));
    }
}
