package net.blay09.mods.bmc.twitchintegration.handler;

import com.google.common.collect.Maps;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.blay09.mods.bmc.api.BetterMinecraftChatAPI;
import net.blay09.mods.bmc.api.image.IChatRenderable;
import net.blay09.mods.bmc.api.image.ITooltipProvider;
import net.blay09.mods.bmc.balyware.CachedAPI;
import net.blay09.mods.bmc.twitchintegration.TwitchIntegration;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IResource;
import net.minecraft.util.ResourceLocation;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

public class TwitchBadge {

	private static final Map<String, TwitchBadge> twitchBadges = Maps.newHashMap();

	private final IChatRenderable chatRenderable;
	private final ITooltipProvider tooltipProvider;

	public TwitchBadge(IChatRenderable chatRenderable, ITooltipProvider tooltipProvider) {
		this.chatRenderable = chatRenderable;
		this.tooltipProvider = tooltipProvider;
	}

	public IChatRenderable getChatRenderable() {
		return chatRenderable;
	}

	public ITooltipProvider getTooltipProvider() {
		return tooltipProvider;
	}

	@Nullable
	public static TwitchBadge getSubscriberBadge(String channel) {
		TwitchBadge badge = twitchBadges.get(channel);
		if(badge == null) {
			JsonObject object = CachedAPI.loadCachedAPI("https://api.twitch.tv/kraken/chat/" + channel + "/badges", "badges_" + channel);
			JsonElement element = object.get("subscriber");
			if(!element.isJsonNull()) {
				try {
					IChatRenderable chatRenderable = BetterMinecraftChatAPI.loadImage(new URI(element.getAsJsonObject().get("image").getAsString()), new File(Minecraft.getMinecraft().mcDataDir, "bmc/cache/badge_" + channel));
					chatRenderable.setScale(0.45f);
					badge = new TwitchBadge(chatRenderable, ITooltipProvider.EMPTY);
				} catch (IOException | URISyntaxException e) {
					e.printStackTrace();
				}
			}
			twitchBadges.put(channel, badge);
		}
		return badge;
	}

	public static void loadInbuiltBadge(String name) {
		try {
			IResource resource = Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation(TwitchIntegration.MOD_ID, "badges/badge_" + name + ".png"));
			IChatRenderable chatRenderable = BetterMinecraftChatAPI.loadImage(resource.getInputStream(), null);
			chatRenderable.setScale(0.45f);
			twitchBadges.put(name, new TwitchBadge(chatRenderable, ITooltipProvider.EMPTY));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Nullable
	public static TwitchBadge getBadge(String name) {
		return twitchBadges.get(name);
	}
}

@@ -401,43 +401,164 @@
 bmc.
-twitchintegration.TwitchIntegration
+chat.emotes.twitch.TwitchAPI;%0Aimport net.blay09.mods.bmc.twitchintegration.TwitchIntegration;%0Aimport net.blay09.mods.bmc.twitchintegration.util.TwitchHelper
 ;%0Aim
@@ -1636,16 +1636,63 @@
 /badges%22
+ + %22?client_id=%22 + TwitchHelper.OAUTH_CLIENT_ID
 , %22badge

Add client_id to badge API call.