package common.item;

import java.util.List;

import common.block.Block;
import common.block.BlockBed;
import common.block.IGrowable;
import common.color.DyeColor;
import common.entity.animal.EntitySheep;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.init.BlockRegistry;
import common.init.Blocks;
import common.material.Material;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ParticleType;
import common.tileentity.TileEntity;
import common.tileentity.TileEntityBeacon;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class ItemDye extends Item
{
    public static final int[] dyeColors = new int[] {1973019, 11743532, 3887386, 5320730, 2437522, 8073150, 2651799, 11250603, 4408131, 14188952, 4312372, 14602026, 6719955, 12801229, 15435844, 15790320};

    public ItemDye()
    {
        this.setHasSubtypes(true);
        this.setMaxDamage(0);
        this.setTab(CheatTab.tabMaterials);
    }

    /**
     * Returns the unlocalized name of this item. This version accepts an ItemStack so different stacks can have
     * different names based on their damage or NBT.
     */
    public String getDisplay(ItemStack stack)
    {
        int i = stack.getMetadata();
        return DyeColor.byDyeDamage(i).getDye();
    }

    /**
     * Called when a Block is right-clicked with this Item
     */
    public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
    {
        if (!playerIn.canPlayerEdit(pos.offset(side), side, stack))
        {
            return false;
        }
        else
        {
            DyeColor enumdyecolor = DyeColor.byDyeDamage(stack.getMetadata());

            if (enumdyecolor == DyeColor.WHITE)
            {
                if (applyBonemeal(stack, worldIn, pos))
                {
                    if (!worldIn.client)
                    {
                        worldIn.playAuxSFX(2005, pos, 0);
                    }

                    return true;
                }
            }
            else if (enumdyecolor == DyeColor.BROWN)
            {
                State iblockstate = worldIn.getState(pos);
                Block block = iblockstate.getBlock();

                if (block == Blocks.jungle_log) // && iblockstate.getValue(BlockPlanks.VARIANT) == BlockPlanks.EnumType.JUNGLE)
                {
                    if (side == Facing.DOWN)
                    {
                        return false;
                    }

                    if (side == Facing.UP)
                    {
                        return false;
                    }

                    pos = pos.offset(side);

                    if (worldIn.isAirBlock(pos))
                    {
                        State iblockstate1 = Blocks.cocoa.onBlockPlaced(worldIn, pos, side, hitX, hitY, hitZ, 0, playerIn);
                        worldIn.setState(pos, iblockstate1, 2);

//                        if (!playerIn.creative)
//                        {
                        --stack.stackSize;
//                        }
                    }

                    return true;
                }
            }
            State iblockstate = worldIn.getState(pos);
            if(iblockstate.getBlock() instanceof BlockBed) {
	            Block bedBlock = BlockRegistry.getByNameOrId(enumdyecolor.getName() + "_bed");
	            if(bedBlock != null) {
	                if (iblockstate.getValue(BlockBed.PART) == BlockBed.EnumPartType.FOOT)
	                {
	                	pos = pos.offset(iblockstate.getValue(BlockBed.FACING));
	                    iblockstate = worldIn.getState(pos);
	                    if(!(iblockstate.getBlock() instanceof BlockBed)) // || iblockstate.getValue(BlockBed.OCCUPIED))
	                        return false;
	                }
	                else {
//	                    if(iblockstate.getValue(BlockBed.OCCUPIED))
//	                        return false;
	                	if(!(worldIn.getState(pos.offset(iblockstate.getValue(BlockBed.FACING).getOpposite())).getBlock() instanceof BlockBed))
	                		return false;
	                }
	                State iblockstate1 = bedBlock.getState()
	                		.withProperty(BlockBed.FACING, iblockstate.getValue(BlockBed.FACING))
	                		.withProperty(BlockBed.PART, BlockBed.EnumPartType.FOOT);
//	                		.withProperty(BlockBed.OCCUPIED, false);
	                if(worldIn.setState(pos.offset(iblockstate.getValue(BlockBed.FACING).getOpposite()), iblockstate1, 2)) {
	                	iblockstate1 = iblockstate1.withProperty(BlockBed.PART, BlockBed.EnumPartType.HEAD);
	                    worldIn.setState(pos, iblockstate1, 2);
	                }
//	                if(!playerIn.creative)
	                --stack.stackSize;
	                return true;
	            }
            }
            else if(iblockstate.getBlock() == Blocks.beacon) {
            	TileEntity te = worldIn.getTileEntity(pos);
            	if(te instanceof TileEntityBeacon) {
            		((TileEntityBeacon)te).setBeamColor(enumdyecolor);
//	                if(!playerIn.creative)
	                --stack.stackSize;
            	}
            }

            return false;
        }
    }

    public static boolean applyBonemeal(ItemStack stack, World worldIn, BlockPos target)
    {
        State iblockstate = worldIn.getState(target);

        if (iblockstate.getBlock() instanceof IGrowable)
        {
            IGrowable igrowable = (IGrowable)iblockstate.getBlock();

            if (igrowable.canGrow(worldIn, target, iblockstate, worldIn.client))
            {
                if (!worldIn.client)
                {
                    if (igrowable.canUseBonemeal(worldIn, worldIn.rand, target, iblockstate))
                    {
                        igrowable.grow((AWorldServer)worldIn, worldIn.rand, target, iblockstate);
                    }

                    --stack.stackSize;
                }

                return true;
            }
        }

        return false;
    }

    public static void spawnBonemealParticles(World worldIn, BlockPos pos, int amount)
    {
        if (amount == 0)
        {
            amount = 15;
        }

        Block block = worldIn.getState(pos).getBlock();

        if (block.getMaterial() != Material.air)
        {
            block.setBlockBoundsBasedOnState(worldIn, pos);

            for (int i = 0; i < amount; ++i)
            {
                double d0 = itemRand.gaussian() * 0.02D;
                double d1 = itemRand.gaussian() * 0.02D;
                double d2 = itemRand.gaussian() * 0.02D;
                worldIn.spawnParticle(ParticleType.GROW, (double)((float)pos.getX() + itemRand.floatv()), (double)pos.getY() + (double)itemRand.floatv() * block.getBlockBoundsMaxY(), (double)((float)pos.getZ() + itemRand.floatv()), d0, d1, d2);
            }
        }
    }

    /**
     * Returns true if the item can be used on the given entity, e.g. shears on sheep.
     */
    public boolean itemInteractionForEntity(ItemStack stack, EntityNPC playerIn, EntityLiving target)
    {
        if (target instanceof EntitySheep)
        {
            EntitySheep entitysheep = (EntitySheep)target;
            DyeColor enumdyecolor = DyeColor.byDyeDamage(stack.getMetadata());

            if (!entitysheep.getSheared() && entitysheep.getFleeceColor() != enumdyecolor)
            {
                entitysheep.setFleeceColor(enumdyecolor);
                --stack.stackSize;
            }

            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
     */
    public void getSubItems(Item itemIn, CheatTab tab, List<ItemStack> subItems)
    {
        for (int i = 0; i < 16; ++i)
        {
            subItems.add(new ItemStack(itemIn, 1, i));
        }
    }

    public Model getModel(ModelProvider provider, String name, int meta) {
    	return provider.getModel(this.getTransform(), "dye_" + DyeColor.byDyeDamage(meta).getName());
    }
}
