package common.block;

import java.util.List;

import common.entity.item.EntityFalling;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.inventory.Container;
import common.inventory.ContainerRepair;
import common.inventory.InventoryPlayer;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.material.Material;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.model.Transforms;
import common.properties.IProperty;
import common.properties.PropertyDirection;
import common.properties.PropertyInteger;
import common.tileentity.IInteractionObject;
import common.util.BlockPos;
import common.util.Facing;
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;

public class BlockAnvil extends BlockFalling
{
    public static final PropertyDirection FACING = PropertyDirection.create("facing", Facing.Plane.HORIZONTAL);
    public static final PropertyInteger DAMAGE = PropertyInteger.create("damage", 0, 2);

    public BlockAnvil()
    {
        super(Material.anvil);
        this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH).withProperty(DAMAGE, Integer.valueOf(0)));
        this.setLightOpacity(0);
        this.setTab(CheatTab.tabTech);
    }

    public boolean isFullCube()
    {
        return false;
    }

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

    /**
     * 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, int meta, EntityLiving placer)
    {
        Facing enumfacing = placer.getHorizontalFacing().rotateY();
        return super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer).withProperty(FACING, enumfacing).withProperty(DAMAGE, Integer.valueOf(meta >> 2));
    }

    public boolean onBlockActivated(World worldIn, BlockPos pos, State state, EntityNPC playerIn, Facing side, float hitX, float hitY, float hitZ)
    {
        if (!worldIn.client)
        {
            playerIn.displayGui(new BlockAnvil.Anvil(worldIn, pos));
        }

        return true;
    }

    /**
     * Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
     * returns the metadata of the dropped item based on the old metadata of the block.
     */
    public int damageDropped(State state)
    {
        return ((Integer)state.getValue(DAMAGE)).intValue();
    }

    public void setBlockBoundsBasedOnState(IWorldAccess worldIn, BlockPos pos)
    {
        Facing enumfacing = (Facing)worldIn.getState(pos).getValue(FACING);

        if (enumfacing.getAxis() == Facing.Axis.X)
        {
            this.setBlockBounds(0.0F, 0.0F, 0.125F, 1.0F, 1.0F, 0.875F);
        }
        else
        {
            this.setBlockBounds(0.125F, 0.0F, 0.0F, 0.875F, 1.0F, 1.0F);
        }
    }

    /**
     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
     */
    public void getSubBlocks(Item itemIn, CheatTab tab, List<ItemStack> list)
    {
        list.add(new ItemStack(itemIn, 1, 0));
        list.add(new ItemStack(itemIn, 1, 1));
        list.add(new ItemStack(itemIn, 1, 2));
    }

    protected void onStartFalling(EntityFalling fallingEntity)
    {
        fallingEntity.setHurtEntities(true);
    }

    public void onEndFalling(World worldIn, BlockPos pos)
    {
        worldIn.playAuxSFX(1022, pos, 0);
    }

    public boolean shouldSideBeRendered(IWorldAccess worldIn, BlockPos pos, Facing side)
    {
        return true;
    }

    /**
     * Possibly modify the given BlockState before rendering it on an Entity (Minecarts, Endermen, ...)
     */
    public State getStateForEntityRender(State state)
    {
        return this.getState().withProperty(FACING, Facing.SOUTH);
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(FACING, Facing.getHorizontal(meta & 3)).withProperty(DAMAGE, Integer.valueOf((meta & 12) == 12 ? 0 : ((meta & 12) >> 2)));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        int i = 0;
        i = i | ((Facing)state.getValue(FACING)).getHorizontalIndex();
        i = i | ((Integer)state.getValue(DAMAGE)).intValue() << 2;
        return i;
    }

    protected IProperty[] getProperties()
    {
        return new IProperty[] {FACING, DAMAGE};
    }

    public Transforms getTransform() {
    	return Transforms.ANVIL;
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return provider.getModel("anvil_base")
    			.add(2, 0, 2, 14, 4, 14).d().uv(2, 2, 14, 14).rot(180).u().uv(2, 2, 14, 14).rot(180).noCull()
    			.ns().uv(2, 12, 14, 16).noCull().w().uv(0, 2, 4, 14).rot(90).noCull().e().uv(4, 2, 0, 14).rot(270).noCull()
    			.add(4, 4, 3, 12, 5, 13).du().uv(4, 3, 12, 13).rot(180).noCull()
    			.ns().uv(4, 11, 12, 12).noCull().w().uv(4, 3, 5, 13).rot(90).noCull().e().uv(5, 3, 4, 13).rot(270).noCull()
    			.add(6, 5, 4, 10, 10, 12).du().uv(10, 12, 6, 4).rot(180).noCull()
    			.ns().uv(6, 6, 10, 11).noCull().w().uv(5, 4, 10, 12).rot(90).noCull().e().uv(10, 4, 5, 12).rot(270).noCull()
    			.add(3, 10, 0, 13, 16, 16).d().uv(3, 0, 13, 16).rot(180).noCull().u("anvil_top_" + state.getValue(DAMAGE))
    			.uv(3, 0, 13, 16).rot(180).noCull()
    			.ns().uv(3, 0, 13, 6).noCull().w().uv(10, 0, 16, 16).rot(90).noCull().e().uv(16, 0, 10, 16).rot(270).noCull()
    			.rotate(ModelRotation.getNorthRot(state.getValue(FACING)));
    }

    public static class Anvil implements IInteractionObject
    {
        private final World world;
        private final BlockPos position;

        public Anvil(World worldIn, BlockPos pos)
        {
            this.world = worldIn;
            this.position = pos;
        }

        public String getName()
        {
            return "anvil";
        }

        public boolean hasCustomName()
        {
            return false;
        }

        public String getCommandName()
        {
            return "Amboss";
        }

        public Container createContainer(InventoryPlayer playerInventory, EntityNPC playerIn)
        {
            return new ContainerRepair(playerInventory, this.world, this.position, playerIn);
        }

        public String getGuiID()
        {
            return "anvil";
        }
    }
}
