package com.extendedae_plus.init;

import appeng.api.parts.IPart;
import appeng.api.parts.PartModels;
import appeng.items.parts.PartModelsHelper;
import com.extendedae_plus.ExtendedAEPlus;
import com.extendedae_plus.ae.definitions.upgrades.EntitySpeedCardItem;
import com.extendedae_plus.ae.items.ChannelCardItem;
import com.extendedae_plus.ae.items.EntitySpeedTickerPartItem;
import com.extendedae_plus.ae.items.InfinityBigIntegerCellItem;
import com.extendedae_plus.util.ModCheckUtils;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public final class ModItems {
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ExtendedAEPlus.MODID);

    public static final RegistryObject<Item> WIRELESS_TRANSCEIVER = ITEMS.register(
            "wireless_transceiver",
            () -> new BlockItem(ModBlocks.WIRELESS_TRANSCEIVER.get(), new Item.Properties())
    );

    public static final RegistryObject<Item> NETWORK_PATTERN_CONTROLLER = ITEMS.register(
            "network_pattern_controller",
            () -> new BlockItem(ModBlocks.NETWORK_PATTERN_CONTROLLER.get(), new Item.Properties())
    );

    // 装配矩阵上传核心（方块物品）
    public static final RegistryObject<Item> ASSEMBLER_MATRIX_UPLOAD_CORE = ITEMS.register(
            "assembler_matrix_upload_core",
            () -> new BlockItem(ModBlocks.ASSEMBLER_MATRIX_UPLOAD_CORE.get(), new Item.Properties())
    );

    // Crafting Accelerators
    public static final RegistryObject<Item> ACCELERATOR_4x = ITEMS.register(
            "4x_crafting_accelerator",
            () -> new BlockItem(ModBlocks.ACCELERATOR_4x.get(), new Item.Properties())
    );

    public static final RegistryObject<Item> ACCELERATOR_16x = ITEMS.register(
            "16x_crafting_accelerator",
            () -> new BlockItem(ModBlocks.ACCELERATOR_16x.get(), new Item.Properties())
    );

    public static final RegistryObject<Item> ACCELERATOR_64x = ITEMS.register(
            "64x_crafting_accelerator",
            () -> new BlockItem(ModBlocks.ACCELERATOR_64x.get(), new Item.Properties())
    );

    public static final RegistryObject<Item> ACCELERATOR_256x = ITEMS.register(
            "256x_crafting_accelerator",
            () -> new BlockItem(ModBlocks.ACCELERATOR_256x.get(), new Item.Properties())
    );

    public static final RegistryObject<Item> ACCELERATOR_1024x = ITEMS.register(
            "1024x_crafting_accelerator",
            () -> new BlockItem(ModBlocks.ACCELERATOR_1024x.get(), new Item.Properties())
    );

    public static final RegistryObject<EntitySpeedTickerPartItem> ENTITY_TICKER_PART_ITEM = ITEMS.register(
            "entity_speed_ticker",
            () -> new EntitySpeedTickerPartItem(new Item.Properties())
    );

    // AE Upgrade Cards: 实体加速卡（四个等级：x2,x4,x8,x16）
    // 单一实体加速卡 Item（不同等级由 ItemStack.nbt 存储）
    public static final RegistryObject<EntitySpeedCardItem> ENTITY_SPEED_CARD = ITEMS.register(
            "entity_speed_card",
            () -> new EntitySpeedCardItem(new Item.Properties())
    );

    public static final RegistryObject<InfinityBigIntegerCellItem> INFINITY_BIGINTEGER_CELL_ITEM = ITEMS.register(
            "infinity_biginteger_cell", InfinityBigIntegerCellItem::new
    );

    // 频道卡（作为 AE 升级卡使用）
    public static final RegistryObject<ChannelCardItem> CHANNEL_CARD = ITEMS.register(
            "channel_card",
            () -> new ChannelCardItem(new Item.Properties())
    );

    public static final RegistryObject<Item> STORAGE_CORE = ITEMS.register(
            "storage_core",
            () -> new Item(new Item.Properties())
    );
    public static final RegistryObject<Item> SPATIAL_CORE = ITEMS.register(
            "spatial_core",
            () -> new Item(new Item.Properties())
    );
    public static final RegistryObject<Item> OBLIVION_SINGULARITY = ITEMS.register(
            "oblivion_singularity",
            () -> new Item(new Item.Properties())
    );
    public static final RegistryObject<Item> ENERGY_CORE;
    public static final RegistryObject<Item> QUANTUM_STORAGE_CORE;

    static {
        if (ModCheckUtils.isAppfluxLoading()) {
            ENERGY_CORE = ITEMS.register(
                    "energy_core",
                    () -> new Item(new Item.Properties())
            );
        } else {
            ENERGY_CORE = null;
        }

        if (ModCheckUtils.isAAELoading()) {
            QUANTUM_STORAGE_CORE = ITEMS.register(
                    "quantum_storage_core",
                    () -> new Item(new Item.Properties())
            );
        } else {
            QUANTUM_STORAGE_CORE = null;
        }
    }

    private ModItems() {}

    /**
     * 为 PartItem 注册 AE2 部件模型。
     * 在客户端进行模型/几何体注册时调用。
     */
    public static void registerPartModels() {
        PartModels.registerModels(
                PartModelsHelper.createModels(
                        ENTITY_TICKER_PART_ITEM.get().getPartClass().asSubclass(IPart.class)
                )
        );
    }

    /**
     * 工厂：创建带 multiplier 的实体加速卡 ItemStack（2/4/8/16）
     */
    public static ItemStack createEntitySpeedCardStack(int multiplier) {
        return EntitySpeedCardItem.withMultiplier(multiplier);
    }
}
