package dev.crucifixbeta.mod.commands.impl;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.PacketEvent;
import dev.crucifixbeta.mod.commands.Command;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.GameMessageS2CPacket;

@Environment(EnvType.CLIENT)
public class PingCommand extends Command {
   private long sendTime;

   public PingCommand() {
      super("ping", "");
   }

   @Override
   public void runCommand(String[] parameters) {
      this.sendTime = System.currentTimeMillis();
      mc.getNetworkHandler().sendChatCommand("chat ");
      CrucifixBeta.EVENT_BUS.subscribe(this);
   }

   @Override
   public String[] getAutocorrect(int count, List<String> seperated) {
      return null;
   }

   @EventListener
   public void onPacketReceive(PacketEvent.Receive e) {
      Packet var2 = e.getPacket();
      GameMessageS2CPacket var3;
      if (var2 instanceof GameMessageS2CPacket
         && (
            (var3 = (GameMessageS2CPacket)var2).content().getString().contains("chat.use")
               || var3.content().getString().contains("\u547d\u4ee4")
               || var3.content().getString().contains("Bad command")
               || var3.content().getString().contains("No such command")
               || var3.content().getString().contains("<--[HERE]")
               || var3.content().getString().contains("Unknown")
               || var3.content().getString().contains("\u5e2e\u52a9")
               || var3.content().getString().contains("\u6267\u884c\u9519\u8bef")
         )) {
         this.sendChatMessage("ping: " + (System.currentTimeMillis() - this.sendTime) + "ms");
         CrucifixBeta.EVENT_BUS.unsubscribe(this);
      }
   }
}
