package com.tiedup.remake.items;

import com.tiedup.remake.core.TiedUpMod;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;

/**
 * TiedUp! Guide Book Item
 *
 * When used, gives the player the Patchouli guide_book item with the correct NBT.
 * If Patchouli is not installed, displays a message.
 */
public class ItemTiedUpGuide extends Item {

    public ItemTiedUpGuide() {
        super(new Item.Properties().stacksTo(1));
    }

    @Override
    public @NotNull InteractionResultHolder<ItemStack> use(
        @NotNull Level level,
        @NotNull Player player,
        @NotNull InteractionHand hand
    ) {
        ItemStack stack = player.getItemInHand(hand);

        if (!level.isClientSide()) {
            // Check if Patchouli is installed
            if (!ModList.get().isLoaded("patchouli")) {
                player.displayClientMessage(
                    Component.translatable("item.tiedup.guide.no_patchouli")
                        .withStyle(ChatFormatting.RED),
                    false
                );
                return InteractionResultHolder.fail(stack);
            }

            // Get the Patchouli guide_book item
            Item guideBookItem = ForgeRegistries.ITEMS.getValue(
                ResourceLocation.fromNamespaceAndPath("patchouli", "guide_book")
            );
            if (guideBookItem == null) {
                player.displayClientMessage(
                    Component.translatable("item.tiedup.guide.book_not_found")
                        .withStyle(ChatFormatting.RED),
                    false
                );
                return InteractionResultHolder.fail(stack);
            }

            // Create the guide book with NBT pointing to our book
            ItemStack guideBook = new ItemStack(guideBookItem);
            CompoundTag nbt = new CompoundTag();
            nbt.putString("patchouli:book", TiedUpMod.MOD_ID + ":guide");
            guideBook.setTag(nbt);

            // Give the player the guide book
            if (!player.getInventory().add(guideBook)) {
                // Drop if inventory is full
                player.drop(guideBook, false);
            }

            // Consume this item
            stack.shrink(1);

            player.displayClientMessage(
                Component.translatable("item.tiedup.guide.received")
                    .withStyle(ChatFormatting.GREEN),
                true
            );
        }

        return InteractionResultHolder.consume(stack);
    }
}
