package asm.mixins;

import dev.crucifixbeta.mod.modules.impl.render.NoRender;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.light.LightingProvider;
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({LightingProvider.class})
public class MixinLightingProvider {
   private boolean shouldBlockLightUpdates() {
      if (NoRender.INSTANCE == null) {
         return false;
      } else if (NoRender.INSTANCE.isOn() && NoRender.INSTANCE.lightsUpdate.getValue()) {
         MinecraftClient var1 = MinecraftClient.getInstance();
         return var1 != null && var1.isOnThread();
      } else {
         return false;
      }
   }

   @Inject(
      at = {@At("HEAD")},
      method = {"checkBlock"},
      cancellable = true
   )
   public void checkBlock(BlockPos pos, CallbackInfo ci) {
      if (this.shouldBlockLightUpdates()) {
         ci.cancel();
      }
   }

   @Inject(
      at = {@At("HEAD")},
      method = {"doLightUpdates"},
      cancellable = true
   )
   public void doLightUpdates(CallbackInfoReturnable<Integer> cir) {
      if (this.shouldBlockLightUpdates()) {
         cir.setReturnValue(0);
         cir.cancel();
      }
   }
}
