package vg.civcraft.mc.civmodcore.items;

import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.pseudo.PseudoServer;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import vg.civcraft.mc.civmodcore.chat.ChatUtils;
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;
import vg.civcraft.mc.civmodcore.serialization.NBTCompound;
import vg.civcraft.mc.civmodcore.util.NullUtils;

public class ItemMetaTests {

	private static final ItemStack TEMPLATE_ITEM = new ItemStack(Material.STICK);

	@BeforeClass
	public static void setupBukkit() {
		PseudoServer.setup();
	}

	/**
	 * Tests whether a primitive display name can match with a component.
	 */
	@Test
	public void testBasicDisplayNameEquality() {
		// Setup
		final var formerItem = NullUtils.isNotNull(NBTCompound.processItem(TEMPLATE_ITEM, (nbt) -> {
			final var display = new NBTCompound();
			display.setString("Name", "\"Hello!\"");
			nbt.setCompound("display", display);
		}));
		final var latterItem = TEMPLATE_ITEM.clone();
		ItemUtils.setComponentDisplayName(latterItem, Component.text("Hello!"));
		// Check
		Assert.assertTrue(ChatUtils.areComponentsEqual(
				ItemUtils.getComponentDisplayName(formerItem),
				ItemUtils.getComponentDisplayName(latterItem)));
	}

	/**
	 * How do different API methods of setting the display name fare?
	 */
	@Test
	@SuppressWarnings("deprecation")
	public void testAdvancedDisplayNameEquality() {
		// Setup
		final var formerItem = TEMPLATE_ITEM.clone();
		ItemUtils.handleItemMeta(formerItem, (ItemMeta meta) -> {
			meta.setDisplayName("Hello!");
			return true;
		});
		final var latterItem = TEMPLATE_ITEM.clone();
		ItemUtils.setComponentDisplayName(latterItem, Component.text("Hello!"));
		// Check
		Assert.assertTrue(ChatUtils.areComponentsEqual(
				ItemUtils.getComponentDisplayName(formerItem),
				ItemUtils.getComponentDisplayName(latterItem)));
		Assert.assertTrue(ItemUtils.areItemsSimilar(formerItem, latterItem));
	}

	/**
	 * Tests whether {@link ChatUtils#isBaseComponent(Component)} works.
	 */
	@Test
	@SuppressWarnings("deprecation")
	public void testBaseComponent() {
		// Setup
		final var formerItem = TEMPLATE_ITEM.clone();
		ItemUtils.handleItemMeta(formerItem, (ItemMeta meta) -> {
			meta.setDisplayName("Hello!");
			return true;
		});
		final var latterItem = TEMPLATE_ITEM.clone();
		ItemUtils.handleItemMeta(latterItem, (ItemMeta meta) -> {
			meta.displayName(Component.text("Hello!"));
			return true;
		});
		// Check
		Assert.assertTrue(ChatUtils.isBaseComponent(
				ItemUtils.getComponentDisplayName(formerItem)));
		Assert.assertFalse(ChatUtils.isBaseComponent(
				ItemUtils.getComponentDisplayName(latterItem)));
	}

}

@@ -741,17 +741,20 @@
 r a 
-primitive
+basic string
  dis
@@ -997,16 +997,695 @@
 ound();%0A
+%09%09%09display.setString(%22Name%22, %22Hello!%22);%0A%09%09%09nbt.setCompound(%22display%22, display);%0A%09%09%7D));%0A%09%09final var latterItem = TEMPLATE_ITEM.clone();%0A%09%09ItemUtils.setComponentDisplayName(latterItem, Component.text(%22Hello!%22));%0A%09%09// Check%0A%09%09Assert.assertTrue(ChatUtils.areComponentsEqual(%0A%09%09%09%09ItemUtils.getComponentDisplayName(formerItem),%0A%09%09%09%09ItemUtils.getComponentDisplayName(latterItem)));%0A%09%7D%0A%0A%09/**%0A%09 * Tests whether a json primitive display name can match with a component.%0A%09 */%0A%09@Test%0A%09public void testBasicJsonPrimitiveDisplayNameEquality() %7B%0A%09%09// Setup%0A%09%09final var formerItem = NullUtils.isNotNull(NBTCompound.processItem(TEMPLATE_ITEM, (nbt) -%3E %7B%0A%09%09%09final var display = new NBTCompound();%0A
 %09%09%09displ
@@ -1831,44 +1831,76 @@
 ils.
-setComponentDisplayName(latterItem, 
+handleItemMeta(latterItem, (ItemMeta meta) -%3E %7B%0A%09%09%09meta.displayName(
 Comp
@@ -1914,32 +1914,54 @@
 ext(%22Hello!%22));%0A
+%09%09%09return true;%0A%09%09%7D);%0A
 %09%09// Check%0A%09%09Ass
@@ -2519,44 +2519,76 @@
 ils.
-setComponentDisplayName(latterItem, 
+handleItemMeta(latterItem, (ItemMeta meta) -%3E %7B%0A%09%09%09meta.displayName(
 Comp
@@ -2602,32 +2602,54 @@
 ext(%22Hello!%22));%0A
+%09%09%09return true;%0A%09%09%7D);%0A
 %09%09// Check%0A%09%09Ass

Improve ItemMetaTests