package com.dairymoose.modernlife.items;

import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
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.minecraft.world.phys.Vec3;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/items/SpeedometerItem.class */
public class SpeedometerItem extends Item {
    private int tickUpdatePeriod;
    public Vec3 prev;
    public Long timestamp;
    public float totalDist;
    public static final boolean GET_SPEED_FROM_DELTA_MOVEMENT = false;
    private static final Logger LOGGER = LogManager.getLogger();
    public static float speed = 0.0f;
    public static boolean horizontalSpeedometer = true;

    public SpeedometerItem(Properties p_i48530_4_) {
        super(p_i48530_4_);
        this.tickUpdatePeriod = 10;
        this.prev = null;
        this.timestamp = null;
        this.totalDist = 0.0f;
    }

    public void onUsingTick(ItemStack itemStack, LivingEntity livingEntity, int count) {
        if (!livingEntity.level.isClientSide || livingEntity != Minecraft.getInstance().player) {
            return;
        }
        Vec3 pos = livingEntity.position();
        if (horizontalSpeedometer) {
            pos = new Vec3(pos.x, 0.0d, pos.z);
        }
        if (this.prev != null) {
            this.totalDist += (float) pos.distanceTo(this.prev);
        }
        if (count % this.tickUpdatePeriod == 0) {
            if (this.prev != null && this.timestamp != null) {
                float d = this.totalDist;
                long currentTime = System.currentTimeMillis();
                float t = ((float) (currentTime - this.timestamp.longValue())) / 1000.0f;
                speed = d / t;
                this.totalDist = 0.0f;
            }
            this.timestamp = Long.valueOf(System.currentTimeMillis());
        }
        this.prev = livingEntity.position();
        if (horizontalSpeedometer) {
            this.prev = new Vec3(this.prev.x, 0.0d, this.prev.z);
        }
    }

    public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand p_77659_3_) {
        ItemStack itemStack = player.getItemInHand(p_77659_3_);
        if (world.isClientSide) {
            horizontalSpeedometer = !horizontalSpeedometer;
            this.totalDist = 0.0f;
            this.prev = null;
            speed = 0.0f;
            this.timestamp = null;
            if (horizontalSpeedometer) {
                Minecraft.getInstance().gui.getChat().addMessage(new TextComponent("Measure horizontal speed only"));
            } else {
                Minecraft.getInstance().gui.getChat().addMessage(new TextComponent("Measure horizontal+vertical speed"));
            }
        }
        return InteractionResultHolder.consume(itemStack);
    }
}
