package main.flowstoneenergy.events;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.client.entity.AbstractClientPlayer;
import net.minecraftforge.client.event.RenderPlayerEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;

public class CapeEventHandler {

    private final String serverLocation = "https://github.com/FlowstoneTeam/FlowstoneEnergy/master/capes/capes.txt";
    private final int timeout = 1000;

    private static final Graphics TEST_GRAPHICS = new BufferedImage(128, 128, BufferedImage.TYPE_INT_RGB).getGraphics();
    private HashMap<String, String> cloaks = new HashMap<String, String>();
    private ArrayList<AbstractClientPlayer> capePlayers = new ArrayList<AbstractClientPlayer>();
    @SuppressWarnings("unused")
    private boolean textureUploaded;

    public static CapeEventHandler instance;

    public CapeEventHandler() {
        buildCloakURLDatabase();
        instance = this;
    }

    @SubscribeEvent
    public void onPreRenderSpecials(RenderPlayerEvent.Specials.Pre event) {
        if (event.entityPlayer instanceof AbstractClientPlayer) {
            AbstractClientPlayer abstractClientPlayer = (AbstractClientPlayer) event.entityPlayer;

            if (!capePlayers.contains(abstractClientPlayer)) {
                String cloakURL = cloaks.get(event.entityPlayer.getDisplayName().toLowerCase());

                if (cloakURL == null) {
                    return;
                }

                capePlayers.add(abstractClientPlayer);

                textureUploaded = false;
                new Thread(new CloakThread(abstractClientPlayer, cloakURL)).start();
                event.renderCape = true;
            }
        }
    }

    public void buildCloakURLDatabase() {
        URL url;
        try {
            url = new URL(serverLocation);
            URLConnection con = url.openConnection();
            con.setConnectTimeout(timeout);
            con.setReadTimeout(timeout);
            InputStream io = con.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(io));

            String str;
            @SuppressWarnings("unused")
            int linetracker = 1;
            while ((str = br.readLine()) != null) {
                if (!str.startsWith("--")) {
                    if (str.contains(":")) {
                        String nick = str.substring(0, str.indexOf(":"));
                        String link = str.substring(str.indexOf(":") + 1);
                        new Thread(new CloakPreload(link)).start();
                        cloaks.put(nick, link);
                    } else {
                        //TConstruct.logger.error("[SkinLoader] Syntax error on line " + linetracker + ": " + str);
                    }
                }
                linetracker++;
            }

            br.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class CloakThread implements Runnable {

        AbstractClientPlayer abstractClientPlayer;
        String cloakURL;

        public CloakThread(AbstractClientPlayer player, String cloak) {
            abstractClientPlayer = player;
            cloakURL = cloak;
        }

        @Override
        public void run() {
            try {
                Image cape = new ImageIcon(new URL(cloakURL)).getImage();
                BufferedImage bo = new BufferedImage(cape.getWidth(null), cape.getHeight(null), BufferedImage.TYPE_INT_ARGB);
                bo.getGraphics().drawImage(cape, 0, 0, null);
                abstractClientPlayer.getTextureCape().setBufferedImage(bo);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    private class CloakPreload implements Runnable {
        String cloakURL;

        public CloakPreload(String link) {
            cloakURL = link;
        }

        @Override
        public void run() {
            try {
                TEST_GRAPHICS.drawImage(new ImageIcon(new URL(cloakURL)).getImage(), 0, 0, null);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    public void refreshCapes() {
        cloaks.clear();
        capePlayers.clear();
        buildCloakURLDatabase();
    }
}
@@ -636,14 +636,29 @@
 s://
+raw.
 github
+usercontent
 .com
@@ -2938,28 +2938,25 @@
    %7D else %7B%0A
-    
+%09
             
@@ -2967,97 +2967,63 @@
     
-//TConstruct.logger.error(%22%5BSkinLoader%5D Syntax error on line %22 + linetracker + %22: %22 + str
+System.out.println(%22%5BFlowstoneEnergy%5D :( - sadfaic.gif%22
 );%0A 

Fix capes... Third time's the charm