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: choking effect for data-driven items.
 * When active, drains air, applies darkness/slowness, deals damage.
 *
 * JSON config:
 * {@code "choking": {"air_drain_per_tick": 8, "non_lethal_for_master": true}}
 */
public class ChokingComponent implements IItemComponent {

    private final int airDrainPerTick;
    private final boolean nonLethalForMaster;

    private ChokingComponent(int airDrainPerTick, boolean nonLethalForMaster) {
        this.airDrainPerTick = airDrainPerTick;
        this.nonLethalForMaster = nonLethalForMaster;
    }

    public static IItemComponent fromJson(JsonObject config) {
        int drain = 8;
        boolean nonLethal = true;
        if (config != null) {
            if (config.has("air_drain_per_tick")) {
                drain = config.get("air_drain_per_tick").getAsInt();
            }
            if (config.has("non_lethal_for_master")) {
                nonLethal = config.get("non_lethal_for_master").getAsBoolean();
            }
        }
        drain = Math.max(1, drain);
        return new ChokingComponent(drain, nonLethal);
    }

    /** Air drained per tick (net after vanilla +4 restoration). */
    public int getAirDrainPerTick() {
        return airDrainPerTick;
    }

    /** Whether the choke is non-lethal when used by a master on their pet. */
    public boolean isNonLethalForMaster() {
        return nonLethalForMaster;
    }

    @Override
    public void appendTooltip(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
        tooltip.add(Component.translatable("item.tiedup.tooltip.choking").withStyle(ChatFormatting.DARK_PURPLE));
    }
}
