package net.blay09.mods.craftingtweaks.client;

import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.InputEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import net.blay09.mods.craftingtweaks.CommonProxy;
import net.blay09.mods.craftingtweaks.CraftingTweaks;
import net.blay09.mods.craftingtweaks.net.MessageBalance;
import net.blay09.mods.craftingtweaks.net.MessageClear;
import net.blay09.mods.craftingtweaks.net.MessageRotate;
import net.blay09.mods.craftingtweaks.net.NetworkHandler;
import net.blay09.mods.craftingtweaks.api.TweakProvider;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import org.lwjgl.input.Keyboard;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ClientProxy extends CommonProxy {

    private boolean wasRotated;
    private boolean wasCleared;
    private boolean wasBalanced;

    private Field neiSearchField;
    private Method focused;

    @Override
    public void init(FMLInitializationEvent event) {
        FMLCommonHandler.instance().bus().register(this);
        MinecraftForge.EVENT_BUS.register(this);
    }

    @Override
    public void postInit(FMLPostInitializationEvent event) {
        try {
            Class neiLayoutManagerClass = Class.forName("codechicken.nei.LayoutManager");
            neiSearchField = neiLayoutManagerClass.getField("searchField");
            Class textFieldClass = Class.forName("codechicken.nei.TextField");
            focused = textFieldClass.getMethod("focused");
        } catch (ClassNotFoundException ignored) {
        } catch (NoSuchMethodException ignored) {
        } catch (NoSuchFieldException ignored) {
        }
    }

    @SubscribeEvent
    public void onClientTick(TickEvent.ClientTickEvent event) {
        EntityPlayer entityPlayer = FMLClientHandler.instance().getClientPlayerEntity();
        if(entityPlayer != null) {
            Container container = entityPlayer.openContainer;
            if (container != null) {
                if(!areHotkeysEnabled()) {
                    return;
                }
                TweakProvider provider = CraftingTweaks.instance.getProvider(container);
                if(provider != null) {
                    if (Keyboard.isKeyDown(Keyboard.KEY_R)) {
                        if(!wasRotated && provider.areHotkeysEnabled(entityPlayer, container)) {
                            NetworkHandler.instance.sendToServer(new MessageRotate(0));
                            wasRotated = true;
                        }
                    } else {
                        wasRotated = false;
                    }
                    if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
                        if(!wasCleared && provider.areHotkeysEnabled(entityPlayer, container)) {
                            NetworkHandler.instance.sendToServer(new MessageClear(0));
                            wasCleared = true;
                        }
                    } else {
                        wasCleared = false;
                    }
                    if (Keyboard.isKeyDown(Keyboard.KEY_B) ) {
                        if(!wasBalanced && provider.areHotkeysEnabled(entityPlayer, container)) {
                            NetworkHandler.instance.sendToServer(new MessageBalance(0));
                            wasBalanced = true;
                        }
                    } else {
                        wasBalanced = false;
                    }
                }
            }
        }
    }

    public boolean areHotkeysEnabled() {
        if(neiSearchField != null) {
            try {
                Object searchField = neiSearchField.get(null);
                return !(Boolean) focused.invoke(searchField);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    @SubscribeEvent
    public void onInitGui(GuiScreenEvent.InitGuiEvent.Post event) {
        if(event.gui instanceof GuiContainer) {
            GuiContainer guiContainer = (GuiContainer) event.gui;
            TweakProvider provider = CraftingTweaks.instance.getProvider(guiContainer.inventorySlots);
            if(provider != null) {
                provider.initGui(guiContainer, event.buttonList);
            }
        }
    }

    @SubscribeEvent
    public void onActionPerformed(GuiScreenEvent.ActionPerformedEvent event) {
        if(event.button instanceof GuiTweakButton) {
            switch(((GuiTweakButton) event.button).getTweakOption()) {
                case Rotate:
                    NetworkHandler.instance.sendToServer(new MessageRotate(((GuiTweakButton) event.button).getTweakId()));
                    event.setCanceled(true);
                    break;
                case Balance:
                    NetworkHandler.instance.sendToServer(new MessageBalance(((GuiTweakButton) event.button).getTweakId()));
                    event.setCanceled(true);
                    break;
                case Clear:
                    NetworkHandler.instance.sendToServer(new MessageClear(((GuiTweakButton) event.button).getTweakId()));
                    event.setCanceled(true);
                    break;
            }
        }
    }

}

@@ -123,32 +123,67 @@
 LCommonHandler;%0A
+import cpw.mods.fml.common.Loader;%0A
 import cpw.mods.
@@ -215,32 +215,32 @@
 alizationEvent;%0A
-
 import cpw.mods.
@@ -1731,32 +1731,87 @@
 nEvent event) %7B%0A
+        if(Loader.isModLoaded(%22NotEnoughItems%22)) %7B%0A    
         try %7B%0A  
@@ -1816,24 +1816,28 @@
             
+    
 Class neiLay
@@ -1914,16 +1914,20 @@
         
+   
+ 
 neiSearc
@@ -1974,32 +1974,36 @@
 %22searchField%22);%0A
+    
             Clas
@@ -2077,16 +2077,20 @@
         
+  
+  
 focused 
@@ -2128,32 +2128,36 @@
 used%22);%0A        
+    
 %7D catch (ClassNo
@@ -2176,117 +2176,232 @@
 ion 
-ignored) %7B%0A        %7D catch (NoSuchMethodException ignored) %7B%0A        %7D catch (NoSuchFieldException ignored) %7B
+e) %7B%0A                e.printStackTrace();%0A            %7D catch (NoSuchMethodException e) %7B%0A                e.printStackTrace();%0A            %7D catch (NoSuchFieldException e) %7B%0A                e.printStackTrace();%0A            %7D
 %0A   
@@ -4305,16 +4305,35 @@
 Field !=
+ null && focused !=
  null) %7B
@@ -4414,16 +4414,62 @@
 (null);%0A
+                if(searchField != null) %7B%0A    
         
@@ -4523,16 +4523,34 @@
 Field);%0A
+                %7D%0A
         

Fix crash when there's no NEI search field while checking for hotkeys.