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: shock collar behavior for data-driven items.
 *
 * JSON config:
 * {@code "shock": {"damage": 2.0, "auto_interval": 0}}
 * auto_interval: ticks between auto-shocks (0 = no auto-shock, manual only)
 */
public class ShockComponent implements IItemComponent {

    private final float damage;
    private final int autoInterval; // 0 = manual only

    private ShockComponent(float damage, int autoInterval) {
        this.damage = damage;
        this.autoInterval = autoInterval;
    }

    public static IItemComponent fromJson(JsonObject config) {
        float damage = 2.0f;
        int autoInterval = 0;
        if (config != null) {
            if (config.has("damage")) {
                damage = config.get("damage").getAsFloat();
            }
            if (config.has("auto_interval")) {
                autoInterval = config.get("auto_interval").getAsInt();
            }
        }
        damage = Math.max(0.0f, damage);
        autoInterval = Math.max(0, autoInterval);
        return new ShockComponent(damage, autoInterval);
    }

    /** Damage dealt per shock. */
    public float getDamage() {
        return damage;
    }

    /** Ticks between auto-shocks. 0 = manual shock only (via controller). */
    public int getAutoInterval() {
        return autoInterval;
    }

    /** Whether this item has auto-shock capability. */
    public boolean hasAutoShock() {
        return autoInterval > 0;
    }

    @Override
    public void appendTooltip(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
        if (hasAutoShock()) {
            float seconds = autoInterval / 20.0f;
            tooltip.add(Component.translatable("item.tiedup.tooltip.shock_auto", String.format("%.1f", seconds))
                .withStyle(ChatFormatting.DARK_RED));
        } else {
            tooltip.add(Component.translatable("item.tiedup.tooltip.shock_manual")
                .withStyle(ChatFormatting.DARK_RED));
        }
    }
}
