package common.block.artificial;

import java.util.List;

import common.block.Block;
import common.block.Material;
import common.block.foliage.BlockFlower;
import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.init.BlockRegistry;
import common.init.Blocks;
import common.init.Items;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.item.block.ItemBlock;
import common.item.block.ItemSmallBlock;
import common.model.BlockLayer;
import common.model.Model;
import common.model.ModelProvider;
import common.rng.Random;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockFlowerPot extends Block
{
	private static final Model flower_pot_cactus = ModelProvider.getModelProvider().getModel("flower_pot")
			.add(5, 0, 5, 6, 6, 11)
				.d().uv(5, 5, 6, 11)
				.u().uv(5, 5, 6, 11).noCull()
				.n().uv(10, 10, 11, 16).noCull()
				.s().uv(5, 10, 6, 16).noCull()
				.w().uv(5, 10, 11, 16).noCull()
				.e().uv(5, 10, 11, 16).noCull()
			.add(10, 0, 5, 11, 6, 11)
				.d().uv(10, 5, 11, 11)
				.u().uv(10, 5, 11, 11).noCull()
				.n().uv(5, 10, 6, 16).noCull()
				.s().uv(10, 10, 11, 16).noCull()
				.w().uv(5, 10, 11, 16).noCull()
				.e().uv(5, 10, 11, 16).noCull()
			.add(6, 0, 5, 10, 6, 6)
				.d().uv(6, 10, 10, 11)
				.u().uv(6, 5, 10, 6).noCull()
				.n().uv(6, 10, 10, 16).noCull()
				.s().uv(6, 10, 10, 16).noCull()
			.add(6, 0, 10, 10, 6, 11)
				.d().uv(6, 5, 10, 6)
				.u().uv(6, 10, 10, 11).noCull()
				.n().uv(6, 10, 10, 16).noCull()
				.s().uv(6, 10, 10, 16).noCull()
			.add(6, 0, 6, 10, 4, 10)
				.d().uv(6, 6, 10, 10)
				.u("dirt").uv(6, 6, 10, 10).noCull()
			.add(6, 4, 6, 10, 8, 10)
				.n("cactus_side").uv(6, 8, 10, 12).noCull()
				.s("cactus_side").uv(6, 8, 10, 12).noCull()
				.w("cactus_side").uv(6, 8, 10, 12).noCull()
				.e("cactus_side").uv(6, 8, 10, 12).noCull()
			.add(6, 8, 6, 10, 12, 10)
				.n("cactus_side").uv(6, 4, 10, 8).noCull()
				.s("cactus_side").uv(6, 4, 10, 8).noCull()
				.w("cactus_side").uv(6, 4, 10, 8).noCull()
				.e("cactus_side").uv(6, 4, 10, 8).noCull()
			.add(6, 12, 6, 10, 16, 10)
				.u("cactus_side").uv(6, 6, 10, 10).noCull()
				.n("cactus_side").uv(6, 0, 10, 4).noCull()
				.s("cactus_side").uv(6, 0, 10, 4).noCull()
				.w("cactus_side").uv(6, 0, 10, 4).noCull()
				.e("cactus_side").uv(6, 0, 10, 4).noCull()
	;
	private static final Model flower_pot = ModelProvider.getModelProvider().getModel("flower_pot")
			.add(5, 0, 5, 6, 6, 11)
				.d().uv(5, 5, 6, 11)
				.u().uv(5, 5, 6, 11).noCull()
				.n().uv(10, 10, 11, 16).noCull()
				.s().uv(5, 10, 6, 16).noCull()
				.w().uv(5, 10, 11, 16).noCull()
				.e().uv(5, 10, 11, 16).noCull()
			.add(10, 0, 5, 11, 6, 11)
				.d().uv(10, 5, 11, 11)
				.u().uv(10, 5, 11, 11).noCull()
				.n().uv(5, 10, 6, 16).noCull()
				.s().uv(10, 10, 11, 16).noCull()
				.w().uv(5, 10, 11, 16).noCull()
				.e().uv(5, 10, 11, 16).noCull()
			.add(6, 0, 5, 10, 6, 6)
				.d().uv(6, 10, 10, 11)
				.u().uv(6, 5, 10, 6).noCull()
				.n().uv(6, 10, 10, 16).noCull()
				.s().uv(6, 10, 10, 16).noCull()
			.add(6, 0, 10, 10, 6, 11)
				.d().uv(6, 5, 10, 6)
				.u().uv(6, 10, 10, 11).noCull()
				.n().uv(6, 10, 10, 16).noCull()
				.s().uv(6, 10, 10, 16).noCull()
			.add(6, 0, 6, 10, 4, 10)
				.d().uv(6, 6, 10, 10)
				.u("dirt").uv(6, 6, 10, 10).noCull()
	;
	
	public static final List<BlockFlowerPot> POTS = Lists.newArrayList();
	
    private final Block content;

    public BlockFlowerPot(Block content)
    {
        super(Material.SMALL);
        this.content = content;
        this.setBlockBoundsForItemRender();
        POTS.add(this);
    }

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

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

    public boolean isFullCube()
    {
        return false;
    }

    public boolean onBlockActivated(World worldIn, BlockPos pos, State state, EntityNPC playerIn, Facing side, float hitX, float hitY, float hitZ)
    {
        ItemStack itemstack = playerIn.inventory.getCurrentItem();

        if (itemstack != null && itemstack.getItem() instanceof ItemBlock)
        {
            if (this.content != null)
            {
                return false;
            }
            else
            {
                Block block = itemstack.getItem().getBlock();

                if (!(block instanceof BlockFlower) && block != Blocks.cactus)
                {
                    return false;
                }
                else
                {
                	worldIn.setState(pos, BlockRegistry.getRegisteredBlock("flowerpot_" + BlockRegistry.getNameFromBlock(block)).getState(), 2);
//                    tileentityflowerpot.setFlowerPotData(itemstack.getItem(), itemstack.getMetadata());
//                    tileentityflowerpot.markDirty();
//                    worldIn.markBlockForUpdate(pos);
//                    playerIn.triggerAchievement(StatRegistry.flowerPottedStat);

                    if (/* !playerIn.creative && */ --itemstack.size <= 0)
                    {
                        playerIn.inventory.setInventorySlotContents(playerIn.inventory.currentItem, (ItemStack)null);
                    }

                    return true;
                }
            }
        }
        else
        {
            return false;
        }
    }

    public Item getItem(World worldIn, BlockPos pos)
    {
        return worldIn.getState(pos).getBlock() == this && this.content != null ? this.content.getItem() : Items.flowerpot;
    }
    
    public boolean isPickStrict()
    {
        return true;
    }

    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        return super.canPlaceBlockAt(worldIn, pos) && worldIn.isBlockSolid(pos.down());
    }

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

    public void onBlockRemoved(AWorldServer worldIn, BlockPos pos, State state)
    {
        if(this.content != null)
            spawnAsEntity(worldIn, pos, new ItemStack(this.content.getItem()));
        super.onBlockRemoved(worldIn, pos, state);
    }

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

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

    public Model getModel(ModelProvider provider, String name, State state) {
    	if(this.content == null)
    		return flower_pot;
    	else if(this.content == Blocks.cactus)
    		return flower_pot_cactus;
    	else {
    		String plant = BlockRegistry.getNameFromBlock(this.content);
    		return provider.getModel("flower_pot")
    				.add(5, 0, 5, 6, 6, 11)
    					.d().uv(5, 5, 6, 11)
    					.u().uv(5, 5, 6, 11).noCull()
    					.n().uv(10, 10, 11, 16).noCull()
    					.s().uv(5, 10, 6, 16).noCull()
    					.w().uv(5, 10, 11, 16).noCull()
    					.e().uv(5, 10, 11, 16).noCull()
    				.add(10, 0, 5, 11, 6, 11)
    					.d().uv(10, 5, 11, 11)
    					.u().uv(10, 5, 11, 11).noCull()
    					.n().uv(5, 10, 6, 16).noCull()
    					.s().uv(10, 10, 11, 16).noCull()
    					.w().uv(5, 10, 11, 16).noCull()
    					.e().uv(5, 10, 11, 16).noCull()
    				.add(6, 0, 5, 10, 6, 6)
    					.d().uv(6, 10, 10, 11)
    					.u().uv(6, 5, 10, 6).noCull()
    					.n().uv(6, 10, 10, 16).noCull()
    					.s().uv(6, 10, 10, 16).noCull()
    				.add(6, 0, 10, 10, 6, 11)
    					.d().uv(6, 5, 10, 6)
    					.u().uv(6, 10, 10, 11).noCull()
    					.n().uv(6, 10, 10, 16).noCull()
    					.s().uv(6, 10, 10, 16).noCull()
    				.add(6, 0, 6, 10, 4, 10)
    					.d().uv(6, 6, 10, 10)
    					.u("dirt").uv(6, 6, 10, 10).noCull()
    				.add(2.6f, 4, 8, 13.4f, 16, 8).rotate(8, 8, 8, Facing.Axis.Y, 45, true)
    					.n(plant).uv(0, 0, 16, 16).noCull()
    					.s(plant).uv(0, 0, 16, 16).noCull()
    				.add(8, 4, 2.6f, 8, 16, 13.4f).rotate(8, 8, 8, Facing.Axis.Y, 45, true)
    					.w(plant).uv(0, 0, 16, 16).noCull()
    					.e(plant).uv(0, 0, 16, 16).noCull();
    	}
    }
	
	protected Item getItemToRegister() {
		return this.content == null ? new ItemSmallBlock(this).setDisplay("Blumentopf").setTab(CheatTab.DECORATION) : null;
	}
}
