package common.block;

import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.ItemRegistry;
import common.init.Items;
import common.item.Item;
import common.item.ItemBlock;
import common.item.ItemStack;
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.Facing;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockFlowerPot extends Block // Container
{
	private static final Model flower_pot_cactus = ModelProvider.getModelProvider().getModel("flower_pot").noOcclude()
			.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").noOcclude()
			.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 PropertyInteger CONTENTS = PropertyInteger.create("contents", 0, 1 + BlockFlower.EnumFlowerType.values().length);

    public BlockFlowerPot()
    {
        super(Material.circuits);
        this.setDefaultState(this.getBaseState().withProperty(CONTENTS, 0)); // .withProperty(LEGACY_DATA, Integer.valueOf(0)));
        this.setBlockBoundsForItemRender();
    }

//    /**
//     * Gets the localized name of this block. Used for the statistics page.
//     */
//    public String getLocalizedName()
//    {
//        return "Blumentopf";
//    }

    /**
     * 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;
    }

//    /**
//     * 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 3;
//    }

    public boolean isFullCube()
    {
        return false;
    }

//    public int colorMultiplier(IWorldAccess worldIn, BlockPos pos, int renderPass)
//    {
//        TileEntity tileentity = worldIn.getTileEntity(pos);
//
//        if (tileentity instanceof TileEntityFlowerPot)
//        {
//            Item item = ((TileEntityFlowerPot)tileentity).getFlowerPotItem();
//
//            if (item instanceof ItemBlock)
//            {
//                return BlockRegistry.getBlockFromItem(item).colorMultiplier(worldIn, pos, renderPass);
//            }
//        }
//
//        return 16777215;
//    }

    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 (state.getValue(CONTENTS) > 0)
            {
                return false;
            }
            else
            {
                Block block = itemstack.getItem().getBlock();

                if (block != Blocks.flower && block != Blocks.cactus)
                {
                    return false;
                }
                else
                {
                	worldIn.setState(pos, state.withProperty(CONTENTS, block == Blocks.cactus ? 1 : (2 + (itemstack.getMetadata() >= 
                			BlockFlower.EnumFlowerType.values().length ? 0 : itemstack.getMetadata()))), 2);
//                    tileentityflowerpot.setFlowerPotData(itemstack.getItem(), itemstack.getMetadata());
//                    tileentityflowerpot.markDirty();
//                    worldIn.markBlockForUpdate(pos);
//                    playerIn.triggerAchievement(StatRegistry.flowerPottedStat);

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

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

    public Item getItem(World worldIn, BlockPos pos)
    {
        State state = worldIn.getState(pos);
        return state.getBlock() == this ? (state.getValue(CONTENTS) == 1 ? ItemRegistry.getItemFromBlock(Blocks.cactus) : 
        	(state.getValue(CONTENTS) >= 2 ? ItemRegistry.getItemFromBlock(Blocks.flower) : Items.flower_pot)) : Items.flower_pot;
    }

    /**
     * Gets the meta to use for the Pick Block ItemStack result
     */
    public int getDamageValue(World worldIn, BlockPos pos)
    {
        State state = worldIn.getState(pos);
        return state.getBlock() == this && state.getValue(CONTENTS) >= 2 ? state.getValue(CONTENTS) - 2 : 0;
    }
    
    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)
    {
        int type = state.getValue(CONTENTS);
        if(type > 0)
            spawnAsEntity(worldIn, pos, new ItemStack(type == 1 ? Blocks.cactus : Blocks.flower, 1, type == 1 ? 0 : (type - 2)));
        super.onBlockRemoved(worldIn, pos, state);
    }

//    public void onBlockHarvested(World worldIn, BlockPos pos, IBlockState state, EntityNPC player)
//    {
//        super.onBlockHarvested(worldIn, pos, state, player);
//
//        if (player.capabilities.isCreativeMode)
//        {
//            TileEntityFlowerPot tileentityflowerpot = this.getTileEntity(worldIn, pos);
//
//            if (tileentityflowerpot != null)
//            {
//                tileentityflowerpot.setFlowerPotData((Item)null, 0);
//            }
//        }
//    }

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

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(CONTENTS, Math.min(meta, BlockFlower.EnumFlowerType.values().length + 1));
    }

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

//    private TileEntityFlowerPot getTileEntity(World worldIn, BlockPos pos)
//    {
//        TileEntity tileentity = worldIn.getTileEntity(pos);
//        return tileentity instanceof TileEntityFlowerPot ? (TileEntityFlowerPot)tileentity : null;
//    }
//
//    /**
//     * 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 TileEntityFlowerPot(null, 0);
//    }

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

//    /**
//     * Get the actual Block state of this Block at the given position. This applies properties not visible in the
//     * metadata, such as fence connections.
//     */
//    public IBlockState getActualState(IBlockState state, IWorldAccess worldIn, BlockPos pos)
//    {
//        int type = 0;
//        TileEntity tileentity = worldIn.getTileEntity(pos);
//
//        if (tileentity instanceof TileEntityFlowerPot)
//        {
//            TileEntityFlowerPot tileentityflowerpot = (TileEntityFlowerPot)tileentity;
//            Item item = tileentityflowerpot.getFlowerPotItem();
//
//            if (item instanceof ItemBlock)
//            {
//                int i = tileentityflowerpot.getFlowerPotData();
//                Block block = BlockRegistry.getBlockFromItem(item);
//                if (block == Blocks.flower)
//                {
//                    
//                }
//                else if (block == Blocks.cactus)
//                {
//                	type = 1;
//                }
//            }
//        }
//
//        return state.withProperty(CONTENTS, type);
//    }

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

    public Model getModel(ModelProvider provider, String name, State state) {
    	switch(state.getValue(CONTENTS)) {
    	case 0:
    		return flower_pot;
    	case 1:
    		return flower_pot_cactus;
//    	case FERN:
//    		return flower_pot_fern;
    	default:
    		String plant = BlockFlower.EnumFlowerType.getType(BlockFlower.EnumFlowerColor.BASE, state.getValue(CONTENTS) - 2).getName().toLowerCase();
    		return provider.getModel("flower_pot").noOcclude()
    				.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();
    	}
    }
}
