package dev.crucifixbeta.mod.gui.acrylic;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.ptr.IntByReference;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWNativeWin32;
import org.lwjgl.system.Platform;

@Environment(EnvType.CLIENT)
public final class AcrylicRuntime {
   private static boolean initialized;
   private static boolean transparencyEnabled;
   private static boolean transparencyFailed;
   private static long hwnd;

   private AcrylicRuntime() {
   }

   public static void applyWindowHints() {
      GLFW.glfwWindowHint(131082, 1);
   }

   public static void init(long glfwWindow) {
      if (!initialized) {
         initialized = true;
         transparencyEnabled = GLFW.glfwGetWindowAttrib(glfwWindow, 131082) == 1;
         transparencyFailed = !transparencyEnabled;
         if (Platform.get() == Platform.WINDOWS) {
            try {
               hwnd = GLFWNativeWin32.glfwGetWin32Window(glfwWindow);
               if (hwnd != 0L && AcrylicRuntime.NtDll.isWindows11OrNewer()) {
                  AcrylicRuntime.DwmApi.setBool(hwnd, 20, true);
                  AcrylicRuntime.DwmApi.setInt(hwnd, 38, 3);
                  AcrylicRuntime.DwmApi.setInt(hwnd, 33, 2);
                  AcrylicRuntime.DwmApi.setInt(hwnd, 34, -2);
               }
            } catch (Throwable var3) {
               transparencyFailed = true;
               var3.printStackTrace();
            }
         }
      }
   }

   public static boolean isTransparencyEnabled() {
      return transparencyEnabled;
   }

   public static boolean isTransparencyFailed() {
      return transparencyFailed;
   }

   public static long getHwnd() {
      return hwnd;
   }

   @Environment(EnvType.CLIENT)
   private interface DwmApi extends Library {
      AcrylicRuntime.DwmApi INSTANCE = Native.load("dwmapi", AcrylicRuntime.DwmApi.class);

      int DwmSetWindowAttribute(HWND hWND, int i, PointerType pointerType, int j);

      static void setBool(long handle, int attribute, boolean value) {
         INSTANCE.DwmSetWindowAttribute(new HWND(Pointer.createConstant(handle)), attribute, new IntByReference(value ? 1 : 0), 4);
      }

      static void setInt(long handle, int attribute, int value) {
         INSTANCE.DwmSetWindowAttribute(new HWND(Pointer.createConstant(handle)), attribute, new IntByReference(value), 4);
      }
   }

   @Environment(EnvType.CLIENT)
   private interface NtDll extends Library {
      AcrylicRuntime.NtDll INSTANCE = Native.load("ntdll", AcrylicRuntime.NtDll.class);

      void RtlGetNtVersionNumbers(IntByReference intByReference, IntByReference intByReference2, IntByReference intByReference3);

      static boolean isWindows11OrNewer() {
         try {
            IntByReference var0 = new IntByReference();
            IntByReference var1 = new IntByReference();
            INSTANCE.RtlGetNtVersionNumbers(var0, new IntByReference(), var1);
            return var0.getValue() >= 10 && (var1.getValue() & 268435455) >= 22621;
         } catch (Throwable var2) {
            return false;
         }
      }
   }
}
