package me.km.utils;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.nbt.StringNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;

public class ItemStackUtils {
    public static void setLore(ItemStack stack, List<Object> list) {
        CompoundNBT com = stack.getOrCreateChildTag("display");
        ListNBT nbtList = new ListNBT();
        list.forEach(s -> nbtList.add(StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s.toString())))));
        com.put("Lore", nbtList);
    }

    public static void addLore(ItemStack stack, String s) {
        CompoundNBT com = stack.getOrCreateChildTag("display");
        ListNBT nbtList = com.getList("Lore", 8);
        nbtList.add(StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
        com.put("Lore", nbtList);
    }

    public static List<Object> getLore(ItemStack stack) {
        CompoundNBT com = stack.getOrCreateChildTag("display");
        ListNBT nbtList = com.getList("Lore", 8);
        ArrayList<Object> list = new ArrayList<>();
        for(int i = 0; i < nbtList.size(); i++) {
            list.add(ITextComponent.Serializer.getComponentFromJson(nbtList.getString(i)).getString());
        }
        return list;
    }

    public static void addLore(ItemStack stack, String s, int i) {
        CompoundNBT com = stack.getOrCreateChildTag("display");
        ListNBT list = com.getList("Lore", 8);
        if(i >= list.size()) {
            list.add(StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
        } else {
            list.set(i, StringNBT.valueOf(ITextComponent.Serializer.toJson(new StringTextComponent(s))));
        }
        com.put("Lore", list);
    }
}
