/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.item;

import com.remains_cave.RemainsCaveMod;
import com.remains_cave.block.ModBlocks;
import com.remains_cave.entity.ModEntities;
import com.remains_cave.item.component.QuiverContent;
import com.remains_cave.item.custom.*;
import com.salinity.ModAttributes;
import com.salinity.SalinityMod;
import net.minecraft.ChatFormatting;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlotGroup;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.*;
import net.minecraft.world.item.component.ItemAttributeModifiers;
import net.neoforged.neoforge.common.DeferredSpawnEggItem;
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.component.ItemLore;
import java.util.List;
import java.util.function.Supplier;
public class ModItems {
    public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(RemainsCaveMod.MODID);
    public static final DeferredItem<Item> SKELETON_WARRIOR_SPAWN_EGG = ITEMS.register("skeleton_warrior_spawn_egg",
            () -> new DeferredSpawnEggItem(ModEntities.SKELETON_WARRIOR, 0xFFFFFF, 0xFFFFFF, new Item.Properties()));
    public static final Supplier<Item> TOMB_ARROW = ITEMS.register("tomb_arrow",
            () -> new CustomArrowItem(new Item
                    .Properties().stacksTo(64)
                    .component(DataComponents.LORE, new ItemLore(List.of(
                            Component.translatable("item.remains_cave.tomb_arrow.desc1").withStyle(ChatFormatting.GRAY)
                    )))
            ));
    public static final DeferredItem<Item> SKELETON_ARCHER_SPAWN_EGG = ITEMS.register("skeleton_archer_spawn_egg",
            () -> new DeferredSpawnEggItem(ModEntities.SKELETON_ARCHER, 0xFFFFFF, 0xFFFFFF, new Item.Properties()));
    public static final DeferredItem<Item> SKELETON_WARLOCK_SPAWN_EGG = ITEMS.register("skeleton_warlock_spawn_egg",
            () -> new DeferredSpawnEggItem(ModEntities.SKELETON_WARLOCK, 0xFFFFFF, 0xFFFFFF, new Item.Properties()));
    public static final DeferredItem<Item> SKELETON_KING_SPAWN_EGG = ITEMS.register("skeleton_king_spawn_egg",
            () -> new DeferredSpawnEggItem(ModEntities.SKELETON_KING, 0xFFFFFF, 0xFFFFFF, new Item.Properties()));
    // 新武器
    public static final DeferredItem<Item> CRIMSON_BOND = ITEMS.register("crimson_bond",
            () -> new CrimsonBondItem(
                    ModToolTiers.CHAOS_SKELETON,
                    new Item.Properties()
                            .attributes(SwordItem.createAttributes(ModToolTiers.CHAOS_SKELETON, 9, -3.5f)
                                    // 添加攻击距离属性修饰符
                                    .withModifierAdded(
                                            ModAttributes.ATTACK_RANGE,   // 使用 Salinity 的攻击距离属性
                                            new AttributeModifier(
                                                    ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "crimson_bond_range"),
                                                    1.25,
                                                    AttributeModifier.Operation.ADD_VALUE
                                            ),
                                            EquipmentSlotGroup.MAINHAND   // 仅主手持有时生效
                                    )
                            )
                            .component(DataComponents.LORE, new ItemLore(List.of(
                                    Component.translatable("item.remains_cave.crimson_bond.desc1").withStyle(ChatFormatting.RED),
                                    Component.translatable("item.remains_cave.crimson_bond.desc2").withStyle(ChatFormatting.GRAY)
                            )))
            ));


    public static final DeferredItem<Item> BONE_HATCHET = ITEMS.register("bone_hatchet",
            () -> new BoneHatchetItem(
                    ModToolTiers.CHAOS_SKELETON,
                    new Item.Properties()
                            .attributes(AxeItem.createAttributes(ModToolTiers.CHAOS_SKELETON, 4, -2.5f))
            ));

    public static final DeferredItem<Item> DOOM_STAFF = ITEMS.register("doom_staff",
            () -> new DoomStaffItem(
                    ModToolTiers.CHAOS_SKELETON,
                    new Item.Properties()
                            .attributes(AxeItem.createAttributes(ModToolTiers.CHAOS_SKELETON, 3, -3f))
            ));
    public static final DeferredItem<Item> TOMB_BOW = ITEMS.register("tomb_bow",
            () -> new TombBowItem(new Item.Properties()
                    .durability(2304)
                    .stacksTo(1)
                    .component(
                            (DataComponentType<Integer>) BuiltInRegistries.DATA_COMPONENT_TYPE.get(ResourceLocation.parse("minecraft:enchantable")),
                            15
                    )
            ));
    public static final DeferredItem<BoneStatelyItem> BONE_STATELY = ITEMS.register("bone_stately",
            () -> new BoneStatelyItem(
                    ModArmorMaterials.CHAOS_SKELETON,
                    ArmorItem.Type.HELMET,
                    new Item.Properties()
                            .stacksTo(1)
                            .durability(2304)
                            .component(DataComponents.ATTRIBUTE_MODIFIERS, createBoneStatelyModifiers())
                            .component(DataComponents.LORE, new ItemLore(List.of(
                                    Component.translatable("item.remains_cave.bone_stately.desc1").withStyle(ChatFormatting.RED),
                                    Component.translatable("item.remains_cave.bone_stately.desc2").withStyle(ChatFormatting.GRAY)
                            )
                            ))
            ));
    //墟墓宽帽
    public static final DeferredItem<TombHatItem> TOMB_HAT = ITEMS.register("tomb_hat",
            () -> new TombHatItem(
                    ModArmorMaterials.CHAOS_SKELETON,
                    ArmorItem.Type.HELMET,
                    new Item.Properties()
                            .stacksTo(1)
                            .durability(1843)
                            .component(DataComponents.ATTRIBUTE_MODIFIERS, createTombHatModifiers())
                    // 可添加属性修饰符、lore等
            ));
    private static ItemAttributeModifiers createTombHatModifiers() {
        ResourceLocation speedId = ResourceLocation.fromNamespaceAndPath(SalinityMod.MOD_ID, "tomb_hat_speed");
        ResourceLocation damageId = ResourceLocation.fromNamespaceAndPath(SalinityMod.MOD_ID, "tomb_hat_damage");
        AttributeModifier speedMod = new AttributeModifier(speedId, 0.03, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL);
        AttributeModifier damageMod = new AttributeModifier(damageId, 2.5, AttributeModifier.Operation.ADD_VALUE);
        return ItemAttributeModifiers.builder()
                .add(ModAttributes.ARROW_SPEED, speedMod, EquipmentSlotGroup.HEAD)
                .add(ModAttributes.ARROW_DAMAGE, damageMod, EquipmentSlotGroup.HEAD)
                .build();
    }
