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

import net.minecraft.data.recipes.RecipeOutput
import net.minecraft.world.item.Item
import net.minecraft.world.item.Items
import net.neoforged.neoforge.common.Tags
import ru.dbotthepony.mc.otm.registry.MItemTags
import ru.dbotthepony.mc.otm.registry.game.MItems

private fun makeNormalRecipe(element: Item, result: Item): MatteryRecipe {
	return MatteryRecipe(result, 1)
		.row(MItemTags.COPPER_WIRES, Items.REDSTONE, MItemTags.COPPER_WIRES)
		.row(MItemTags.CARBON_PLATES, element, MItemTags.CARBON_PLATES)
		.rowB(MItems.ELECTRIC_PARTS)
		.unlockedBy(MItemTags.IRON_PLATES)
		.unlockedBy(MItems.ELECTRIC_PARTS)
		.unlockedBy(element)
}

private fun makeAdvancedRecipe(element: Item, result: Item): MatteryRecipe {
	return MatteryRecipe(result, 1)
		.row(MItemTags.GOLD_WIRES, MItemTags.DILITHIUM_GEMS, MItemTags.GOLD_WIRES)
		.row(MItemTags.TRITANIUM_PLATES, element, MItemTags.TRITANIUM_PLATES)
		.row(MItems.ELECTRIC_PARTS, MItemTags.DILITHIUM_GEMS, MItems.ELECTRIC_PARTS)
		.unlockedBy(MItemTags.TRITANIUM_PLATES)
		.unlockedBy(MItems.ELECTRIC_PARTS)
		.unlockedBy(element)
}

