package moe.hazu.tinkeringabnormally.data;

import com.teamabnormals.caverns_and_chasms.core.registry.CCBlocks;
import com.teamabnormals.caverns_and_chasms.core.registry.CCItems;
import com.teamabnormals.environmental.core.registry.EnvironmentalBlocks;
import com.teamabnormals.upgrade_aquatic.core.registry.UABlocks;
import moe.hazu.tinkeringabnormally.TinkeringAbnormally;
import moe.hazu.tinkeringabnormally.data.material.MaterialIds;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.data.recipes.RecipeProvider;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.common.crafting.conditions.IConditionBuilder;
import slimeknights.mantle.recipe.data.IRecipeHelper;
import slimeknights.tconstruct.common.TinkerTags;
import slimeknights.tconstruct.library.materials.definition.MaterialVariantId;
import slimeknights.tconstruct.library.recipe.modifiers.adding.ModifierRecipeBuilder;
import slimeknights.tconstruct.library.recipe.modifiers.adding.SwappableModifierRecipeBuilder;
import slimeknights.tconstruct.library.tools.SlotType;
import slimeknights.tconstruct.tables.TinkerTables;
import slimeknights.tconstruct.tools.TinkerModifiers;

import java.util.function.Consumer;

public class ModifierRecipeProvider extends RecipeProvider implements IConditionBuilder, IRecipeHelper {
    public ModifierRecipeProvider(DataGenerator generator) {
        super(generator);
    }

    @Override
    public String getName() {
        return "Tinkering Abnormally Modifier Recipes";
    }

    @Override
    public String getModId() {
        return TinkeringAbnormally.MODID;
    }

    @Override
    protected void buildCraftingRecipes(Consumer<FinishedRecipe> consumer) {
        addModifierRecipes(consumer);
        addTextureRecipes(consumer);
    }

    private void addModifierRecipes(Consumer<FinishedRecipe> consumer) {
        String upgradeFolder = "tools/modifiers/upgrade/";
        String upgradeSalvage = "tools/modifiers/salvage/upgrade/";
        ModifierRecipeBuilder.modifier(ModifierIds.spinel)
                .setTools(TinkerTags.Items.DURABILITY)
                .addInput(CCItems.SPINEL.get())
                .setMaxLevel(1)
                .setSlots(SlotType.UPGRADE, 1)
                .saveSalvage(consumer, prefix(ModifierIds.spinel, upgradeSalvage))
                .save(consumer, prefix(ModifierIds.spinel, upgradeFolder));
        ModifierRecipeBuilder.modifier(ModifierIds.necromium)
                .setTools(TinkerTags.Items.DURABILITY)
                .addInput(CCItems.NECROMIUM_INGOT.get())
                .setMaxLevel(1)
                .setSlots(SlotType.UPGRADE, 1)
                .saveSalvage(consumer, prefix(ModifierIds.necromium, upgradeSalvage))
                .save(consumer, prefix(ModifierIds.necromium, upgradeFolder));
        ModifierRecipeBuilder.modifier(ModifierIds.sanguine)
                .setTools(TinkerTags.Items.WORN_ARMOR)
                .addInput(CCItems.SANGUINE_PLATING.get(), 6)
                .setMaxLevel(1)
                .setSlots(SlotType.UPGRADE, 1)
                .saveSalvage(consumer, prefix(ModifierIds.sanguine, upgradeSalvage))
                .save(consumer, prefix(ModifierIds.sanguine, upgradeFolder));
    }

    private void addTextureRecipes(Consumer<FinishedRecipe> consumer) {
        String folder = "tools/modifiers/slotless/";
        woodTexture(consumer, MaterialIds.willow, EnvironmentalBlocks.WILLOW_PLANKS.get(), folder);
        woodTexture(consumer, MaterialIds.pine, EnvironmentalBlocks.PINE_PLANKS.get(), folder);
        woodTexture(consumer, MaterialIds.cherry, EnvironmentalBlocks.CHERRY_PLANKS.get(), folder);
        woodTexture(consumer, MaterialIds.wisteria, EnvironmentalBlocks.WISTERIA_PLANKS.get(), folder);
        woodTexture(consumer, MaterialIds.driftwood, UABlocks.DRIFTWOOD_PLANKS.get(), folder);
        woodTexture(consumer, MaterialIds.river, UABlocks.RIVER_PLANKS.get(), folder);
        woodTexture(consumer, MaterialIds.azalea, CCBlocks.AZALEA_PLANKS.get(), folder);
    }

    private void woodTexture(Consumer<FinishedRecipe> consumer, MaterialVariantId material, ItemLike planks, String folder) {
        SwappableModifierRecipeBuilder.modifier(TinkerModifiers.embellishment, material.toString())
                .setTools(TinkerTags.Items.EMBELLISHMENT_WOOD)
                .addInput(planks).addInput(TinkerTables.pattern).addInput(planks)
                .save(consumer, wrap(TinkerModifiers.embellishment, folder, "/wood/" + material.getLocation('_').getPath()));
    }
}
