package com.lothrazar.powerinventory;

import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.client.GuiIngameForge;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import com.lothrazar.powerinventory.config.ModConfig;
import com.lothrazar.powerinventory.inventory.button.GuiButtonOpenInventory;
import com.lothrazar.powerinventory.net.EnderChestPacket;
import com.lothrazar.powerinventory.net.EnderPearlPacket;
import com.lothrazar.powerinventory.net.HotbarSwapPacket;
import com.lothrazar.powerinventory.net.OpenInventoryPacket;
import com.lothrazar.powerinventory.proxy.ClientProxy;
import net.minecraftforge.fml.client.event.ConfigChangedEvent.OnConfigChangedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class EventHandler {
	@SubscribeEvent
	public void onKeyInput(InputEvent.KeyInputEvent event) {
		if (ClientProxy.keyEnderpearl.isPressed()) {
			ModInv.instance.network.sendToServer(new EnderPearlPacket());
		}
		if (ClientProxy.keyEnderchest.isPressed()) {
			ModInv.instance.network.sendToServer(new EnderChestPacket());
		}
		if (ClientProxy.keyHotbar.isPressed()) {
			ModInv.instance.network.sendToServer(new HotbarSwapPacket());
		}
		if (ClientProxy.keyInventory.isPressed()) {
			ModInv.instance.network.sendToServer(new OpenInventoryPacket());
		}
	}

	@SubscribeEvent
	public void onConfigChanged(OnConfigChangedEvent event) {
		if (event.modID.equals(Const.MODID))
			ModConfig.syncConfig();
	}

	@SubscribeEvent
	public void onEntityConstruct(EntityConstructing event) // More reliable
															// than on entity
															// join
	{
		if (event.entity instanceof EntityPlayer) {
			EntityPlayer player = (EntityPlayer) event.entity;

			if (PlayerPersistProperty.get(player) == null) {
				PlayerPersistProperty.register(player);
			}
		}
	}

	@SubscribeEvent
	public void onPlayerClone(PlayerEvent.Clone event) {
		if (event.wasDeath == false || // changing dimensions -> so always do it
										// or it was a death => maybe do it
		(ModConfig.persistUnlocksOnDeath && event.wasDeath)) {
			PlayerPersistProperty.clonePlayerData(event.original, event.entityPlayer);
		}
	}

	@SubscribeEvent
	public void onEntityDeath(LivingDeathEvent event) {
		if (event.entityLiving instanceof EntityPlayer && !event.entityLiving.worldObj.isRemote) {
			EntityPlayer p = (EntityPlayer) event.entityLiving;

			PlayerPersistProperty prop = PlayerPersistProperty.get(p);
			// the vanilla inventory stuff (first hotbar) already drops by
			// default

			for (int i = Const.HOTBAR_SIZE; i < prop.inventory.getSizeInventory(); ++i) {
				prop.inventory.dropStackInSlot(p, i);
			}

			prop.inventory.dropStackInSlot(p, Const.SLOT_ECHEST);
			prop.inventory.dropStackInSlot(p, Const.SLOT_EPEARL);
		}
	}

	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public void onGuiPostInit(InitGuiEvent.Post event) {
		if (event.gui == null) {
			return;
		}// probably doesnt ever happen

		if (ModConfig.showGuiButton && event.gui instanceof net.minecraft.client.gui.inventory.GuiInventory) {
			// omg thanks so much to this guy
			// http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/mods-discussion/1390983-making-guis-scale-to-screen-width-height
			ScaledResolution res = new ScaledResolution(event.gui.mc);

			int screenWidth = res.getScaledWidth();
			int screenHeight = res.getScaledHeight();

			int button_id = 256;
			int  x, y;

			x = screenWidth / 2 + Const.VWIDTH / 2 - GuiButtonOpenInventory.width - 1;// align tight to top
			y = screenHeight / 2 - Const.VHEIGHT / 2 - GuiButtonOpenInventory.height + 1;

			event.buttonList.add(new GuiButtonOpenInventory(button_id++, x, y));
		}
	}

	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public void onRenderOverlay(RenderGameOverlayEvent event) {
		// force it to always show food - otherwise its hidden when riding a
		// horse

		if (ModConfig.alwaysShowHungerbar) {
			GuiIngameForge.renderFood = true;
		}
	}
}

@@ -32,16 +32,55 @@
 ntory;%0A%0A
+import net.minecraft.client.Minecraft;%0A
 import n
@@ -2066,82 +2066,8 @@
 nt) 
-// More reliable%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// than on entity%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09// join%0A%09
 %7B%0A%09%09
@@ -3862,16 +3862,38 @@
 int 
- 
 x, y;%0A%0A
+%09%09%09//align t top right%0A
 %09%09%09x
@@ -3969,29 +3969,8 @@
 - 1;
-// align tight to top
 %0A%09%09%09
@@ -4048,16 +4048,177 @@
 t + 1;%0A%0A
+%09%09%09boolean hasPotions = Minecraft.getMinecraft().thePlayer.getActivePotionEffects().size() %3E 0;%0A%09%09%09if(hasPotions)%7B%0A%09%09%09%09x += 60;%0A%09%09%09%7D%0A%09%09%09//this is the tab button%0A
 %09%09%09event

align tab button even when potions applied