package com.level314.teleportanchor;

import net.fabricmc.api.ModInitializer;

import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;

import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.BiFunction;
import java.util.function.Function;

public class TeleportAnchor implements ModInitializer {
	public static final String MOD_ID = "teleportanchor";
    public static final Block TELEPORT_ANCHOR_BLOCK = register(
        "teleport_anchor",
        Block::new,
        TeleportAnchorBlockItem::new,
        BlockBehaviour.Properties.of(),
        true
    );

	// This logger is used to write text to the console and the log file.
	// It is considered best practice to use your mod id as the logger's name.
	// That way, it's clear which mod wrote info, warnings, and errors.
	public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

	@Override
	public void onInitialize() {
		// This code runs as soon as Minecraft is in a mod-load-ready state.
		// However, some things (like resources) may still be uninitialized.
		// Proceed with mild caution.
		LOGGER.info("Mod \"TeleportAnchor\" initialized!");
	}

    private static <T extends BlockItem> Block register(
        String name,
        Function<BlockBehaviour.Properties, Block> blockFactory,
        BiFunction<Block, Item.Properties, T> itemFactory,
        BlockBehaviour.Properties properties,
        boolean shouldRegisterItem
    ) {
        // Create a registry key for the block
        ResourceKey<Block> blockKey = ResourceKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath(TeleportAnchor.MOD_ID, name));
        // Create the block instance
        Block block = blockFactory.apply(properties.setId(blockKey));

        // Sometimes, you may not want to register an item for the block.
        // Eg: if it's a technical block like `minecraft:moving_piston` or `minecraft:end_gateway`
        if (shouldRegisterItem) {
            // Items need to be registered with a different type of registry key, but the ID
            // can be the same.
            ResourceKey<Item> itemKey = ResourceKey.create(Registries.ITEM,
                Identifier.fromNamespaceAndPath(TeleportAnchor.MOD_ID, name));

            Item.Properties itemProps = new Item.Properties()
                .setId(itemKey);

            T blockItem = itemFactory.apply(block, itemProps);
            Registry.register(BuiltInRegistries.ITEM, itemKey, blockItem);
        }

        return Registry.register(BuiltInRegistries.BLOCK, blockKey, block);
    }

	public static Identifier id(String path) {
		return Identifier.fromNamespaceAndPath(MOD_ID, path);
	}
}
