package common.init;

import common.color.DyeColor;
import common.enchantment.Enchantment;
import common.enchantment.EnchantmentHelper;
import common.enchantment.RngEnchantment;
import common.item.Item;
import common.item.ItemStack;
import common.rng.Random;
import common.village.MerchantRecipe;
import common.village.MerchantRecipeList;

public abstract class TradeRegistry {
	public interface ITradeList {
		void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random);
	}
	
	public static final ITradeList[] TRADES = new ITradeList[] {
			new GemForItem(Items.wheats, new PriceInfo(18, 22)),
			new GemForItem(Items.potato, new PriceInfo(15, 19)),
			new GemForItem(Items.carrot, new PriceInfo(15, 19)),
			new ItemForGem(Items.bread, new PriceInfo(-4, -2)),
			new GemForItem(ItemRegistry.getItemFromBlock(Blocks.pumpkin), new PriceInfo(8, 13)),
			new ItemForGem(Items.pumpkin_pie, new PriceInfo(-3, -2)),
			new GemForItem(ItemRegistry.getItemFromBlock(Blocks.melon_block), new PriceInfo(7, 12)),
			new ItemForGem(Items.apple, new PriceInfo(-5, -7)),
			new ItemForGem(Items.cookie, new PriceInfo(-6, -10)),
			new ItemForGem(Items.cake, new PriceInfo(1, 1)),
			new GemForItem(Items.string, new PriceInfo(15, 20)),
			new GemForItem(Items.coal, new PriceInfo(16, 24)),
			new ItemForGemItem(Items.fish, new PriceInfo(6, 6), Items.cooked_fish,
					new PriceInfo(6, 6)),
			new EnchForGem(Items.fishing_rod, new PriceInfo(7, 8)),
			new GemForItem(ItemRegistry.getItemFromBlock(Blocks.wool), new PriceInfo(16, 22)),
			new ItemForGem(Items.iron_shears, new PriceInfo(3, 4)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 0),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 1),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 2),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 3),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 4),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 5),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 6),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 7),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 8),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 9),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 10),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 11),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 12),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 13),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 14),
					new PriceInfo(1, 2)),
			new ItemForGem(new ItemStack(ItemRegistry.getItemFromBlock(Blocks.wool), 1, 15),
					new PriceInfo(1, 2)),
			new GemForItem(Items.string, new PriceInfo(15, 20)),
			new ItemForGem(Items.arrow, new PriceInfo(-12, -8)),
			new ItemForGem(Items.bow, new PriceInfo(2, 3)),
			new ItemForGemItem(ItemRegistry.getItemFromBlock(Blocks.gravel),
					new PriceInfo(10, 10), Items.flint, new PriceInfo(6, 10)),
			new GemForItem(Items.paper, new PriceInfo(24, 36)),
			new BookForGem(),
			new GemForItem(Items.book, new PriceInfo(8, 10)),
			new ItemForGem(Items.navigator, new PriceInfo(10, 12)),
			new ItemForGem(ItemRegistry.getItemFromBlock(Blocks.bookshelf),
					new PriceInfo(3, 4)),
			new GemForItem(Items.written_book, new PriceInfo(2, 2)),
