package asm.mixins;

import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.SplashOverlay;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.VertexFormat.DrawMode;
import net.minecraft.util.Identifier;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Environment(EnvType.CLIENT)
@Mixin({SplashOverlay.class})
public class MixinSplashOverlay {
   private static final int CRUCIFIX_BLACK_BACKGROUND = -16777216;
   private static final Identifier CRUCIFIX_LOGO = Identifier.of("crucifixbeta", "textures/gui/crucifix_logo.png");
   private static final float LOGO_TEXTURE_WIDTH = 2048.0F;
   private static final float LOGO_TEXTURE_HEIGHT = 779.0F;

   @Inject(
      method = {"method_35733"},
      at = {@At("RETURN")},
      cancellable = true
   )
   private static void forceBlackBrandBackground(CallbackInfoReturnable<Integer> cir) {
      cir.setReturnValue(-16777216);
   }

   @Inject(
      method = {"render"},
      at = {@At("TAIL")}
   )
   private void renderCrucifixLogo(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
      int var6 = context.getScaledWindowWidth();
      int var7 = context.getScaledWindowHeight();
      if (var6 > 0 && var7 > 0) {
         float var8 = clamp((float)var6 * 0.58F, Math.min(220.0F, (float)var6 - 24.0F), Math.min(520.0F, (float)var6 - 32.0F));
         float var9 = var8 * 779.0F / 2048.0F;
         float var10 = ((float)var6 - var8) * 0.5F;
         float var11 = (float)var7 * 0.5F - var9 * 0.5F - (float)var7 * 0.045F;
         context.fill(Math.round(var10 - 20.0F), Math.round(var11 - 16.0F), Math.round(var10 + var8 + 20.0F), Math.round(var11 + var9 + 16.0F), -16777216);
         drawTexture(context, CRUCIFIX_LOGO, var10, var11, var8, var9, 1.0F);
      }
   }

   private static void drawTexture(DrawContext context, Identifier texture, float x, float y, float width, float height, float alpha) {
      int var7 = Math.round(clamp(alpha, 0.0F, 1.0F) * 255.0F);
      if (var7 > 0 && !(width <= 0.0F) && !(height <= 0.0F)) {
         RenderSystem.enableBlend();
         RenderSystem.defaultBlendFunc();
         RenderSystem.setShader(GameRenderer::getPositionTexColorProgram);
         RenderSystem.setShaderTexture(0, texture);
         Matrix4f var8 = context.getMatrices().peek().getPositionMatrix();
         BufferBuilder var9 = Tessellator.getInstance().begin(DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR);
         var9.vertex(var8, x, y + height, 0.0F).texture(0.0F, 1.0F).color(255, 255, 255, var7);
         var9.vertex(var8, x + width, y + height, 0.0F).texture(1.0F, 1.0F).color(255, 255, 255, var7);
         var9.vertex(var8, x + width, y, 0.0F).texture(1.0F, 0.0F).color(255, 255, 255, var7);
         var9.vertex(var8, x, y, 0.0F).texture(0.0F, 0.0F).color(255, 255, 255, var7);
         BufferRenderer.drawWithGlobalProgram(var9.end());
         RenderSystem.disableBlend();
      }
   }

   private static float clamp(float value, float min, float max) {
      return Math.max(min, Math.min(max, value));
   }
}
