/*
 * Copyright (c) 2018 modmuss50 and Gigabit101
 *
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 *
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package reborncore.common.crafting;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DefaultedList;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import reborncore.common.util.NonNullListCollector;
import reborncore.common.util.serialization.SerializationUtil;
import reborncore.mixin.extensions.RecipeManagerExtensions;

import java.util.ArrayList;
import java.util.List;

public class RecipeUtils {

	public static <T extends RebornRecipe> List<T> getRecipes(World world, RebornRecipeType<?> type){
		RecipeManagerExtensions recipeManagerExtensions = (RecipeManagerExtensions) world.getRecipeManager();
		//noinspection unchecked
		return new ArrayList<>(recipeManagerExtensions.getAll(type).values());
	}

	public static DefaultedList<ItemStack> deserializeItems(JsonElement jsonObject){
		if(jsonObject.isJsonArray()){
			return SerializationUtil.stream(jsonObject.getAsJsonArray()).map(entry -> deserializeItem(entry.getAsJsonObject())).collect(NonNullListCollector.toList());
		} else {
			return DefaultedList.copyOf(deserializeItem(jsonObject.getAsJsonObject()));
		}
	}

	private static ItemStack deserializeItem(JsonObject jsonObject){
		Identifier resourceLocation = new Identifier(JsonHelper.getString(jsonObject, "item"));
		Item item = Registry.ITEM.get(resourceLocation);
		if(item == null){
			throw new IllegalStateException(resourceLocation + " did not exist");
		}
		int count = 1;
		if(jsonObject.has("count")){
			count = JsonHelper.getInt(jsonObject, "count");
		}
		//TODO support nbt
		return new ItemStack(item, count);
	}

}

@@ -1261,68 +1261,261 @@
 ort 
-net.minecraft.item.Item;%0Aimport net.minecraft.item.ItemStack
+com.mojang.datafixers.Dynamic;%0Aimport com.mojang.datafixers.types.JsonOps;%0Aimport net.minecraft.datafixers.NbtOps;%0Aimport net.minecraft.item.Item;%0Aimport net.minecraft.item.ItemStack;%0Aimport net.minecraft.item.Items;%0Aimport net.minecraft.nbt.CompoundTag
 ;%0Aim
@@ -2866,12 +2866,17 @@
  == 
-null
+Items.AIR
 )%7B%0A%09
@@ -3060,62 +3060,233 @@
 %7D%0A%09%09
-//TODO support nbt%0A%09%09return new ItemStack(item, count)
+ItemStack stack = new ItemStack(item, count);%0A%09%09if(jsonObject.has(%22tag%22))%7B%0A%09%09%09CompoundTag tag = (CompoundTag) Dynamic.convert(JsonOps.INSTANCE, NbtOps.INSTANCE, jsonObject.get(%22tag%22));%0A%09%09%09stack.setTag(tag);%0A%09%09%7D%0A%09%09return stack
 ;%0A%09%7D

Fix NBT in recipe outputs