package wtf.opal.utility.misc.chat;

import java.util.EnumSet;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import wtf.opal.client.Constants;
import wtf.opal.client.ReleaseInfo;

@Environment(EnvType.CLIENT)
public final class ChatUtility {

   private ChatUtility() {
   }

   public static void debug(Object o) {
      if (ReleaseInfo.CHANNEL == ReleaseInfo.ReleaseChannel.DEVELOPMENT) {
         MutableText var1 = Text.literal("[")
            .formatted(Formatting.GRAY)
            .append(Text.literal("\u27a2").formatted(Formatting.GREEN, Formatting.BOLD))
            .append(Text.literal("] ").formatted(Formatting.GRAY))
            .append(o.toString());
         display(var1);
      }
   }

   public static void print(Object o) {
      MutableText var1 = Text.literal("[")
         .formatted(Formatting.GRAY)
         .append(Text.literal("\u2139").formatted(Formatting.AQUA))
         .append(Text.literal("] ").formatted(Formatting.GRAY))
         .append(o.toString());
      display(var1);
   }

   public static void error(Object o) {
      MutableText var1 = Text.literal("[")
         .formatted(Formatting.GRAY)
         .append(Text.literal("\u2716").formatted(Formatting.RED))
         .append(Text.literal("] ").formatted(Formatting.GRAY))
         .append(o.toString());
      display(var1);
   }

   public static void success(Object o) {
      MutableText var1 = Text.literal("[")
         .formatted(Formatting.GRAY)
         .append(Text.literal("\u2714").formatted(Formatting.GREEN))
         .append(Text.literal("] ").formatted(Formatting.GRAY))
         .append(o.toString());
      display(var1);
   }

   public static void display(Text text) {
      if (Constants.mc.player != null) {
         Constants.mc.inGameHud.getChatHud().addMessage(text);
      }
   }

   public static void send(String content) {
      if (Constants.mc.player != null) {
         Constants.mc.player.networkHandler.sendChatMessage(content);
      }
   }

   public static void sendCommand(String command) {
      if (Constants.mc.player != null) {
         Constants.mc.player.networkHandler.sendChatCommand(command);
      }
   }

   public static MutableText translateAlternateColorCodes(String str) {
      MutableText var1 = Text.empty();
      char[] var2 = str.toCharArray();
      EnumSet<Formatting> var3 = EnumSet.noneOf(Formatting.class);

      for (int var4 = 0; var4 < var2.length; var4++) {
         char var5 = var2[var4];
         if (var5 == '&' && var4 + 1 < var2.length) {
            char var6 = var2[var4 + 1];
            Formatting var7 = Formatting.byCode(var6);
            if (var7 != null) {
               var4++;
               if (var7 == Formatting.RESET) {
                  var3.clear();
               } else {
                  var3.add(var7);
               }
               continue;
            }
         }

         Style var9 = Style.EMPTY;

         for (Formatting var8 : var3) {
            var9 = var9.withFormatting(var8);
         }

         var1.append(Text.literal(String.valueOf(var5)).setStyle(var9));
      }

      return var1;
   }
}
