package asm.mixins;

import dev.crucifixbeta.api.utils.render.GlobalFontRenderer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.font.TextRenderer.TextLayerType;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.text.OrderedText;
import net.minecraft.text.StringVisitable;
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Environment(EnvType.CLIENT)
@Mixin({TextRenderer.class})
public class MixinTextRenderer {
   @Inject(
      method = {"draw(Ljava/lang/String;FFIZLorg/joml/Matrix4f;Lnet/minecraft/client/render/VertexConsumerProvider;Lnet/minecraft/client/font/TextRenderer$TextLayerType;IIZ)I"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void crucifix$drawStringWithClientFont(
      String text,
      float x,
      float y,
      int color,
      boolean shadow,
      Matrix4f matrix,
      VertexConsumerProvider vertexConsumers,
      TextLayerType layerType,
      int backgroundColor,
      int light,
      boolean rightToLeft,
      CallbackInfoReturnable<Integer> cir
   ) {
      if (GlobalFontRenderer.shouldReplaceDraw(vertexConsumers, layerType, backgroundColor, light)) {
         GlobalFontRenderer.flush(vertexConsumers);
         String var13 = rightToLeft ? ((TextRenderer)(Object)this).mirror(text) : text;
         cir.setReturnValue(GlobalFontRenderer.drawString(var13, x, y, color, shadow, matrix));
      }
   }

   @Inject(
      method = {"draw(Lnet/minecraft/text/OrderedText;FFIZLorg/joml/Matrix4f;Lnet/minecraft/client/render/VertexConsumerProvider;Lnet/minecraft/client/font/TextRenderer$TextLayerType;II)I"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void crucifix$drawOrderedWithClientFont(
      OrderedText text,
      float x,
      float y,
      int color,
      boolean shadow,
      Matrix4f matrix,
      VertexConsumerProvider vertexConsumers,
      TextLayerType layerType,
      int backgroundColor,
      int light,
      CallbackInfoReturnable<Integer> cir
   ) {
      if (GlobalFontRenderer.shouldReplaceDraw(vertexConsumers, layerType, backgroundColor, light)) {
         GlobalFontRenderer.flush(vertexConsumers);
         cir.setReturnValue(GlobalFontRenderer.drawOrderedText(text, x, y, color, shadow, matrix));
      }
   }

   @Inject(
      method = {"getWidth(Ljava/lang/String;)I"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void crucifix$getStringWidthWithClientFont(String text, CallbackInfoReturnable<Integer> cir) {
      if (GlobalFontRenderer.shouldReplace()) {
         cir.setReturnValue(GlobalFontRenderer.widthString(text));
      }
   }

   @Inject(
      method = {"getWidth(Lnet/minecraft/text/OrderedText;)I"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void crucifix$getOrderedWidthWithClientFont(OrderedText text, CallbackInfoReturnable<Integer> cir) {
      if (GlobalFontRenderer.shouldReplace()) {
         cir.setReturnValue(GlobalFontRenderer.widthOrderedText(text));
      }
   }

   @Inject(
      method = {"getWidth(Lnet/minecraft/text/StringVisitable;)I"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void crucifix$getVisitableWidthWithClientFont(StringVisitable text, CallbackInfoReturnable<Integer> cir) {
      if (GlobalFontRenderer.shouldReplace()) {
         cir.setReturnValue(GlobalFontRenderer.widthStringVisitable(text));
      }
   }
}
