package org.squiddev.plethora.integration.vanilla.meta;

import com.google.common.collect.Maps;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.output.NullOutputStream;
import org.squiddev.plethora.api.Injects;
import org.squiddev.plethora.api.meta.BasicMetaProvider;
import org.squiddev.plethora.integration.PlethoraIntegration;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Adds basic properties for item stacks.
 */
@Injects
public final class MetaItemBasic extends BasicMetaProvider<ItemStack> {
	@Nonnull
	@Override
	public Map<Object, Object> getMeta(@Nonnull ItemStack stack) {
		if (stack.isEmpty()) return Collections.emptyMap();

		HashMap<Object, Object> data = new HashMap<>();
		fillBasicMeta(data, stack);

		String display = stack.getDisplayName();
		data.put("displayName", display == null || display.length() == 0 ? stack.getTranslationKey() : display);
		data.put("rawName", stack.getTranslationKey());

		data.put("maxCount", stack.getMaxStackSize());
		data.put("maxDamage", stack.getMaxDamage());

		if (stack.getItem().showDurabilityBar(stack)) {
			data.put("durability", stack.getItem().getDurabilityForDisplay(stack));
		}

		return data;
	}

	@Nonnull
	public static HashMap<Object, Object> getBasicMeta(@Nonnull ItemStack stack) {
		HashMap<Object, Object> data = Maps.newHashMap();
		fillBasicMeta(data, stack);
		return data;
	}

	public static void fillBasicMeta(@Nonnull Map<? super String, Object> data, @Nonnull ItemStack stack) {
		data.put("name", stack.getItem().getRegistryName().toString());
		data.put("damage", stack.getItemDamage());
		data.put("count", stack.getCount());
		data.put("nbtHash", getNBTHash(stack));
	}

	@Nullable
	@Override
	public ItemStack getExample() {
		return new ItemStack(Items.STICK, 5);
	}

	@Nullable
	public static String getNBTHash(@Nonnull ItemStack stack) {
		return stack.hasTagCompound() ? getNBTHash(stack.getTagCompound()) : null;
	}

	@Nullable
	public static String getNBTHash(@Nullable NBTTagCompound tag) {
		if (tag == null || tag.isEmpty()) return null;

		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			OutputStream dump = new NullOutputStream();
			DigestOutputStream hasher = new DigestOutputStream(dump, digest);
			DataOutput output = new DataOutputStream(hasher);
			CompressedStreamTools.write(tag, output);
			byte[] hash = digest.digest();
			return new String(Hex.encodeHex(hash));
		} catch (NoSuchAlgorithmException | IOException e) {
			PlethoraIntegration.LOG.error("Cannot hash NBT", e);
			return null;
		}
	}
}

@@ -184,37 +184,17 @@
 aft.nbt.
-CompressedStreamTools
+*
 ;%0Aimport
@@ -211,27 +211,35 @@
 raft
-.nbt.NBTTagCompound
+forge.common.util.Constants
 ;%0Aim
@@ -494,16 +494,60 @@
 gration;
+%0Aimport org.squiddev.plethora.utils.LuaList;
 %0A%0Aimport
@@ -1707,16 +1707,524 @@
 );%0A%09%09%7D%0A%0A
+%09%09NBTTagCompound tag = stack.getTagCompound();%0A%09%09if (tag != null && tag.hasKey(%22display%22, Constants.NBT.TAG_COMPOUND)) %7B%0A%09%09%09NBTTagCompound displayTag = tag.getCompoundTag(%22display%22);%0A%09%09%09if (displayTag.hasKey(%22Lore%22, Constants.NBT.TAG_LIST)) %7B%0A%09%09%09%09NBTTagList loreTag = displayTag.getTagList(%22Lore%22, Constants.NBT.TAG_STRING);%0A%09%09%09%09LuaList%3CString%3E loreText = new LuaList%3C%3E();%0A%09%09%09%09for (NBTBase child : loreTag) loreText.add(((NBTTagString) child).getString());%0A%0A%09%09%09%09data.put(%22lore%22, loreText.asMap());%0A%09%09%09%7D%0A%09%09%7D%0A%0A
 %09%09return
@@ -2729,23 +2729,22 @@
 %0A%09%7D%0A%0A%09@N
+onn
 ull
-able
 %0A%09@Overr

Return lore from item meta providers