package com.xfestudio.mydimensionlord.registry;

import com.xfestudio.mydimensionlord.MyDimension;
import com.xfestudio.mydimensionlord.equipment.GearSet;
import com.xfestudio.mydimensionlord.platform.EquipmentFactory;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import java.util.List;

/** New content uses the addon namespace; only migrated builder items keep the old IDs. */
public final class EquipmentItems {
    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MyDimension.MOD_ID);
    public static final RegistryObject<Item> MIND_SHARD = material("mind_shard");
    public static final RegistryObject<Item> LORD_ALLOY = material("lord_alloy"); // Legacy save ID; converts into a core.
    public static final RegistryObject<Item> LORD_CORE = material("lord_core");
    public static final RegistryObject<Item> LORD_UPGRADE_TEMPLATE = ITEMS.register("lord_upgrade_template", EquipmentFactory::template);

    public static final RegistryObject<Item> LORD_HELMET = armor(GearSet.LORD, EquipmentSlot.HEAD);
    public static final RegistryObject<Item> LORD_CHESTPLATE = armor(GearSet.LORD, EquipmentSlot.CHEST);
    public static final RegistryObject<Item> LORD_LEGGINGS = armor(GearSet.LORD, EquipmentSlot.LEGS);
    public static final RegistryObject<Item> LORD_BOOTS = armor(GearSet.LORD, EquipmentSlot.FEET);
    public static final RegistryObject<Item> DIMENSION_BLADE = ITEMS.register("dimension_blade", EquipmentFactory::blade);
    public static final RegistryObject<Item> DIMENSION_PICKAXE = ITEMS.register("dimension_pickaxe", EquipmentFactory::pickaxe);
    public static final List<RegistryObject<Item>> ARMOR = List.of(LORD_HELMET, LORD_CHESTPLATE, LORD_LEGGINGS, LORD_BOOTS);

    private EquipmentItems() {}
    private static RegistryObject<Item> material(String name) { return ITEMS.register(name, () -> EquipmentFactory.material(name)); }
    private static RegistryObject<Item> armor(GearSet set, EquipmentSlot slot) {
        return ITEMS.register(set.id() + "_" + slotName(slot), () -> EquipmentFactory.armor(set, slot));
    }
    public static String slotName(EquipmentSlot slot) {
        return switch (slot) { case HEAD -> "helmet"; case CHEST -> "chestplate"; case LEGS -> "leggings"; case FEET -> "boots"; default -> throw new IllegalArgumentException("Not an armor slot"); };
    }
    public static int slotIndex(EquipmentSlot slot) {
        return switch (slot) { case HEAD -> 0; case CHEST -> 1; case LEGS -> 2; case FEET -> 3; default -> throw new IllegalArgumentException("Not an armor slot"); };
    }
    public static Item armorItem(GearSet set, EquipmentSlot slot) { return ARMOR.get(set.ordinal() * 4 + slotIndex(slot)).get(); }
    public static Item repairMaterial(GearSet set) {
        return LORD_CORE.get();
    }
    public static void register(IEventBus bus) { ITEMS.register(bus); }
    public static void addCreative(BuildCreativeModeTabContentsEvent event) {
        if (event.getTabKey() == CreativeModeTabs.COMBAT) {
            for (var armor : ARMOR) event.accept(armor.get());
            event.accept(DIMENSION_BLADE.get());
        }
        if (event.getTabKey() == CreativeModeTabs.INGREDIENTS)
            for (var ingredient : List.of(MIND_SHARD, LORD_CORE, LORD_UPGRADE_TEMPLATE)) event.accept(ingredient.get());
        if (event.getTabKey() == CreativeModeTabs.TOOLS_AND_UTILITIES) event.accept(DIMENSION_PICKAXE.get());
    }
}