//			new ItemForGem(Items.clock, new PriceInfo(10, 12)),
			new ItemForGem(ItemRegistry.getItemFromBlock(Blocks.glass),
					new PriceInfo(-5, -3)),
			new BookForGem(),
			new BookForGem(),
			new ItemForGem(Items.name_tag, new PriceInfo(20, 22)),
			new GemForItem(Items.rotten_flesh, new PriceInfo(36, 40)),
			new GemForItem(Items.gold_ingot, new PriceInfo(8, 10)),
			new ItemForGem(Items.redstone, new PriceInfo(-4, -1)),
			new ItemForGem(new ItemStack(Items.dye, 1, DyeColor.BLUE.getDyeDamage()),
					new PriceInfo(-2, -1)),
			new ItemForGem(Items.charged_orb, new PriceInfo(7, 11)),
			new ItemForGem(ItemRegistry.getItemFromBlock(Blocks.glowstone),
					new PriceInfo(-3, -1)),
			new ItemForGem(Items.experience_bottle, new PriceInfo(3, 11)),
			new GemForItem(Items.coal, new PriceInfo(16, 24)),
			new ItemForGem(Items.iron_helmet, new PriceInfo(4, 6)),
			new GemForItem(Items.iron_ingot, new PriceInfo(7, 9)),
			new ItemForGem(Items.iron_chestplate, new PriceInfo(10, 14)),
			new GemForItem(Items.diamond, new PriceInfo(3, 4)),
			new EnchForGem(Items.diamond_chestplate, new PriceInfo(16, 19)),
			new ItemForGem(Items.chain_boots, new PriceInfo(5, 7)),
			new ItemForGem(Items.chain_leggings, new PriceInfo(9, 11)),
			new ItemForGem(Items.chain_helmet, new PriceInfo(5, 7)),
			new ItemForGem(Items.chain_chestplate, new PriceInfo(11, 15)),
			new GemForItem(Items.coal, new PriceInfo(16, 24)),
			new ItemForGem(Items.iron_axe, new PriceInfo(6, 8)),
			new GemForItem(Items.iron_ingot, new PriceInfo(7, 9)),
			new EnchForGem(Items.iron_sword, new PriceInfo(9, 10)),
			new GemForItem(Items.diamond, new PriceInfo(3, 4)),
			new EnchForGem(Items.diamond_sword, new PriceInfo(12, 15)),
			new EnchForGem(Items.diamond_axe, new PriceInfo(9, 12)),
			new GemForItem(Items.coal, new PriceInfo(16, 24)),
			new EnchForGem(Items.iron_shovel, new PriceInfo(5, 7)),
			new GemForItem(Items.iron_ingot, new PriceInfo(7, 9)),
			new EnchForGem(Items.iron_pickaxe, new PriceInfo(9, 11)),
			new GemForItem(Items.diamond, new PriceInfo(3, 4)),
			new EnchForGem(Items.diamond_pickaxe, new PriceInfo(12, 15)),
			new GemForItem(Items.porkchop, new PriceInfo(14, 18)),
			new GemForItem(Items.chicken, new PriceInfo(14, 18)),
			new GemForItem(Items.coal, new PriceInfo(16, 24)),
			new ItemForGem(Items.cooked_porkchop, new PriceInfo(-7, -5)),
			new ItemForGem(Items.cooked_chicken, new PriceInfo(-8, -6)),
			new GemForItem(Items.leather, new PriceInfo(9, 12)),
			new ItemForGem(Items.leather_leggings, new PriceInfo(2, 4)),
			new EnchForGem(Items.leather_chestplate, new PriceInfo(7, 12)),
			new ItemForGem(Items.saddle, new PriceInfo(8, 10))
	};

	static class GemForItem implements ITradeList {
		public Item sellItem;
		public PriceInfo price;

		public GemForItem(Item itemIn, PriceInfo priceIn) {
			this.sellItem = itemIn;
			this.price = priceIn;
		}

		public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random) {
			int i = 1;

			if(this.price != null) {
				i = this.price.getPrice(random);
			}

			recipeList.add(new MerchantRecipe(new ItemStack(this.sellItem, i, 0), Items.emerald));
		}
	}

	static class ItemForGemItem implements ITradeList {
		public ItemStack buyingItemStack;
		public PriceInfo buyingPriceInfo;
		public ItemStack sellingItemstack;
		public PriceInfo field_179408_d;

		public ItemForGemItem(Item p_i45813_1_, PriceInfo p_i45813_2_, Item p_i45813_3_, PriceInfo p_i45813_4_) {
			this.buyingItemStack = new ItemStack(p_i45813_1_);
			this.buyingPriceInfo = p_i45813_2_;
			this.sellingItemstack = new ItemStack(p_i45813_3_);
			this.field_179408_d = p_i45813_4_;
		}

		public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random) {
			int i = 1;

			if(this.buyingPriceInfo != null) {
				i = this.buyingPriceInfo.getPrice(random);
			}

			int j = 1;

			if(this.field_179408_d != null) {
				j = this.field_179408_d.getPrice(random);
			}

			recipeList.add(new MerchantRecipe(new ItemStack(this.buyingItemStack.getItem(), i, this.buyingItemStack.getMetadata()),
					new ItemStack(Items.emerald), new ItemStack(this.sellingItemstack.getItem(), j, this.sellingItemstack.getMetadata())));
		}
	}

	static class BookForGem implements ITradeList {
		public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random) {
			Enchantment enchantment = random.pick(Enchantment.enchantmentsBookList);
			int i = random.range(enchantment.getMinLevel(), enchantment.getMaxLevel());
			ItemStack itemstack = Items.enchanted_book.getEnchantedItemStack(new RngEnchantment(enchantment, i));
			int j = 2 + random.zrange(5 + i * 10) + 3 * i;

			if(j > 64) {
				j = 64;
			}

			recipeList.add(new MerchantRecipe(new ItemStack(Items.book), new ItemStack(Items.emerald, j), itemstack));
		}
	}

	static class EnchForGem implements ITradeList {
		public ItemStack enchantedItemStack;
		public PriceInfo priceInfo;

		public EnchForGem(Item p_i45814_1_, PriceInfo p_i45814_2_) {
			this.enchantedItemStack = new ItemStack(p_i45814_1_);
			this.priceInfo = p_i45814_2_;
		}

		public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random) {
			int i = 1;

			if(this.priceInfo != null) {
				i = this.priceInfo.getPrice(random);
			}

			ItemStack itemstack = new ItemStack(Items.emerald, i, 0);
			ItemStack itemstack1 = new ItemStack(this.enchantedItemStack.getItem(), 1, this.enchantedItemStack.getMetadata());
			itemstack1 = EnchantmentHelper.addRandomEnchantment(random, itemstack1, random.range(5, 19));
			recipeList.add(new MerchantRecipe(itemstack, itemstack1));
		}
	}

	static class ItemForGem implements ITradeList {
		public ItemStack itemToBuy;
		public PriceInfo priceInfo;

		public ItemForGem(Item par1Item, PriceInfo priceInfo) {
			this.itemToBuy = new ItemStack(par1Item);
			this.priceInfo = priceInfo;
		}

		public ItemForGem(ItemStack stack, PriceInfo priceInfo) {
			this.itemToBuy = stack;
			this.priceInfo = priceInfo;
		}

		public void modifyMerchantRecipeList(MerchantRecipeList recipeList, Random random) {
			int i = 1;

			if(this.priceInfo != null) {
				i = this.priceInfo.getPrice(random);
			}

			ItemStack itemstack;
			ItemStack itemstack1;

			if(i < 0) {
				itemstack = new ItemStack(Items.emerald, 1, 0);
				itemstack1 = new ItemStack(this.itemToBuy.getItem(), -i, this.itemToBuy.getMetadata());
			}
			else {
				itemstack = new ItemStack(Items.emerald, i, 0);
				itemstack1 = new ItemStack(this.itemToBuy.getItem(), 1, this.itemToBuy.getMetadata());
			}

			recipeList.add(new MerchantRecipe(itemstack, itemstack1));
		}
	}

	private static class PriceInfo {
		private Integer a;
		private Integer b;
		
		private PriceInfo(int a, int b) {
	        this.a = a;
	        this.b = b;
		}
		
		private int getPrice(Random rand) {
			return this.a >= this.b ? this.a : this.a + rand.zrange(this.b - this.a + 1);
		}
	}
}
