package ru.dbotthepony.mc.otm.datagen.recipes

import net.minecraft.data.recipes.RecipeCategory
import net.minecraft.data.recipes.RecipeOutput
import net.minecraft.data.recipes.ShapelessRecipeBuilder
import net.minecraft.tags.TagKey
import net.minecraft.world.item.DyeColor
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.item.crafting.Ingredient
import net.minecraft.world.level.ItemLike
import net.minecraftforge.common.Tags
import ru.dbotthepony.mc.otm.core.registryName
import ru.dbotthepony.mc.otm.datagen.modLocation
import ru.dbotthepony.mc.otm.registry.MItemTags
import ru.dbotthepony.mc.otm.registry.MItems
import ru.dbotthepony.mc.otm.registry.MRegistry

fun hammerRecipe(output: ItemLike, input: ItemLike, consumer: RecipeOutput) {
	ShapelessRecipeBuilder(RecipeCategory.MISC, output, 1)
		.requires(MItemTags.TOOLS_HAMMERS)
		.requires(input)
		.unlockedBy(MItemTags.TOOLS_HAMMERS)
		.unlockedBy(input)
		.save(consumer)
}

fun hammerRecipe(output: ItemLike, input: TagKey<Item>, consumer: RecipeOutput) {
	ShapelessRecipeBuilder(RecipeCategory.MISC, output, 1)
		.requires(MItemTags.TOOLS_HAMMERS)
		.requires(input)
		.unlockedBy(MItemTags.TOOLS_HAMMERS)
		.unlockedBy(input)
		.save(consumer)
}

fun addShapelessRecipes(consumer: RecipeOutput) {
	for (color in DyeColor.entries) {
		ShapelessRecipeBuilder(RecipeCategory.TRANSPORTATION, MItems.CARGO_CRATE_MINECARTS[color]!!, 1)
			.requires(Items.MINECART)
			.requires(MRegistry.CARGO_CRATES.items[color]!!)
			.unlockedBy(Items.MINECART)
			.unlockedBy(MRegistry.CARGO_CRATES.items[color]!!)
			.save(consumer)

		ShapelessRecipeBuilder(RecipeCategory.TRANSPORTATION, MItems.CARGO_CRATE_MINECARTS[color]!!, 1)
			.requires(Ingredient.of(MItems.CARGO_CRATE_MINECARTS.entries.stream().filter { it.key != color }.map { ItemStack(it.value) }))
			.requires(color.tag)
			.unlockedBy(MItems.CARGO_CRATE_MINECARTS.entries.stream().filter { it.key != color }.map { it.value })
			.save(consumer, modLocation(MItems.CARGO_CRATE_MINECARTS[color]!!.registryName!!.path + "_alt"))
	}

	ShapelessRecipeBuilder(RecipeCategory.TRANSPORTATION, MItems.CARGO_CRATE_MINECARTS[null]!!, 1)
		.requires(Items.MINECART)
		.requires(MRegistry.CARGO_CRATES.item)
		.unlockedBy(Items.MINECART)
		.unlockedBy(MRegistry.CARGO_CRATES.item)
		.save(consumer)

	hammerRecipe(MItems.TRITANIUM_PLATE, MItemTags.TRITANIUM_INGOTS, consumer)
	hammerRecipe(MItems.IRON_PLATE, Tags.Items.INGOTS_IRON, consumer)
	hammerRecipe(MItems.GOLD_PLATE, Tags.Items.INGOTS_GOLD, consumer)
}
