package wtf.opal.client.screen.click.dropdown;

import dev.crucifixbeta.mod.modules.impl.client.ClientSetting;
import java.io.IOException;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gl.PostEffectProcessor;
import net.minecraft.client.gl.WindowFramebuffer;
import net.minecraft.util.Identifier;
import wtf.opal.client.Constants;
import wtf.opal.client.renderer.NVGRenderer;

@Environment(EnvType.CLIENT)
public final class ClickGuiBlur {
   private static final Identifier BLUR_IDENTIFIER;
   private static final float BLUR_RADIUS;
   private static Framebuffer blurFramebuffer;
   private static PostEffectProcessor blurProcessor;
   private static int framebufferWidth;
   private static int framebufferHeight;
   private static long lastBlurUpdate;
   private static final long BLUR_UPDATE_INTERVAL_MS;

   private ClickGuiBlur() {
   }

   public static void update(float tickDelta) {
      if (!isEnabled()) {
         release();
      } else if (Constants.mc.getFramebuffer() != null && Constants.mc.getWindow() != null) {
         int var1 = Constants.mc.getWindow().getFramebufferWidth();
         int var2 = Constants.mc.getWindow().getFramebufferHeight();
         if (blurFramebuffer == null || var1 != framebufferWidth || var2 != framebufferHeight) {
            resize(var1, var2);
         }

         if (blurFramebuffer != null) {
            long var3 = System.currentTimeMillis();
            if (var3 - lastBlurUpdate >= 33L) {
               lastBlurUpdate = var3;

               try {
                  blurFramebuffer.beginWrite(true);
                  Constants.mc.getFramebuffer().draw(var1, var2, false);
                  if (blurProcessor == null) {
                     blurProcessor = new PostEffectProcessor(
                        Constants.mc.getTextureManager(), Constants.mc.getResourceManager(), blurFramebuffer, BLUR_IDENTIFIER
                     );
                     blurProcessor.setupDimensions(var1, var2);
                  }

                  blurProcessor.setUniforms("Radius", getRadius());
                  blurProcessor.render(tickDelta);
                  Constants.mc.getFramebuffer().beginWrite(true);
               } catch (RuntimeException | IOException var6) {
                  Constants.mc.getFramebuffer().beginWrite(true);
                  blurProcessor = null;
               }
            }
         }
      }
   }

   private static void resize(int width, int height) {
      NVGRenderer.deleteBlurPaintImage();
      if (blurFramebuffer != null) {
         blurFramebuffer.delete();
      }

      blurProcessor = null;
      blurFramebuffer = new WindowFramebuffer(width, height);
      framebufferWidth = width;
      framebufferHeight = height;
      lastBlurUpdate = 0L;
      blurFramebuffer.setClearColor(0.0F, 0.0F, 0.0F, 0.0F);
      blurFramebuffer.clear(false);
      NVGRenderer.createNVGPaintFromTex(width, height, blurFramebuffer.getColorAttachment(), NVGRenderer.BLUR_PAINT);
   }

   public static void release() {
      NVGRenderer.deleteBlurPaintImage();
      if (blurProcessor != null) {
         blurProcessor.close();
         blurProcessor = null;
      }

      if (blurFramebuffer != null) {
         blurFramebuffer.delete();
         blurFramebuffer = null;
      }

      framebufferWidth = 0;
      framebufferHeight = 0;
      lastBlurUpdate = 0L;
   }

   public static boolean isEnabled() {
      return ClientSetting.INSTANCE != null && ClientSetting.INSTANCE.guiGlass.getValue();
   }

   public static float getRadius() {
      return ClientSetting.INSTANCE == null ? 38.0F : ClientSetting.INSTANCE.guiGlassBlur.getValueFloat();
   }

   static {

      BLUR_UPDATE_INTERVAL_MS = 33L;
      BLUR_RADIUS = 28.0F;
      BLUR_IDENTIFIER = Identifier.ofVanilla("shaders/post/blur.json");
   }
}
