package net.crazysnailboy.mods.villagertrades.trades;

import java.util.HashMap;

import net.crazysnailboy.mods.villagertrades.trades.CustomTrades.VTTVillagerBuyingTrade;
import net.crazysnailboy.mods.villagertrades.trades.CustomTrades.VTTVillagerSellingTrade;
import net.minecraft.entity.passive.EntityVillager.EmeraldForItems;
import net.minecraft.entity.passive.EntityVillager.ITradeList;
import net.minecraft.entity.passive.EntityVillager.ItemAndEmeraldToItem;
import net.minecraft.entity.passive.EntityVillager.ListEnchantedBookForEmeralds;
import net.minecraft.entity.passive.EntityVillager.ListEnchantedItemForEmeralds;
import net.minecraft.entity.passive.EntityVillager.ListItemForEmeralds;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;


/**
 * The trade handlers provide an abstraction from the differences in the various EntityVillager.ITradeList implementations
 * and give a uniform way of getting the buying stack and/or the selling stack for each implementation.
 *
 * They also provide a convenient way of classifying the trades into buying and selling trades, which is used for sorting.
 *
 */
public class TradeHandlers
{

	public static abstract interface ITradeHandler
	{
	}

	public static final HashMap<Class<? extends ITradeList>, ITradeHandler> tradeHandlers;

	static
	{
		tradeHandlers = new HashMap<Class<? extends ITradeList>, ITradeHandler>();

		// villager buying handlers
		// built-in
		tradeHandlers.put(EmeraldForItems.class, new EmeraldForItemsHandler());
		// custom
		tradeHandlers.put(VTTVillagerBuyingTrade.class, new VTTVillagerBuyingHandler());

		// villager selling handlers
		// built-in
		tradeHandlers.put(ItemAndEmeraldToItem.class, new ItemAndEmeraldToItemHandler());
		tradeHandlers.put(ListEnchantedBookForEmeralds.class, new ListEnchantedBookForEmeraldsHandler());
		tradeHandlers.put(ListEnchantedItemForEmeralds.class, new ListEnchantedItemForEmeraldsHandler());
		tradeHandlers.put(ListItemForEmeralds.class, new ListItemForEmeraldsHandler());
		// custom
		tradeHandlers.put(VTTVillagerSellingTrade.class, new VTTVillagerSellingHandler());
	}



	// ==================================================
	// HANDLERS FOR VILLAGER BUYING TRADES
	// ==================================================

	public static class VTTVillagerBuyingHandler implements ITradeHandler
	{
		public ItemStack getBuyingStack(ITradeList trade)
		{
			return ItemStack.EMPTY;
		}
	}

	public static class EmeraldForItemsHandler extends VTTVillagerBuyingHandler
	{
		@Override
		public ItemStack getBuyingStack(ITradeList t)
		{
			EmeraldForItems trade = (EmeraldForItems)t;
			ItemStack stack = new ItemStack(trade.buyingItem, 1, 0);
			return stack;
		}
	}


	// ==================================================
	// HANDLERS FOR VILLAGER SELLING TRADES
	// ==================================================

	public static class VTTVillagerSellingHandler implements ITradeHandler
	{
		public ItemStack getSellingStack(ITradeList trade)
		{
			return ItemStack.EMPTY;
		}
	}

	public static class ItemAndEmeraldToItemHandler extends VTTVillagerSellingHandler
	{
		@Override
		public ItemStack getSellingStack(ITradeList t)
		{
			ItemAndEmeraldToItem trade = (ItemAndEmeraldToItem)t;
			ItemStack stack = trade.sellingItemstack.copy();
			return stack;
		}
	}

	public static class ListEnchantedBookForEmeraldsHandler extends VTTVillagerSellingHandler
	{
		@Override
		public ItemStack getSellingStack(ITradeList t)
		{
			ListEnchantedBookForEmeralds trade = (ListEnchantedBookForEmeralds)t;
			ItemStack stack = new ItemStack(Items.ENCHANTED_BOOK, 1, 0);
			return stack;
		}
	}

	public static class ListEnchantedItemForEmeraldsHandler extends VTTVillagerSellingHandler
	{
		@Override
		public ItemStack getSellingStack(ITradeList t)
		{
			ListEnchantedItemForEmeralds trade = (ListEnchantedItemForEmeralds)t;
			ItemStack stack = trade.enchantedItemStack.copy();
			return stack;
		}
	}

	public static class ListItemForEmeraldsHandler extends VTTVillagerSellingHandler
	{
		@Override
		public ItemStack getSellingStack(ITradeList t)
		{
			ListItemForEmeralds trade = (ListItemForEmeralds)t;
			ItemStack stack = trade.itemToBuy.copy();
			return stack;
		}
	}

}

@@ -2008,32 +2008,300 @@
 aldsHandler());%0A
+%0A%09%09try%0A%09%09%7B%0A%09%09%09Class TreasureMapForEmeralds$class = Class.forName(%22net.minecraft.entity.passive.EntityVillager$TreasureMapForEmeralds%22);%0A%09%09%09tradeHandlers.put(TreasureMapForEmeralds$class, new TreasureMapForEmeraldsHandler());%0A%09%09%7D%0A%09%09catch (ClassNotFoundException e)%7B %7D%0A%0A
 %09%09// custom%0A%09%09tr
@@ -4491,10 +4491,308 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09public static class TreasureMapForEmeraldsHandler extends VTTVillagerSellingHandler%0A%09%7B%0A%09%09@Override%0A%09%09public ItemStack getSellingStack(ITradeList t)%0A%09%09%7B%0A//%09%09%09TreasureMapForEmeralds trade = (TreasureMapForEmeralds)t;%0A%09%09%09ItemStack stack = new ItemStack(Items.FILLED_MAP, 1);%0A%09%09%09return stack;%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

fix for removing treasure map trades from cartographers