/*
 * Caveworld
 *
 * Copyright (c) 2014 kegare
 * https://github.com/kegare
 *
 * This mod is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL.
 * Please check the contents of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt
 */

package com.kegare.caveworld.block;

import net.minecraft.block.Block;
import net.minecraft.block.BlockDispenser;
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
import net.minecraft.dispenser.IBlockSource;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraft.world.World;
import net.minecraftforge.common.ChestGenHooks;
import net.minecraftforge.oredict.OreDictionary;

import com.kegare.caveworld.core.Config;
import com.kegare.caveworld.item.ItemPortalCaveworld;
import com.kegare.caveworld.item.ItemRope;

import cpw.mods.fml.common.registry.GameRegistry;

public class CaveBlock
{
	public static BlockPortalCaveworld portalCaveworld;
	public static BlockRope rope;

	public static void initialize()
	{
		portalCaveworld = new BlockPortalCaveworld(Config.portalCaveworld, "portalCaveworld");

		if (Config.rope > 0)
		{
			rope = new BlockRope(Config.rope, "rope");
		}
	}

	public static void register()
	{
		GameRegistry.registerBlock(portalCaveworld, ItemPortalCaveworld.class, "caveworld_portal");

		OreDictionary.registerOre("portalCaveworld", portalCaveworld);

		BlockDispenser.dispenseBehaviorRegistry.putObject(Item.itemsList[portalCaveworld.blockID], new BehaviorDefaultDispenseItem()
		{
			@Override
			public ItemStack dispenseStack(IBlockSource blockSource, ItemStack itemstack)
			{
				EnumFacing facing = BlockDispenser.getFacing(blockSource.getBlockMetadata());
				World world = blockSource.getWorld();
				int x = blockSource.getXInt() + facing.getFrontOffsetX();
				int y = blockSource.getYInt() + facing.getFrontOffsetY();
				int z = blockSource.getZInt() + facing.getFrontOffsetZ();

				if (portalCaveworld.tryToCreatePortal(world, x, y, z))
				{
					--itemstack.stackSize;
				}

				return itemstack;
			}

			@Override
			public void playDispenseSound(IBlockSource blockSource)
			{
				super.playDispenseSound(blockSource);

				blockSource.getWorld().playSoundEffect(blockSource.getXInt(), blockSource.getYInt(), blockSource.getZInt(), portalCaveworld.stepSound.getPlaceSound(), 1.0F, 2.0F);
			}
		});

		if (rope != null)
		{
			GameRegistry.registerBlock(rope, ItemRope.class, "rope");

			Block.setBurnProperties(rope.blockID, 15, 100);

			OreDictionary.registerOre("rope", new ItemStack(rope));

			ChestGenHooks.addItem(ChestGenHooks.BONUS_CHEST, new WeightedRandomChestContent(rope.blockID, 0, 2, 5, 10));
			ChestGenHooks.addItem(ChestGenHooks.MINESHAFT_CORRIDOR, new WeightedRandomChestContent(rope.blockID, 0, 3, 6, 10));
			ChestGenHooks.addItem(ChestGenHooks.PYRAMID_JUNGLE_CHEST, new WeightedRandomChestContent(rope.blockID, 0, 3, 6, 10));

			BlockDispenser.dispenseBehaviorRegistry.putObject(Item.itemsList[rope.blockID], new BehaviorDefaultDispenseItem()
			{
				@Override
				public ItemStack dispenseStack(IBlockSource blockSource, ItemStack itemstack)
				{
					EnumFacing facing = BlockDispenser.getFacing(blockSource.getBlockMetadata());
					World world = blockSource.getWorld();
					int x = blockSource.getXInt() + facing.getFrontOffsetX();
					int y = blockSource.getYInt() + facing.getFrontOffsetY();
					int z = blockSource.getZInt() + facing.getFrontOffsetZ();

					if (world.isAirBlock(x, y, z) && world.setBlock(x, y, z, rope.blockID, 1, 3))
					{
						--itemstack.stackSize;

						for (int i = 1; itemstack.stackSize > 0 && i < itemstack.stackSize + 1; ++i)
						{
							int next = y - 5 * i;

							if (world.getBlockId(x, next, z) == rope.blockID && world.isAirBlock(x, --next, z) && next > 0)
							{
								if (world.setBlock(x, next, z, rope.blockID, 1, 3))
								{
									--itemstack.stackSize;
								}
								else break;
							}
						}
					}

					return itemstack;
				}

				@Override
				public void playDispenseSound(IBlockSource blockSource)
				{
					super.playDispenseSound(blockSource);

					blockSource.getWorld().playSoundEffect(blockSource.getXInt(), blockSource.getYInt(), blockSource.getZInt(), rope.stepSound.getPlaceSound(), 1.0F, 2.0F);
				}
			});
		}
	}
}