package com.husj;

import com.husj.block.DisassemblerBlock;
import com.husj.block.entity.DisassemblerBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.util.Identifier;

/**
 * 模组方块注册类
 * 
 * 注册拆解台方块和对应的方块实体。
 * 
 * 注意：在 Minecraft 1.21+ 中，物品模型需要在 assets/husj/items/ 目录下定义，
 * 而不是传统的 assets/husj/models/item/ 目录。
 * 
 * 资源文件结构：
 * - 方块状态: assets/husj/blockstates/disassembler.json
 * - 方块模型: assets/husj/models/block/disassembler.json
 * - 物品定义: assets/husj/items/disassembler.json (1.21+新格式)
 * - 合成配方: data/husj/recipe/disassembler.json (注意是单数)
 * - 战利品表: data/husj/loot_table/blocks/disassembler.json (注意是单数)
 */

public class ModBlocks {
    public static final Block DISASSEMBLER_BLOCK = registerBlock("disassembler",
            DisassemblerBlock::new, AbstractBlock.Settings.copy(Blocks.IRON_BLOCK).nonOpaque().requiresTool());

    public static final BlockEntityType<DisassemblerBlockEntity> DISASSEMBLER_BLOCK_ENTITY = Registry.register(
            Registries.BLOCK_ENTITY_TYPE,
            Identifier.of("husj", "disassembler_block_entity"),
            FabricBlockEntityTypeBuilder.create(DisassemblerBlockEntity::new, DISASSEMBLER_BLOCK).build());

    private static Block registerBlock(String name, java.util.function.Function<AbstractBlock.Settings, Block> factory,
            AbstractBlock.Settings settings) {
        RegistryKey<Block> blockKey = RegistryKey.of(RegistryKeys.BLOCK, Identifier.of("husj", name));
        settings.registryKey(blockKey);
        Block block = factory.apply(settings);

        Registry.register(Registries.BLOCK, blockKey, block);

        RegistryKey<Item> itemKey = RegistryKey.of(RegistryKeys.ITEM, Identifier.of("husj", name));
        Item.Settings itemSettings = new Item.Settings().registryKey(itemKey).useBlockPrefixedTranslationKey();
        Registry.register(Registries.ITEM, itemKey, new BlockItem(block, itemSettings));

        return block;
    }

    public static void registerModBlocks() {
        Husj.LOGGER.info("Registering ModBlocks for " + Husj.MOD_ID);
    }
}