fun addMachineUpgradeRecipes(consumer: RecipeOutput) {
	MatteryRecipe(MItems.MachineUpgrades.Basic.BLANK)
		.rowB(Items.REDSTONE)
		.rowAC(MItemTags.IRON_PLATES, MItemTags.IRON_PLATES)
		.rowB(MItems.ELECTRIC_PARTS)
		.unlockedBy(MItemTags.IRON_PLATES)
		.unlockedBy(MItems.ELECTRIC_PARTS)
		.build(consumer)

	makeNormalRecipe(MItems.MachineUpgrades.Basic.BLANK, MItems.MachineUpgrades.Normal.BLANK)
		.build(consumer)

	makeAdvancedRecipe(MItems.MachineUpgrades.Normal.BLANK, MItems.MachineUpgrades.Advanced.BLANK)
		.build(consumer)

	makeNormalRecipe(MItems.MachineUpgrades.Basic.SPEED, MItems.MachineUpgrades.Normal.SPEED)
		.build(consumer, "upgrade")
	makeNormalRecipe(MItems.MachineUpgrades.Basic.ENERGY_CONSUMPTION, MItems.MachineUpgrades.Normal.ENERGY_CONSUMPTION)
		.build(consumer, "upgrade")
	makeNormalRecipe(MItems.MachineUpgrades.Basic.FAILSAFE, MItems.MachineUpgrades.Normal.FAILSAFE)
		.build(consumer, "upgrade")
	makeNormalRecipe(MItems.MachineUpgrades.Basic.ENERGY_STORAGE, MItems.MachineUpgrades.Normal.ENERGY_STORAGE)
		.build(consumer, "upgrade")
	makeNormalRecipe(MItems.MachineUpgrades.Basic.MATTER_STORAGE, MItems.MachineUpgrades.Normal.MATTER_STORAGE)
		.build(consumer, "upgrade")
	makeNormalRecipe(MItems.MachineUpgrades.Basic.PROCESSING_ITEMS, MItems.MachineUpgrades.Normal.PROCESSING_ITEMS)
		.build(consumer, "upgrade")

	makeAdvancedRecipe(MItems.MachineUpgrades.Normal.SPEED, MItems.MachineUpgrades.Advanced.SPEED)
		.build(consumer, "upgrade")
	makeAdvancedRecipe(MItems.MachineUpgrades.Normal.ENERGY_CONSUMPTION, MItems.MachineUpgrades.Advanced.ENERGY_CONSUMPTION)
		.build(consumer, "upgrade")
	makeAdvancedRecipe(MItems.MachineUpgrades.Normal.FAILSAFE, MItems.MachineUpgrades.Advanced.FAILSAFE)
		.build(consumer, "upgrade")
	makeAdvancedRecipe(MItems.MachineUpgrades.Normal.ENERGY_STORAGE, MItems.MachineUpgrades.Advanced.ENERGY_STORAGE)
		.build(consumer, "upgrade")
	makeAdvancedRecipe(MItems.MachineUpgrades.Normal.MATTER_STORAGE, MItems.MachineUpgrades.Advanced.MATTER_STORAGE)
		.build(consumer, "upgrade")
	makeAdvancedRecipe(MItems.MachineUpgrades.Normal.PROCESSING_ITEMS, MItems.MachineUpgrades.Advanced.PROCESSING_ITEMS)
		.build(consumer, "upgrade")

	for (tier in MItems.MachineUpgrades.CRAFTABLE_TIERS) {
		MatteryRecipe(tier.SPEED, 1)
			.rowB(Items.REDSTONE)
			.row(Items.SUGAR, MItemTags.COPPER_WIRES, Items.SUGAR)
			.row(MItems.MECHANICAL_PARTS, tier.BLANK, MItems.MECHANICAL_PARTS)
			.unlockedBy(tier.BLANK)
			.unlockedBy(MItemTags.COPPER_WIRES)
			.build(consumer)

		MatteryRecipe(tier.ENERGY_CONSUMPTION, 1)
			.rowB(Items.REDSTONE)
			.row(MItemTags.COPPER_WIRES, MItemTags.GOLD_WIRES, MItemTags.COPPER_WIRES)
			.row(MItems.ELECTRIC_PARTS, tier.BLANK, MItems.ELECTRIC_PARTS)
			.unlockedBy(tier.BLANK)
			.unlockedBy(MItemTags.GOLD_WIRES)
			.build(consumer)

		MatteryRecipe(tier.FAILSAFE, 1)
			.rowB(MItems.ELECTRIC_PARTS)
			.row(MItemTags.COPPER_WIRES, MItems.QUANTUM_TRANSCEIVER, MItemTags.COPPER_WIRES)
			.row(Tags.Items.DUSTS_GLOWSTONE, tier.BLANK, Tags.Items.DUSTS_GLOWSTONE)
			.unlockedBy(tier.BLANK)
			.unlockedBy(MItems.QUANTUM_TRANSCEIVER)
			.build(consumer)

		MatteryRecipe(tier.ENERGY_STORAGE, 1)
			.rowB(Items.REDSTONE)
			.row(Items.REDSTONE, MItems.ENERGY_BUS, Items.REDSTONE)
			.row(MItems.ELECTRIC_PARTS, tier.BLANK, MItems.ELECTRIC_PARTS)
			.unlockedBy(tier.BLANK)
			.unlockedBy(MItems.ENERGY_BUS)
			.build(consumer)

		MatteryRecipe(tier.MATTER_STORAGE, 1)
			.rowB(MItemTags.HARDENED_GLASS)
			.row(MItemTags.HARDENED_GLASS, MItems.MATTER_CAPACITOR_PARTS, MItemTags.HARDENED_GLASS)
			.row(MItems.MATTER_CAPACITOR_PARTS, tier.BLANK, MItems.MATTER_CAPACITOR_PARTS)
			.unlockedBy(tier.BLANK)
			.unlockedBy(MItems.MATTER_CAPACITOR_PARTS)
			.build(consumer)

		MatteryRecipe(tier.PROCESSING_ITEMS, 1)
			.rowB(MItems.MECHANICAL_PARTS)
			.row(MItems.ELECTROMAGNET, MItemTags.TRITANIUM_PLATES, MItems.ELECTROMAGNET)
			.row(Tags.Items.ENDER_PEARLS, tier.BLANK, Tags.Items.ENDER_PEARLS)
			.unlockedBy(tier.BLANK)
			.unlockedBy(MItems.ELECTROMAGNET)
			.build(consumer)
	}
}
