package asm.mixins;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.impl.PreRender2DEvent;
import dev.crucifixbeta.mod.modules.impl.client.ClientSetting;
import dev.crucifixbeta.mod.modules.impl.render.Crosshair;
import dev.crucifixbeta.mod.modules.impl.render.NoRender;
import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.render.RenderTickCounter;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Environment(EnvType.CLIENT)
@Mixin({InGameHud.class})
public abstract class MixinInGameHud {
   @Final
   @Shadow
   private MinecraftClient client;
   @Unique
   final Color minColor = new Color(196, 0, 0);
   @Unique
   final Color maxColor = new Color(0, 227, 0);

   @Inject(
      method = {"renderPortalOverlay"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void onRenderPortalOverlay(DrawContext context, float nauseaStrength, CallbackInfo ci) {
      if (NoRender.INSTANCE.isOn() && NoRender.INSTANCE.portal.getValue()) {
         ci.cancel();
      }
   }

   @Inject(
      method = {"renderStatusEffectOverlay"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void onRenderStatusEffectOverlay(CallbackInfo info) {
      if (NoRender.INSTANCE.isOn() && NoRender.INSTANCE.potionsIcon.getValue()) {
         info.cancel();
      }
   }

   @Inject(
      method = {"renderArmor"},
      at = {@At("HEAD")},
      cancellable = true,
      require = 0
   )
   private static void cancelArmorHud(DrawContext context, PlayerEntity player, int i, int j, int x, int y, CallbackInfo ci) {
      if (NoRender.INSTANCE.isOn() && NoRender.INSTANCE.armorHud.getValue()) {
         ci.cancel();
      }
   }

   @Shadow
   private int getHeartRows(int t) {
      return 0;
   }

   @Shadow
   private int getHeartCount(LivingEntity livingEntity) {
      return 0;
   }

   @Shadow
   private PlayerEntity getCameraPlayer() {
      return null;
   }

   @Shadow
   private LivingEntity getRiddenEntity() {
      return null;
   }

   @Shadow
   public abstract void render(DrawContext drawContext, RenderTickCounter renderTickCounter);

   @Inject(
      at = {@At("HEAD")},
      method = {"render"}
   )
   public void renderStart(DrawContext context, RenderTickCounter tickCounter, CallbackInfo ci) {
      CrucifixBeta.EVENT_BUS.post(PreRender2DEvent.get(context));
   }

   @Inject(
      at = {@At("TAIL")},
      method = {"render"}
   )
   public void renderHook(DrawContext context, RenderTickCounter tickCounter, CallbackInfo ci) {
      CrucifixBeta.MODULE.onRender2D(context);
   }

   @Inject(
      method = {"clear"},
      at = {@At(
         value = "INVOKE",
         target = "Lnet/minecraft/client/gui/hud/ChatHud;clear(Z)V"
      )},
      cancellable = true
   )
   private void onClear(CallbackInfo info) {
      if (ClientSetting.INSTANCE.isOn() && ClientSetting.INSTANCE.keepHistory.getValue()) {
         info.cancel();
      }
   }

   @ModifyArg(
      method = {"renderHotbar"},
      at = @At(
         value = "INVOKE",
         target = "Lnet/minecraft/client/gui/DrawContext;drawGuiTexture(Lnet/minecraft/util/Identifier;IIII)V",
         ordinal = 1
      ),
      index = 1
   )
   private int selectedSlotX(int x) {
      if (ClientSetting.INSTANCE.hotbar()) {
         double var2 = ClientSetting.animation
            .get((double)x, (long)ClientSetting.INSTANCE.hotbarTime.getValueInt(), ClientSetting.INSTANCE.animEase.getValue());
         return (int)var2;
      } else {
         return x;
      }
   }

   @Inject(
      method = {"renderCrosshair"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void onRenderCrosshairBegin(DrawContext context, RenderTickCounter tickCounter, CallbackInfo ci) {
      if (Crosshair.INSTANCE.isOn()) {
         Crosshair.INSTANCE.draw(context);
         ci.cancel();
      }
   }
}
