/*
 * Copyright (C) 2014  Kihira
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package kihira.tails.client;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import kihira.tails.client.gui.GuiEditTail;
import kihira.tails.client.render.RenderDragonTail;
import kihira.tails.client.render.RenderFoxTail;
import kihira.tails.client.render.RenderRaccoonTail;
import kihira.tails.client.render.RenderTail;
import kihira.tails.client.texture.TextureHelper;
import kihira.tails.common.TailInfo;
import kihira.tails.common.Tails;
import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.RenderPlayerEvent;

import java.util.UUID;

@SideOnly(Side.CLIENT)
public class ClientEventHandler {

	public static final RenderTail[] tailTypes = { new RenderFoxTail(), new RenderDragonTail(), new RenderRaccoonTail() };

    @SubscribeEvent
    public void onPlayerRenderTick(RenderPlayerEvent.Specials.Pre e) {
        //TESTING REMOVE
/*        if (e.entityPlayer.isSneaking()) {
            EntityPlayer player = e.entityPlayer;
            UUID uuid = player.getGameProfile().getId();
            int typeid = 0;
            int subtype = 2;

            TailInfo tailInfo = new TailInfo(uuid, true, typeid, subtype, -1803209, -1938144, -592395, null);
            tailInfo.setTexture(TextureHelper.generateTexture(tailInfo));
            tailInfo.needsTextureCompile = false;

            Tails.proxy.removeTailInfo(uuid);
            Tails.proxy.addTailInfo(uuid, tailInfo);
        }*/
        if (e.entityPlayer.isSneaking() && !(Minecraft.getMinecraft().currentScreen instanceof GuiEditTail)) {
            Minecraft.getMinecraft().displayGuiScreen(new GuiEditTail());
        }

    	UUID uuid = e.entityPlayer.getGameProfile().getId();
        if (Tails.proxy.hasTailInfo(uuid) && Tails.proxy.getTailInfo(uuid).hastail && !e.entityPlayer.isInvisible()) {
        	TailInfo info = Tails.proxy.getTailInfo(uuid);
        	
        	int type = info.typeid;
        	type = type > tailTypes.length ? 0 : type;
        	
            tailTypes[type].render(e.entityPlayer, info);
        }
    }

    @SubscribeEvent
    public void onConnectToServer(FMLNetworkEvent.ClientConnectedToServerEvent event) {
        //Add local player texture to map
        if (TextureHelper.localPlayerTailInfo != null) {
            TailInfo tailInfo = TextureHelper.localPlayerTailInfo;
            Tails.proxy.addTailInfo(tailInfo.id, tailInfo);
        }
    }

    @SubscribeEvent
    public void onPlayerLeave(FMLNetworkEvent.ClientDisconnectionFromServerEvent e) {
        Tails.hasRemote = false;
        Tails.proxy.clearAllTailInfo(); //TODO This won't delete the textures cause it's called from the wrong thread
    }

    @SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent e) {
        if (TextureHelper.needsBuild(e.player)) {
            TextureHelper.buildPlayerInfo(e.player);
        }
    }
}

@@ -1192,24 +1192,139 @@
 .client.
-Minecraf
+gui.GuiButton;%0Aimport net.minecraft.client.gui.inventory.GuiInventory;%0Aimport net.minecraftforge.client.event.GuiScreenEven
 t;%0Aimpor
@@ -2255,203 +2255,8 @@
  %7D*/
-%0A        if (e.entityPlayer.isSneaking() && !(Minecraft.getMinecraft().currentScreen instanceof GuiEditTail)) %7B%0A            Minecraft.getMinecraft().displayGuiScreen(new GuiEditTail());%0A        %7D
 %0A%0A  
@@ -2583,32 +2583,32 @@
 type;%0A        %09%0A
-
             tail
@@ -2658,32 +2658,673 @@
        %7D%0A    %7D%0A%0A
+    @SubscribeEvent%0A    @SuppressWarnings(%22unchecked%22)%0A    public void onScreenInitPost(GuiScreenEvent.InitGuiEvent.Post event) %7B%0A        if (event.gui instanceof GuiInventory) %7B%0A            event.buttonList.add(new GuiButton(1234, event.gui.width - 25, event.gui.height - 25, 20, 20, %22T%22));%0A        %7D%0A    %7D%0A%0A    @SubscribeEvent%0A    public void onButtonClickPre(GuiScreenEvent.ActionPerformedEvent.Pre event) %7B%0A        if (event.gui instanceof GuiInventory) %7B%0A            if (event.button.id == 1234) %7B%0A                event.gui.mc.displayGuiScreen(new GuiEditTail());%0A                event.setCanceled(true);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     @SubscribeEv

Add in button to open tail editing window