package net.blay09.mods.chattweaks.chat.emotes.twitch;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.blay09.mods.chattweaks.chat.emotes.IEmote;
import net.blay09.mods.chattweaks.balyware.CachedAPI;
import net.minecraft.util.IntHashMap;

import javax.annotation.Nullable;
import java.util.Map;

public class TwitchEmotesAPI {

	public static final String CLIENT_ID = "gdhi94otnk7c7746syjv7gkr6bizq4w";

	public static final int EMOTESET_GLOBAL = 0;
	public static final int EMOTESET_TURBO = 19194;

	private static final IntHashMap<String> emoteSets = new IntHashMap<>();
	private static final IntHashMap<IEmote> twitchEmotes = new IntHashMap<>();

	public static void loadEmoteSets() throws Exception {
		JsonObject sets = CachedAPI.loadCachedAPI("https://twitchemotes.com/api_cache/v3/sets.json", "twitch_emotesets_v3.json", null);
		if(sets != null) {
			for(Map.Entry<String, JsonElement> entry : sets.entrySet()) {
				emoteSets.addKey(Integer.parseInt(entry.getKey()), entry.getValue().getAsJsonObject().get("channel_name").getAsString());
			}
		}
	}

	@Nullable
	public static String getChannelForEmoteSet(int emoteSet) {
		return emoteSets.lookup(emoteSet);
	}

	@Nullable
	public static JsonObject loadEmotes(int... emotesets) {
		StringBuilder sb = new StringBuilder();
		for (int emoteset : emotesets) {
			if (sb.length() > 0) {
				sb.append(",");
			}
			sb.append(emoteset);
		}
		String url = "https://api.twitch.tv/kraken/chat/emoticon_images?api_version=5&client_id=" + CLIENT_ID;
		if(emotesets.length > 0) {
			url += "&emotesets=" + sb.toString();
		}
		return CachedAPI.loadCachedAPI(url, "twitch_emotes" + (sb.length() > 0 ? "-" + sb.toString() : "") + ".json", "application/vnd.twitchtv.v5+json");
	}

	public static void registerTwitchEmote(int id, IEmote emote) {
		twitchEmotes.addKey(id, emote);
	}

	@Nullable
	public static IEmote getEmoteById(int id) {
		return twitchEmotes.lookup(id);
	}

}

@@ -53,78 +53,66 @@
 h;%0A%0A
+%0A
 import 
-com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;
+java.util.Map;%0A%0Aimport javax.annotation.Nullable;%0A
 %0Aimp
@@ -146,26 +146,26 @@
 aks.
-chat.emotes.IEmote
+balyware.CachedAPI
 ;%0Aim
@@ -196,34 +196,34 @@
 ttweaks.
-balyware.CachedAPI
+chat.emotes.IEmote
 ;%0Aimport
@@ -266,56 +266,72 @@
 ort 
-javax.annotation.Nullable;%0Aimport java.util.Map;
+com.google.gson.JsonElement;%0Aimport com.google.gson.JsonObject;%0A
 %0A%0Apu
@@ -405,39 +405,38 @@
  = %22
-gdhi94otnk7c7746syjv7gkr6bizq4w
+10napoemcms7qf9j0dxf28ndl2ltc3
 %22;%0A%0A
@@ -780,16 +780,17 @@
 chedAPI(
+ 
 %22https:/
@@ -868,16 +868,17 @@
 null
+ 
 );%0A%09%09if(
 sets
@@ -873,16 +873,17 @@
 );%0A%09%09if(
+ 
 sets != 
@@ -886,16 +886,17 @@
  != null
+ 
 ) %7B%0A%09%09%09f

Update Client ID to fix emotes