package common.block;

import common.entity.npc.EntityNPC;
import common.init.Items;
import common.item.Item;
import common.material.Material;
import common.model.BlockLayer;
import common.model.Model;
import common.model.ModelProvider;
import common.properties.IProperty;
import common.properties.PropertyInteger;
import common.rng.Random;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.Facing;
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;

public class BlockCake extends Block
{
	private static final Model cake_slice3 = ModelProvider.getModelProvider().getModel("cake_side")
			.add(7, 0, 1, 15, 8, 15)
				.d("cake_bottom").uv(7, 1, 15, 15)
				.u("cake_top").uv(7, 1, 15, 15).noCull()
				.n().uv(1, 8, 9, 16).noCull()
				.s().uv(1, 8, 9, 16).noCull()
				.w("cake_inner").uv(1, 8, 15, 16).noCull()
				.e().uv(1, 8, 15, 16).noCull()
	;
	private static final Model cake_slice1 = ModelProvider.getModelProvider().getModel("cake_side")
			.add(3, 0, 1, 15, 8, 15)
				.d("cake_bottom").uv(3, 1, 15, 15)
				.u("cake_top").uv(3, 1, 15, 15).noCull()
				.n().uv(1, 8, 13, 16).noCull()
				.s().uv(1, 8, 13, 16).noCull()
				.w("cake_inner").uv(1, 8, 15, 16).noCull()
				.e().uv(1, 8, 15, 16).noCull()
	;
	private static final Model cake_slice2 = ModelProvider.getModelProvider().getModel("cake_side")
			.add(5, 0, 1, 15, 8, 15)
				.d("cake_bottom").uv(5, 1, 15, 15)
				.u("cake_top").uv(5, 1, 15, 15).noCull()
				.n().uv(1, 8, 11, 16).noCull()
				.s().uv(1, 8, 11, 16).noCull()
				.w("cake_inner").uv(1, 8, 15, 16).noCull()
				.e().uv(1, 8, 15, 16).noCull()
	;
	private static final Model cake_slice5 = ModelProvider.getModelProvider().getModel("cake_side")
			.add(11, 0, 1, 15, 8, 15)
				.d("cake_bottom").uv(11, 1, 15, 15)
				.u("cake_top").uv(11, 1, 15, 15).noCull()
				.n().uv(1, 8, 5, 16).noCull()
				.s().uv(1, 8, 5, 16).noCull()
				.w("cake_inner").uv(1, 8, 15, 16).noCull()
				.e().uv(1, 8, 15, 16).noCull()
	;
	private static final Model cake_slice4 = ModelProvider.getModelProvider().getModel("cake_side")
			.add(9, 0, 1, 15, 8, 15)
				.d("cake_bottom").uv(9, 1, 15, 15)
				.u("cake_top").uv(9, 1, 15, 15).noCull()
				.n().uv(1, 8, 7, 16).noCull()
				.s().uv(1, 8, 7, 16).noCull()
				.w("cake_inner").uv(1, 8, 15, 16).noCull()
				.e().uv(1, 8, 15, 16).noCull()
	;
	private static final Model cake_uneaten = ModelProvider.getModelProvider().getModel("cake_side")
			.add(1, 0, 1, 15, 8, 15)
				.d("cake_bottom").uv(1, 1, 15, 15)
				.u("cake_top").uv(1, 1, 15, 15).noCull()
				.n().uv(1, 8, 15, 16).noCull()
				.s().uv(1, 8, 15, 16).noCull()
				.w().uv(1, 8, 15, 16).noCull()
				.e().uv(1, 8, 15, 16).noCull()
	;
	private static final Model cake_slice6 = ModelProvider.getModelProvider().getModel("cake_side")
			.add(13, 0, 1, 15, 8, 15)
				.d("cake_bottom").uv(13, 1, 15, 15)
				.u("cake_top").uv(13, 1, 15, 15).noCull()
				.n().uv(1, 8, 3, 16).noCull()
				.s().uv(1, 8, 3, 16).noCull()
				.w("cake_inner").uv(1, 8, 15, 16).noCull()
				.e().uv(1, 8, 15, 16).noCull()
	;
	private static final Model[] cake_slices = 
			new Model[] {cake_uneaten, cake_slice1, cake_slice2, cake_slice3, cake_slice4, cake_slice5, cake_slice6};
	
