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: GPS tracking and safe zone for data-driven items.
 *
 * JSON config:
 * {@code "gps": {"safe_zone_radius": 50, "public_tracking": false}}
 */
public class GpsComponent implements IItemComponent {

    private final int safeZoneRadius;
    private final boolean publicTracking;

    private GpsComponent(int safeZoneRadius, boolean publicTracking) {
        this.safeZoneRadius = safeZoneRadius;
        this.publicTracking = publicTracking;
    }

    public static IItemComponent fromJson(JsonObject config) {
        int radius = 50;
        boolean publicTracking = false;
        if (config != null) {
            if (config.has("safe_zone_radius")) {
                radius = config.get("safe_zone_radius").getAsInt();
            }
            if (config.has("public_tracking")) {
                publicTracking = config.get("public_tracking").getAsBoolean();
            }
        }
        radius = Math.max(0, radius);
        return new GpsComponent(radius, publicTracking);
    }

    /** Safe zone radius in blocks. 0 = no safe zone (tracking only). */
    public int getSafeZoneRadius() {
        return safeZoneRadius;
    }

    /** Whether any player can track (not just the owner). */
    public boolean isPublicTracking() {
        return publicTracking;
    }

    @Override
    public void appendTooltip(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
        tooltip.add(Component.translatable("item.tiedup.tooltip.gps_tracking")
            .withStyle(ChatFormatting.AQUA));
        if (safeZoneRadius > 0) {
            tooltip.add(Component.translatable("item.tiedup.tooltip.gps_zone_radius", safeZoneRadius)
                .withStyle(ChatFormatting.DARK_AQUA));
        }
    }
}
