package common.item.material;

import common.block.Block;
import common.block.artificial.BlockBed;
import common.block.foliage.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.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
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.util.Vec3;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class ItemDye extends Item
{
    public static final int[] FIREWORK_COLORS = new int[] {1973019, 11743532, 3887386, 5320730, 2437522, 8073150, 2651799, 11250603, 4408131, 14188952, 4312372, 14602026, 6719955, 12801229, 15435844, 15790320};
    private static final ItemDye[] DIES = new ItemDye[DyeColor.values().length];
    
    private final DyeColor color;
    
    public static ItemDye getByColor(DyeColor color) {
    	return DIES[color.getMetadata()];
    }

    public ItemDye(DyeColor color)
    {
    	this.color = color;
        this.setTab(CheatTab.MATERIALS);
        DIES[this.color.getMetadata()] = this;
    }
    
    public DyeColor getColor() {
    	return this.color;
    }

    /**
     * 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
        {
            if (this.color == DyeColor.WHITE)
            {
                if (applyBonemeal(stack, worldIn, pos))
                {
                    if (!worldIn.client)
                    {
                        worldIn.playAuxSFX(2005, pos, 0);
                    }

                    return true;
                }
            }
            else if (this.color == 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, playerIn);
                        worldIn.setState(pos, iblockstate1, 2);

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

                    return true;
                }
            }
            State iblockstate = worldIn.getState(pos);
            if(iblockstate.getBlock() instanceof BlockBed) {
	            Block bedBlock = BlockRegistry.getRegisteredBlock(this.color.getName() + "_bed");
	            if(bedBlock != Blocks.air) {
	                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.size;
	                return true;
	            }
            }
            else if(iblockstate.getBlock() == Blocks.beacon) {
            	TileEntity te = worldIn.getTileEntity(pos);
            	if(te instanceof TileEntityBeacon) {
            		((TileEntityBeacon)te).setBeamColor(this.color);
//	                if(!playerIn.creative)
	                --stack.size;
            	}
            }

            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.size;
                }

                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 != Blocks.air)
        {
            block.setBlockBoundsBasedOnState(worldIn, pos);

            for (int i = 0; i < amount; ++i)
            {
                double d0 = worldIn.rand.gaussian() * 0.02D;
                double d1 = worldIn.rand.gaussian() * 0.02D;
                double d2 = worldIn.rand.gaussian() * 0.02D;
                worldIn.spawnParticle(ParticleType.GROW, (double)((float)pos.getX() + worldIn.rand.floatv()), (double)pos.getY() + (double)worldIn.rand.floatv() * block.getBlockBoundsMaxY(), (double)((float)pos.getZ() + worldIn.rand.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;

            if (!entitysheep.getSheared() && entitysheep.getFleeceColor() != this.color)
            {
                entitysheep.setFleeceColor(this.color);
                --stack.size;
            }

            return true;
        }
        else
        {
            return false;
        }
    }

    public Model getModel(ModelProvider provider, String name) {
    	return provider.getModel(this.getTransform(), "dye_" + this.color.getName());
    }
    
    public ItemStack dispenseStack(World world, TileEntity source, Vec3 position, BlockPos blockpos, Facing facing, ItemStack stack) {
    	if(this.color != DyeColor.WHITE)
    		return super.dispenseStack(world, source, position, blockpos, facing, stack);
        BlockPos pos = blockpos = blockpos.offset(facing);

        if (applyBonemeal(stack, world, pos))
        {
            if (!world.client)
            {
                world.playAuxSFX(2005, pos, 0);
            }
            world.playAuxSFX(1000, blockpos, 0);
        }
        else
        {
            world.playAuxSFX(1001, blockpos, 0);
        }

        return stack;
	}

	public int getDispenseSoundId() {
		return this.color == DyeColor.WHITE ? 0 : super.getDispenseSoundId();
	}
}
