package com.r3944realms.leashedplayer.content.items;

import com.r3944realms.leashedplayer.LeashedPlayer;
import com.r3944realms.leashedplayer.content.blocks.ModBlocksRegister;
import com.r3944realms.leashedplayer.content.items.type.*;
import com.r3944realms.leashedplayer.datagen.provider.attributes.ModJukeboxSongs;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.JukeboxSong;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.alchemy.PotionContents;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class ModItemRegister {
    public static final DeferredRegister<Item> ITEMS =
            DeferredRegister.create(BuiltInRegistries.ITEM, LeashedPlayer.MOD_ID);
    public static final List<DeferredHolder<Item, Item>> ITEM_SUPPLIER = new ArrayList<>();
    public static final DeferredHolder<Item, Item> LEASH_ROPE_ARROW = ModItemRegister.register("leash_rope_arrow",
            () -> new LeashRopeArrowItem(new Item.Properties().stacksTo(16)
                    .setId(ModItemResourceKeys.LEASH_ROPE_ARROW.getResourceKey()))
    );
    public static final DeferredHolder<Item, Item> SPECTRAL_LEASH_ROPE_ARROW = ModItemRegister.register("spectral_leash_rope_arrow",
            () -> new SpectralLeashRopeArrowItem(new Item.Properties().stacksTo(16)
                    .setId(ModItemResourceKeys.SPECTRAL_LEASH_ROPE_ARROW.getResourceKey())));

    public static final DeferredHolder<Item, Item> TIPPED_LEASH_ROPE_ARROW = ModItemRegister.register("tipped_leash_rope_arrow",
            () -> new TippedLeashRopeArrowItem(new Item.Properties().stacksTo(16).component(DataComponents.POTION_CONTENTS, PotionContents.EMPTY)
                    .setId(ModItemResourceKeys.TIPPED_LEASH_ROPE_ARROW.getResourceKey())));
    public static final DeferredHolder<Item, Item> AMETHYST_SHEARS = ModItemRegister.register("amethyst_shears",
            () -> new LeadBreakerItem(ModToolMaterials.AMETHYST, new Item.Properties().durability(100).component(DataComponents.TOOL, LeadBreakerItem.createToolProperties())
                    .stacksTo(1)
                    .setId(ModItemResourceKeys.AMETHYST_SHEARS.getResourceKey())));
    public static final DeferredHolder<Item, Item> FABRIC = ModItemRegister.register("fabric",
            () -> new TestItem(new Item.Properties().stacksTo(1)
                    .setId(ModItemResourceKeys.FABRIC.getResourceKey())));
    public static final DeferredHolder<Item, Item> NEOFORGE =  ModItemRegister.register("neoforge",
            () -> new Item(DistProperties(ModJukeboxSongs.FOX_MUSIC)
                    .setId(ModItemResourceKeys.NEOFORGE.getResourceKey())));
    public static final DeferredHolder<Item, BlockItem> TEST_BLOCK_ITEM =
            ModBlocksRegister.registerBlockItem("test_block_item", ModBlocksRegister.TEST_BLOCK, ModItemResourceKeys.TEST_BLOCK_ITEM.getResourceKey());
    public static Item.Properties DistProperties(ResourceKey<JukeboxSong> song) {
        return new Item.Properties().stacksTo(1).rarity(Rarity.RARE).jukeboxPlayable(song);
    }
    public static DeferredHolder<Item, BlockItem> registerBlockItem(String name, Supplier<? extends BlockItem> sup) {
        return ITEMS.register(name, sup);
    }
    public static DeferredHolder<Item, Item> register(String name, Supplier<Item> supplier) {
        return register(name, supplier, true);
    }

    public static DeferredHolder<Item, Item> register(String name, Supplier<Item> supplier, boolean shouldJoinSupplierLists) {
        DeferredHolder<Item, Item> supplierItem = ITEMS.register(name, supplier);
        if(shouldJoinSupplierLists) ITEM_SUPPLIER.add(supplierItem);
        return supplierItem;
    }

    public static void register(IEventBus eventBus) {
        ITEMS.register(eventBus);
    }
}
