package com.tiedup.remake.v2.bondage.component;

import com.google.gson.JsonObject;
import java.util.List;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

/**
 * Component: lockable behavior for data-driven items.
 *
 * <p>Stores the per-item lock resistance parsed from JSON. The lock check
 * itself is NOT done here because {@code AbstractV2BondageItem.canUnequip()}
 * already delegates to {@code ILockable.isLocked()} -- duplicating it in
 * {@code blocksUnequip()} would be redundant.</p>
 *
 * <p>Consumers retrieve the per-item lock resistance via
 * {@link com.tiedup.remake.v2.bondage.datadriven.DataDrivenBondageItem#getItemLockResistance(net.minecraft.world.item.ItemStack)}.</p>
 *
 * JSON config:
 * {@code "lockable": {}} or {@code "lockable": {"lock_resistance": 300}}
 */
public class LockableComponent implements IItemComponent {

    private final int lockResistance;

    private LockableComponent(int lockResistance) {
        this.lockResistance = lockResistance;
    }

    public static IItemComponent fromJson(JsonObject config) {
        int resistance = 250; // default
        if (config != null && config.has("lock_resistance")) {
            resistance = config.get("lock_resistance").getAsInt();
        }
        resistance = Math.max(0, resistance);
        return new LockableComponent(resistance);
    }

    /**
     * Get the per-item lock resistance value parsed from JSON.
     *
     * @return lock resistance (always >= 0 after RISK-003 clamping)
     */
    public int getLockResistance() {
        return lockResistance;
    }

    @Override
    public void appendTooltip(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
        // Lock status ("Locked"/"Lockable") is handled by ILockable.appendLockTooltip()
        // called from AbstractV2BondageItem.appendHoverText() — no duplicate here.
        if (flag.isAdvanced()) {
            tooltip.add(Component.translatable("item.tiedup.tooltip.lock_resistance", lockResistance)
                .withStyle(ChatFormatting.DARK_GRAY));
        }
    }
}
