package wtf.opal.utility.render;

import com.ibm.icu.impl.Pair;
import dev.crucifixbeta.mod.modules.impl.client.ClickGui;
import dev.crucifixbeta.mod.modules.impl.client.ColorsModule;
import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import wtf.opal.utility.misc.math.MathUtility;

@Environment(EnvType.CLIENT)
public final class ColorUtility {
   public static final int MUTED_COLOR = -8355712;
   public static final int THEME_COLOR = -2076417;
   public static final int THEME_SECOND_COLOR = -37930;
   private static long cachedThemeFrame = Long.MIN_VALUE;
   private static Pair<Integer, Integer> cachedClientTheme = Pair.of(-2076417, -37930);

   private ColorUtility() {
   }

   public static Pair<Integer, Integer> getClientTheme() {
      long var0 = System.currentTimeMillis() / 16L;
      if (var0 != cachedThemeFrame) {
         cachedThemeFrame = var0;
         cachedClientTheme = Pair.of(getThemeColor(), getThemeSecondColor());
      }

      return cachedClientTheme;
   }

   public static int getThemeColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      if (var0 != null && var0.colorMode.getValue() == ColorsModule.ColorMode.Static) {
         return var0.accent.getValue().getRGB();
      } else {
         ClickGui var1 = ClickGui.getInstance();
         if (var1 != null) {
            return var1.getActiveColor(0.0).getRGB();
         } else {
            return var0 == null ? -2076417 : var0.accent.getValue().getRGB();
         }
      }
   }

   public static int getThemeSecondColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      if (var0 != null && var0.colorMode.getValue() == ColorsModule.ColorMode.Static) {
         return var0.accent.getValue().getRGB();
      } else {
         ClickGui var1 = ClickGui.getInstance();
         if (var1 != null) {
            return var1.getActiveColor(48.0).getRGB();
         } else {
            return var0 == null ? -37930 : var0.moduleBgOn.getValue().getRGB();
         }
      }
   }

   public static int getPanelBackgroundColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -15790321 : var0.panelBg.getValue().getRGB();
   }

   public static int getHeaderBackgroundColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -15790321 : var0.headerBg.getValue().getRGB();
   }

   public static int getModuleBackgroundColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -14803411 : var0.moduleBg.getValue().getRGB();
   }

   public static int getModuleEnabledBackgroundColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -2076417 : var0.moduleBgOn.getValue().getRGB();
   }

   public static int getModuleHoverColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -14408668 : var0.moduleHover.getValue().getRGB();
   }

   public static int getDisabledTextColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? darker(-1, 0.2F) : var0.moduleText.getValue().getRGB();
   }

   public static int getEnabledTextColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -1 : var0.moduleTextOn.getValue().getRGB();
   }

   public static int getToggleBackgroundColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -12303292 : var0.toggleOff.getValue().getRGB();
   }

   public static int getToggleCircleColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -1 : var0.toggleCircle.getValue().getRGB();
   }

   public static int getToggleFillColor() {
      ColorsModule var0 = ColorsModule.INSTANCE;
      return var0 == null ? -2076417 : var0.toggleOn.getValue().getRGB();
   }

   public static int getShadowColor(int color) {
      return (color & 16579836) >> 2 | color & 0xFF000000;
   }

   public static int[] hexToRGBA(int hex) {
      int var1 = hex >> 16 & 0xFF;
      int var2 = hex >> 8 & 0xFF;
      int var3 = hex & 0xFF;
      int var4 = hex >> 24 & 0xFF;
      return new int[]{var1, var2, var3, var4};
   }

   public static int[] hexToRGB(int hex) {
      int var1 = hex >> 16 & 0xFF;
      int var2 = hex >> 8 & 0xFF;
      int var3 = hex & 0xFF;
      return new int[]{var1, var2, var3};
   }

   public static int rgbToHex(int red, int green, int blue) {
      return red << 16 | green << 8 | blue;
   }

   public static int rgbaToHex(int red, int green, int blue, int alpha) {
      return alpha << 24 | red << 16 | green << 8 | blue;
   }

   public static int darker(int color, float factor) {
      float var2 = 1.0F - factor;
      int var3 = (int)((float)(color >> 16 & 0xFF) * var2);
      int var4 = (int)((float)(color >> 8 & 0xFF) * var2);
      int var5 = (int)((float)(color & 0xFF) * var2);
      int var6 = color >> 24 & 0xFF;
      return (var3 & 0xFF) << 16 | (var4 & 0xFF) << 8 | var5 & 0xFF | (var6 & 0xFF) << 24;
   }

   public static int brighter(int color, float factor) {
      float var2 = 1.0F / (1.0F - factor);
      int var3 = color >> 16 & 0xFF;
      int var4 = color >> 8 & 0xFF;
      int var5 = color & 0xFF;
      int var6 = color >> 24 & 0xFF;
      if (var3 == 0 && var4 == 0 && var5 == 0) {
         int var11 = (int)(1.0 / (1.0 - (double)factor));
         return (var6 & 0xFF) << 24 | (var11 & 0xFF) << 16 | (var11 & 0xFF) << 8 | var11 & 0xFF;
      } else {
         int var7 = (int)(1.0 / (1.0 - (double)factor));
         int var8 = var3 > 0 && var3 < var7 ? var7 : var3;
         int var9 = var4 > 0 && var4 < var7 ? var7 : var4;
         int var10 = var5 > 0 && var5 < var7 ? var7 : var5;
         var8 = Math.min((int)((float)var8 * var2), 255);
         var9 = Math.min((int)((float)var9 * var2), 255);
         var10 = Math.min((int)((float)var10 * var2), 255);
         return (var6 & 0xFF) << 24 | (var8 & 0xFF) << 16 | (var9 & 0xFF) << 8 | var10 & 0xFF;
      }
   }

   public static int applyOpacity(int color, float opacityFactor) {
      opacityFactor = Math.min(1.0F, Math.max(0.0F, opacityFactor));
      return color & 16777215 | ((int)(opacityFactor * 255.0F) & 0xFF) << 24;
   }

   public static int scaleOpacity(int color, float opacityFactor) {
      opacityFactor = Math.min(1.0F, Math.max(0.0F, opacityFactor));
      int var2 = color >>> 24 & 0xFF;
      return color & 16777215 | (Math.round((float)var2 * opacityFactor) & 0xFF) << 24;
   }

   public static int applyOpacity(int color, int opacity) {
      opacity = Math.min(255, Math.max(0, opacity));
      return color & 16777215 | (opacity & 0xFF) << 24;
   }

   public static int interpolateColors(int color1, int color2, float amount) {
      amount = Math.min(1.0F, Math.max(0.0F, amount));
      int var3 = (int)MathUtility.interpolate((float)(color1 >> 16 & 0xFF), (float)(color2 >> 16 & 0xFF), amount);
      int var4 = (int)MathUtility.interpolate((float)(color1 >> 8 & 0xFF), (float)(color2 >> 8 & 0xFF), amount);
      int var5 = (int)MathUtility.interpolate((float)(color1 & 0xFF), (float)(color2 & 0xFF), amount);
      int var6 = (int)MathUtility.interpolate((float)(color1 >>> 24 & 0xFF), (float)(color2 >>> 24 & 0xFF), amount);
      return rgbaToHex(var3, var4, var5, var6);
   }

   public static int rainbow(int speed, int index, float saturation, float brightness) {
      int var4 = (int)((System.currentTimeMillis() / (long)speed + (long)index) % 360L);
      float var5 = (float)var4 / 360.0F;
      return Color.HSBtoRGB(var5, saturation, brightness);
   }

   public static int interpolateColorsBackAndForth(int speed, int index, int startColor, int endColor) {
      int var4 = (int)((System.currentTimeMillis() / (long)speed - (long)index) % 360L);
      var4 = (var4 >= 180 ? 360 - var4 : var4) * 2;
      return interpolateColors(startColor, endColor, (float)var4 / 360.0F);
   }
}
