package me.TehGoldyLockz.OlympicHeroes.listeners;

import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.enchantment.EnchantItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import me.TehGoldyLockz.OlympicHeroes.OlympicHeroes;
import me.TehGoldyLockz.OlympicHeroes.player.OHPlayer;

public class CraftListener implements Listener{

	public CraftListener(OlympicHeroes plugin) {
		plugin.getServer().getPluginManager().registerEvents(this, plugin);
	}
	
	@EventHandler
	public void onEnchant(EnchantItemEvent e) {
		Player player = e.getEnchanter();
		
		if(player.hasPermission("olympicheroes.enchant") == false) {
			e.setCancelled(true);
		}
	}
	
	@EventHandler(priority = EventPriority.HIGHEST)
	public void onAnvilUse(InventoryClickEvent e) {
		if(!e.isCancelled()) {
			HumanEntity ent = e.getWhoClicked();
			
			if(ent instanceof Player) {
				Player player = (Player) ent;
				Inventory inv = e.getInventory();
				
				if(inv instanceof AnvilInventory) {
					AnvilInventory anvil = (AnvilInventory) inv;
					ItemStack[] items = anvil.getContents();

					// item in the left slot
					ItemStack item1 = items[0];
					 
					// item in the right slot
					ItemStack item2 = items[1];
					
					if(item1 != null && item2 != null) {
						ItemStack result = e.getCurrentItem();
						
						if(result != null && (result.getType() == item1.getType() || result.getType() == item2.getType())){
							OHPlayer ohPlayer = new OHPlayer(player);
							int hephLevel = ohPlayer.getLevel("Hephaestus");

							if(result.getEnchantments().size() > 0) {
								if(hephLevel < 5) {
									e.setCancelled(true);
									player.sendMessage("Your devotion to Hephaestus is not high enough to repair enchanted items.");
								}
							}else {
								if(hephLevel < 3) {
									e.setCancelled(true);
									player.sendMessage("Your devotion to Hephaestus is not high enough to repair items.");
								}
							}
						}
					}
				}
			}
		}
	}
}

@@ -1543,22 +1543,23 @@
 emStack 
-result
+clicked
  = e.get
@@ -1589,22 +1589,85 @@
 %09%09%09%09%09if(
-result
+!clicked.equals(item1) && !clicked.equals(item2)) %7B%0A%09%09%09%09%09%09%09if(clicked
  != null
@@ -1671,22 +1671,23 @@
 ull && (
-result
+clicked
 .getType
@@ -1711,22 +1711,23 @@
 pe() %7C%7C 
-result
+clicked
 .getType
@@ -1751,16 +1751,17 @@
 pe()))%7B%0A
+%09
 %09%09%09%09%09%09%09O
@@ -1808,16 +1808,17 @@
 %0A%09%09%09%09%09%09%09
+%09
 int heph
@@ -1870,17 +1870,19 @@
 %09%09%09%09
+%09
 if(
-result
+clicked
 .get
@@ -1906,24 +1906,25 @@
 ze() %3E 0) %7B%0A
+%09
 %09%09%09%09%09%09%09%09if(h
@@ -1940,32 +1940,33 @@
 %3C 5) %7B%0A%09%09%09%09%09%09%09%09%09
+%09
 e.setCancelled(t
@@ -1963,32 +1963,33 @@
 ancelled(true);%0A
+%09
 %09%09%09%09%09%09%09%09%09player.
@@ -2078,34 +2078,36 @@
 ems.%22);%0A%09%09%09%09%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09%09%09%09%7Delse %7B%0A%09
@@ -2105,16 +2105,17 @@
 %7Delse %7B%0A
+%09
 %09%09%09%09%09%09%09%09
@@ -2135,32 +2135,33 @@
 %3C 3) %7B%0A%09%09%09%09%09%09%09%09%09
+%09
 e.setCancelled(t
@@ -2158,32 +2158,33 @@
 ancelled(true);%0A
+%09
 %09%09%09%09%09%09%09%09%09player.
@@ -2255,32 +2255,43 @@
 epair items.%22);%0A
+%09%09%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09
@@ -2297,24 +2297,31 @@
 %09%09%7D%0A%09%09%09%09%09%09%7D%0A
+%09%09%09%09%09%09%0A
 %09%09%09%09%09%7D%0A%09%09%09%09%7D

fix hephaestus