    public static final PropertyInteger BITES = PropertyInteger.create("bites", 0, 6);

    public BlockCake()
    {
        super(Material.cake);
        this.setDefaultState(this.getBaseState().withProperty(BITES, Integer.valueOf(0)));
//        this.setTickRandomly(true);
    }

    public void setBlockBoundsBasedOnState(IWorldAccess worldIn, BlockPos pos)
    {
        float f = 0.0625F;
        float f1 = (float)(1 + ((Integer)worldIn.getState(pos).getValue(BITES)).intValue() * 2) / 16.0F;
        float f2 = 0.5F;
        this.setBlockBounds(f1, 0.0F, f, 1.0F - f, f2, 1.0F - f);
    }

    /**
     * Sets the block's bounds for rendering it as an item
     */
    public void setBlockBoundsForItemRender()
    {
        float f = 0.0625F;
        float f1 = 0.5F;
        this.setBlockBounds(f, 0.0F, f, 1.0F - f, f1, 1.0F - f);
    }

    public BoundingBox getCollisionBoundingBox(World worldIn, BlockPos pos, State state)
    {
        float f = 0.0625F;
        float f1 = (float)(1 + ((Integer)state.getValue(BITES)).intValue() * 2) / 16.0F;
        float f2 = 0.5F;
        return new BoundingBox((double)((float)pos.getX() + f1), (double)pos.getY(), (double)((float)pos.getZ() + f), (double)((float)(pos.getX() + 1) - f), (double)((float)pos.getY() + f2), (double)((float)(pos.getZ() + 1) - f));
    }

    public BoundingBox getSelectedBoundingBox(World worldIn, BlockPos pos)
    {
        return this.getCollisionBoundingBox(worldIn, pos, worldIn.getState(pos));
    }

    public boolean isFullCube()
    {
        return false;
    }

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

    public boolean onBlockActivated(World worldIn, BlockPos pos, State state, EntityNPC playerIn, Facing side, float hitX, float hitY, float hitZ)
    {
        this.eatCake(worldIn, pos, state, playerIn);
        return true;
    }

    public void onBlockClicked(World worldIn, BlockPos pos, EntityNPC playerIn)
    {
        this.eatCake(worldIn, pos, worldIn.getState(pos), playerIn);
    }

    private void eatCake(World worldIn, BlockPos pos, State state, EntityNPC player)
    {
//        player.triggerAchievement(StatRegistry.cakeEatenStat);
        int i = ((Integer)state.getValue(BITES)).intValue();

        if (i < 6)
        {
            worldIn.setState(pos, state.withProperty(BITES, Integer.valueOf(i + 1)), 3);
        }
        else
        {
            worldIn.setBlockToAir(pos);
        }
    }

    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        return super.canPlaceBlockAt(worldIn, pos) ? this.canBlockStay(worldIn, pos) : false;
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
    {
        if (!this.canBlockStay(worldIn, pos))
        {
            worldIn.setBlockToAir(pos);
        }
    }

    private boolean canBlockStay(World worldIn, BlockPos pos)
    {
        return worldIn.getState(pos.down()).getBlock().getMaterial().isSolid();
    }

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

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

    public Item getItem(World worldIn, BlockPos pos)
    {
        return Items.cake;
    }

    public BlockLayer getBlockLayer()
    {
        return BlockLayer.CUTOUT;
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(BITES, Integer.valueOf(meta > 6 ? 0 : meta));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((Integer)state.getValue(BITES)).intValue();
    }

    protected IProperty[] getProperties()
    {
        return new IProperty[] {BITES};
    }

    public int getComparatorInputOverride(World worldIn, BlockPos pos)
    {
        return (7 - ((Integer)worldIn.getState(pos).getValue(BITES)).intValue()) * 2;
    }

    public boolean hasComparatorInputOverride()
    {
        return true;
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return cake_slices[state.getValue(BITES)];
    }
}