//骨噬头盔
    public static final DeferredItem<BoneHelmetItem> BONE_HELMET = ITEMS.register("bone_helmet",
            () -> new BoneHelmetItem(
                    ModArmorMaterials.CHAOS_SKELETON,
                    ArmorItem.Type.HELMET,
                    new Item.Properties()
                            .component(DataComponents.ATTRIBUTE_MODIFIERS, createBoneHelmetModifiers())
                            .stacksTo(1)
                            .durability(1843)
            ));
//兜帽
    public static final DeferredItem<DoomHoodItem> DOOM_HOOD = ITEMS.register("doom_hood",
            () -> new DoomHoodItem(
                    ModArmorMaterials.CHAOS_SKELETON,
                    ArmorItem.Type.HELMET,
                    new Item.Properties()
                            .stacksTo(1)
                            .durability(1843)
            ));
    //箭袋
    public static final DeferredItem<TombQuiverItem> TOMB_QUIVER = ITEMS.register("tomb_quiver",
            () -> new TombQuiverItem(new Item.Properties()
                    .stacksTo(1)
                    .durability(2304)
                    .component(
                            (DataComponentType<Integer>) BuiltInRegistries.DATA_COMPONENT_TYPE.get(ResourceLocation.parse("minecraft:enchantable")),
                            15
                    )
                    .component(ModDataComponents.QUIVER_CONTENT.get(), QuiverContent.EMPTY())
                    .component(DataComponents.ATTRIBUTE_MODIFIERS, createQuiverModifiers())
                    .component(DataComponents.LORE, new ItemLore(List.of(
                            Component.translatable("item.remains_cave.offhanditem").withStyle(ChatFormatting.WHITE),
                            Component.translatable("item.remains_cave.tomb_quiver.desc1").withStyle(ChatFormatting.GRAY))))
            ));
    private static ItemAttributeModifiers createQuiverModifiers() {
        ResourceLocation addId = ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "tomb_quiver_arrow_damage_add");
        ResourceLocation multiId = ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "tomb_quiver_arrow_damage_multiplier");
        ResourceLocation toughnessId = ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "tomb_quiver_arrow_toughness");

        AttributeModifier addModifier = new AttributeModifier(addId, 3.0, AttributeModifier.Operation.ADD_VALUE);
        AttributeModifier multiModifier = new AttributeModifier(multiId, 0.03, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL);
        AttributeModifier toughnessMod = new AttributeModifier(toughnessId, 3.0, AttributeModifier.Operation.ADD_VALUE);

        return ItemAttributeModifiers.builder()
                .add(ModAttributes.ARROW_DAMAGE, addModifier, EquipmentSlotGroup.OFFHAND)
                .add(ModAttributes.ARROW_DAMAGE, multiModifier, EquipmentSlotGroup.OFFHAND)
                .add(Attributes.ARMOR_TOUGHNESS, toughnessMod, EquipmentSlotGroup.OFFHAND)
                .build();
    }
    //肩甲
    public static final DeferredItem<BonePauldronItem> BONE_PAULDRON = ITEMS.register("bone_pauldron",
            () -> new BonePauldronItem(new Item.Properties()
                    .stacksTo(1)
                    .durability(2304)
                    .component(
                            (DataComponentType<Integer>) BuiltInRegistries.DATA_COMPONENT_TYPE.get(ResourceLocation.parse("minecraft:enchantable")),
                            15
                    )
                    .component(DataComponents.LORE, new ItemLore(List.of(
                            Component.translatable("item.remains_cave.offhanditem").withStyle(ChatFormatting.WHITE),
                            Component.translatable("item.remains_cave.bone_pauldron.desc1").withStyle(ChatFormatting.RED),
                            Component.translatable("item.remains_cave.bone_pauldron.desc2").withStyle(ChatFormatting.RED)
                    )))
                    .component(DataComponents.ATTRIBUTE_MODIFIERS, createPauldronModifiers()) // 新增属性
            ));
    private static ItemAttributeModifiers createPauldronModifiers() {
        ResourceLocation armorId = ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_pauldron_armor");
        ResourceLocation armorModid = ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_pauldron_armor_mul");
        ResourceLocation toughnessId = ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_pauldron_toughness");

        AttributeModifier armorMod = new AttributeModifier(armorId, 3.0, AttributeModifier.Operation.ADD_VALUE);
        AttributeModifier armorModmul = new AttributeModifier(armorModid, 0.03, AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL);
        AttributeModifier toughnessMod = new AttributeModifier(toughnessId, 3.0, AttributeModifier.Operation.ADD_VALUE);

        return ItemAttributeModifiers.builder()
                .add(Attributes.ARMOR, armorMod, EquipmentSlotGroup.OFFHAND)
                .add(Attributes.ARMOR, armorModmul, EquipmentSlotGroup.OFFHAND)
                .add(Attributes.ARMOR_TOUGHNESS, toughnessMod, EquipmentSlotGroup.OFFHAND)
                .build();
    }



    private static ItemAttributeModifiers createBoneHelmetModifiers() {
        ItemAttributeModifiers.Builder builder = ItemAttributeModifiers.builder();

        // 原有效果：护甲
        builder.add(
                Attributes.ARMOR,
                new AttributeModifier(
                        ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_helmet_armor"),
                        3.5,
                        AttributeModifier.Operation.ADD_VALUE
                ),
                EquipmentSlotGroup.HEAD
        );

        // 原有效果：护甲韧性
        builder.add(
                Attributes.ARMOR_TOUGHNESS,
                new AttributeModifier(
                        ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_helmet_armortoughness"),
                        3.0,
                        AttributeModifier.Operation.ADD_VALUE
                ),
                EquipmentSlotGroup.HEAD
        );

        // 原有效果：击退抗性
        builder.add(
                Attributes.KNOCKBACK_RESISTANCE,
                new AttributeModifier(
                        ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_helmet_knockback"),
                        0.06,
                        AttributeModifier.Operation.ADD_VALUE
                ),
                EquipmentSlotGroup.HEAD
        );

        // ===== 新增：减伤效果（3%） =====
        builder.add(
                ModAttributes.MITIGATION,                // 使用 Salinity 定义的减伤属性
                new AttributeModifier(
                        ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_helmet_mitigation"),
                        0.03,                             // 3% 减伤
                        AttributeModifier.Operation.ADD_MULTIPLIED_TOTAL // 必须使用此操作类型
                ),
                EquipmentSlotGroup.HEAD
        );

        return builder.build();
    }

        private static ItemAttributeModifiers createBoneStatelyModifiers() {
            ItemAttributeModifiers.Builder builder = ItemAttributeModifiers.builder();
            builder.add(
                    Attributes.ARMOR,
                    new AttributeModifier(
                            ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_stately_armor"),
                            3.0,//增加护甲
                            AttributeModifier.Operation.ADD_VALUE
                    ),
                    EquipmentSlotGroup.HEAD // 仅在头盔槽生效
            );
            builder.add(
                    Attributes.ARMOR_TOUGHNESS,
                    new AttributeModifier(
                            ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_stately_armor_toughness"),
                            3.0,//增加护甲韧性
                            AttributeModifier.Operation.ADD_VALUE
                    ),
                    EquipmentSlotGroup.HEAD // 仅在头盔槽生效
            );
            builder.add(
                    Attributes.KNOCKBACK_RESISTANCE,
                    new AttributeModifier(
                            ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_stately_knockback"),
                            0.06,//增加击退抗性
                            AttributeModifier.Operation.ADD_VALUE
                    ),
                    EquipmentSlotGroup.HEAD
            );
        builder.add(
                Attributes.ATTACK_DAMAGE,
                new AttributeModifier(
                        ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_stately_damage_mult"),
                        0.15,//近战伤害
                        AttributeModifier.Operation.ADD_MULTIPLIED_BASE//基于基础伤害的百分比加成
                ),
                EquipmentSlotGroup.HEAD
        );
        return builder.build();
    }
    public static final AttributeModifier BONE_STATELY_SPEED_MODIFIER =
            new AttributeModifier(
                    ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "bone_stately_haste"),
                    0.125, // 12.5% 攻速加成
                    AttributeModifier.Operation.ADD_MULTIPLIED_BASE//基于基础攻速的百分比
            );
    // 战利品方块物品
    public static final DeferredItem<SkeletonArcherTrophyItem> SKELETON_ARCHER_TROPHY_ITEM = ITEMS.register("skeleton_archer_trophy",
            () -> new SkeletonArcherTrophyItem(ModBlocks.SKELETON_ARCHER_TROPHY.get(), new Item.Properties()));
    public static final DeferredItem<SkeletonKingTrophyItem> SKELETON_KING_TROPHY_ITEM = ITEMS.register("skeleton_king_trophy",
            () -> new SkeletonKingTrophyItem(ModBlocks.SKELETON_KING_TROPHY.get(), new Item.Properties()
                    .component(DataComponents.LORE, new ItemLore(List.of(
                            Component.translatable("item.remains_cave.skeleton_king_trophy.desc1").withStyle(ChatFormatting.RED),
                            Component.translatable("item.remains_cave.skeleton_king_trophy.desc2").withStyle(ChatFormatting.RED))))
            ));
    public static final DeferredItem<SkeletonWarlockTrophyItem> SKELETON_WARLOCK_TROPHY_ITEM = ITEMS.register("skeleton_warlock_trophy",
            () -> new SkeletonWarlockTrophyItem(ModBlocks.SKELETON_WARLOCK_TROPHY.get(), new Item.Properties()));
    public static final DeferredItem<SkeletonWarriorTrophyItem> SKELETON_WARRIOR_TROPHY_ITEM = ITEMS.register("skeleton_warrior_trophy",
            () -> new SkeletonWarriorTrophyItem(ModBlocks.SKELETON_WARRIOR_TROPHY.get(), new Item.Properties()));

    public static final DeferredItem<Item> STATELY_KEY = ITEMS.register("stately_key",
            () -> new Item(new Item.Properties()                    .component(DataComponents.LORE, new ItemLore(List.of(
                    Component.translatable("item.remains_cave.stately_key.desc1").withStyle(ChatFormatting.RED),
                    Component.translatable("item.remains_cave.stately_key.desc2").withStyle(ChatFormatting.RED)
            )))
            ));
    public static final DeferredItem<Item> CRIMSON_CRYSTAL = ITEMS.register("crimson_crystal",
            () -> new Item(new Item.Properties()));
    //地牢
    public static final DeferredItem<BlockItem> ROOM_VAULT_ITEM = ITEMS.register("room_vault",
            () -> new BlockItem(ModBlocks.ROOM_VAULT.get(), new Item.Properties()));
    public static final DeferredItem<BlockItem> BOSS_VAULT_ITEM = ITEMS.register("boss_vault",
            () -> new BlockItem(ModBlocks.BOSS_VAULT.get(), new Item.Properties()));
    // 在 ModItems.java 中修改
    public static final DeferredItem<Item> TOMB_DOOR = ITEMS.register("tomb_door",
            () -> new BlockItem(ModBlocks.TOMB_DOOR_CORE.get(), new Item.Properties()));
    //骷髅王_块
    public static final DeferredItem<BlockItem> SKELETON_KING_SUMMON = ITEMS.register("skeleton_king_summon",
            () -> new BlockItem(ModBlocks.SKELETON_KING_SUMMON.get(), new Item.Properties()));
    public static final DeferredItem<BlockItem> SKELETON_KING_REMAINS = ITEMS.register("skeleton_king_remains",
            () -> new BlockItem(ModBlocks.SKELETON_KING_REMAINS.get(), new Item.Properties()));
    //挑战
    public static final DeferredItem<BlockItem> CHALLENGE_ALTAR_ITEM = ITEMS.register("challenge_altar",
            () -> new BlockItem(ModBlocks.CHALLENGE_ALTAR.get(), new Item.Properties()));

    public static final DeferredItem<BlockItem> CHALLENGE_CAGE_ITEM = ITEMS.register("challenge_cage",
            () -> new BlockItem(ModBlocks.CHALLENGE_CAGE.get(), new Item.Properties()));
    public static final DeferredItem<BlockItem> EMBLEM_BLOCK_ITEM = ITEMS.register("emblem_block",
            () -> new BlockItem(ModBlocks.EMBLEM_BLOCK.get(), new Item.Properties()));
}