package dev.micle.firefly_bush_backport.data.client;

import dev.micle.firefly_bush_backport.FireflyBushBackport;
import dev.micle.firefly_bush_backport.block.ModBlocks;
import net.minecraft.data.DataGenerator;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.client.model.generators.ItemModelBuilder;
import net.minecraftforge.client.model.generators.ItemModelProvider;
import net.minecraftforge.client.model.generators.ModelFile;
import net.minecraftforge.common.data.ExistingFileHelper;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.Objects;

public class ModItemModelProvider extends ItemModelProvider {
    public ModItemModelProvider(DataGenerator generator, ExistingFileHelper existingFileHelper) {
        super(generator.getPackOutput(), FireflyBushBackport.MOD_ID, existingFileHelper);
    }

    @Override
    protected void registerModels() {
        ModBlocks.BLOCKS.getEntries().stream().map(RegistryObject::get).forEach(this::blockItemModel);
    }

    private void blockItemModel(Block block) {
        String name = Objects.requireNonNull(ForgeRegistries.BLOCKS.getKey(block)).getPath();

        if (block == ModBlocks.FIREFLY_BUSH.get()) {
            builder(getExistingFile(mcLoc("item/generated")), name);
        } else if (block.asItem() != Items.AIR) {
            withExistingParent(name, modLoc("block/" + name));
        }
    }

    private ItemModelBuilder builder(ModelFile itemGenerated, String name) {
        return getBuilder(name).parent(itemGenerated).texture("layer0", "item/" + name);
    }
}
