package dev.crucifixbeta.mod.modules.impl.client;

import dev.crucifixbeta.mod.gui.epsilon.EpsilonTheme;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.ColorSetting;
import dev.crucifixbeta.mod.modules.settings.impl.EnumSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import java.awt.Color;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.DrawContext;

@Environment(EnvType.CLIENT)
public class ColorsModule extends Module {
   public static ColorsModule INSTANCE;
   public final EnumSetting<ColorsModule.ColorMode> colorMode = this.add(
      new EnumSetting<>("ColorMode", ColorsModule.ColorMode.Static).injectTask(this::syncClickGuiColorMode)
   );
   public final SliderSetting rainbowSpeed = this.add(new SliderSetting("RainbowSpeed", 4.0, 1.0, 10.0, 0.1));
   public final SliderSetting saturation = this.add(new SliderSetting("Saturation", 130.0, 1.0, 255.0));
   public final SliderSetting rainbowDelay = this.add(new SliderSetting("Delay", 350, 0, 1000));
   public final ColorSetting panelBg = this.add(new ColorSetting("PanelBg", new Color(15, 15, 15, 210), this::isStaticMode));
   public final ColorSetting accent = this.add(new ColorSetting("Accent", new Color(224, 80, 255, 255), this::isStaticMode));
   public final ColorSetting moduleBg = this.add(new ColorSetting("ModuleBg", new Color(30, 30, 45, 150), this::isStaticMode));
   public final ColorSetting moduleBgOn = this.add(new ColorSetting("ModuleBgOn", new Color(224, 80, 255, 165), this::isStaticMode));
   public final ColorSetting moduleHover = this.add(new ColorSetting("ModuleHover", new Color(36, 36, 36, 215), this::isStaticMode));
   public final ColorSetting moduleText = this.add(new ColorSetting("ModuleText", new Color(215, 215, 215, 255), this::isStaticMode));
   public final ColorSetting moduleTextOn = this.add(new ColorSetting("ModuleTextOn", new Color(255, 255, 255, 255), this::isStaticMode));
   public final ColorSetting settingText = this.add(new ColorSetting("SettingText", new Color(230, 230, 230, 255), this::isStaticMode));
   public final ColorSetting settingTextDim = this.add(new ColorSetting("SettingTextDim", new Color(154, 154, 154, 255), this::isStaticMode));
   public final ColorSetting toggleOn = this.add(new ColorSetting("ToggleOn", new Color(224, 80, 255, 190), this::isStaticMode));
   public final ColorSetting toggleOff = this.add(new ColorSetting("ToggleOff", new Color(68, 68, 68, 165), this::isStaticMode));
   public final ColorSetting toggleCircle = this.add(new ColorSetting("ToggleCircle", new Color(255, 255, 255, 255), this::isStaticMode));
   public final ColorSetting sliderTrack = this.add(new ColorSetting("SliderTrack", new Color(-1764896038, true), this::isStaticMode));
   public final ColorSetting sliderFill = this.add(new ColorSetting("SliderFill", new Color(224, 80, 255, 255), this::isStaticMode));
   public final ColorSetting sliderKnob = this.add(new ColorSetting("SliderKnob", new Color(255, 107, 214, 255), this::isStaticMode));
   public final ColorSetting headerBg = this.add(new ColorSetting("HeaderBg", new Color(15, 15, 15, 220), this::isStaticMode));
   public final ColorSetting stringSetting = this.add(new ColorSetting("StringSetting", new Color(255, 107, 214, 255), this::isStaticMode));
   public final ColorSetting bindSetting = this.add(new ColorSetting("BindSetting", new Color(182, 108, 255, 255), this::isStaticMode));
   public final ColorSetting hexInput = this.add(new ColorSetting("HexInput", new Color(255, 107, 214, 255), this::isStaticMode));
   public final ColorSetting enumBg = this.add(new ColorSetting("EnumBg", new Color(-385875969, true), this::isStaticMode));
   public final ColorSetting enumHover = this.add(new ColorSetting("EnumHover", new Color(-1091834124, true), this::isStaticMode));
   public final ColorSetting enumText = this.add(new ColorSetting("EnumText", new Color(-14935010, true), this::isStaticMode));
   public final ColorSetting enumSelectedText = this.add(new ColorSetting("EnumSelectedText", new Color(224, 80, 255, 255), this::isStaticMode));
   public final ColorSetting clickColor = this.add(new ColorSetting("ClickColor", new Color(224, 80, 255, 255), this::isStaticMode));
   public final ColorSetting clickActiveColor = this.add(new ColorSetting("ClickActiveColor", new Color(224, 80, 255, 255), this::isStaticMode));
   public final ColorSetting clickHoverColor = this.add(new ColorSetting("ClickHoverColor", new Color(2030043136, true), this::isStaticMode));
   public final ColorSetting clickDefaultColor = this.add(new ColorSetting("ClickDefaultColor", new Color(640363307, true), this::isStaticMode));
   public final ColorSetting clickDefaultTextColor = this.add(new ColorSetting("ClickDefaultTextColor", new Color(-3159809, true), this::isStaticMode));
   public final ColorSetting clickEnableTextColor = this.add(new ColorSetting("ClickEnableTextColor", new Color(255, 255, 255), this::isStaticMode));
   public final ColorSetting clickBackGround = this.add(
      new ColorSetting("ClickBackGround", new Color(255, 255, 255, 0), this::isStaticMode).injectBoolean(false)
   );
   public final ColorSetting clickLineColor = this.add(new ColorSetting("ClickLineColor", new Color(-1, true), this::isStaticMode));
   public final ColorSetting clickGear = this.add(
      new ColorSetting("ClickGear", new Color(255, 214, 250, 255), this::isStaticMode).injectBoolean(true)
   );
   public final ColorSetting clickTint = this.add(
      new ColorSetting("ClickTint", new Color(224, 80, 255, 72), this::isStaticMode).injectBoolean(false)
   );
   public final ColorSetting clickTintEnd = this.add(new ColorSetting("ClickTintEnd", new Color(255, 107, 214, 42), this::isStaticMode));
   public final SliderSetting clickBackgroundAlpha = this.add(new SliderSetting("ClickBgAlpha", 0, 0, 255));
   public final EnumSetting<ColorsModule.HudColorMode> hudColorMode = this.add(new EnumSetting<>("HudColorMode", ColorsModule.HudColorMode.Pulse));
   public final ColorSetting hudColor = this.add(new ColorSetting("HudColor", new Color(-7699730, true), this::showHudCustomColors));
   public final ColorSetting hudEndColor = this.add(new ColorSetting("HudEndColor", new Color(-7699730, true), this::showHudPulseColors));
   public final SliderSetting hudRainbowSpeed = this.add(new SliderSetting("HudRainbowSpd", 1.0, 1.0, 10.0, 0.1));
   public final SliderSetting hudSaturation = this.add(new SliderSetting("HudSaturation", 220.0, 1.0, 255.0));
   public final SliderSetting hudRainbowDelay = this.add(new SliderSetting("HudDelay", 220, 0, 1000));
   public final SliderSetting hudPulseSpeed = this.add(new SliderSetting("HudPulseSpd", 1.0, 0.0, 5.0, 0.1));
   public final SliderSetting hudPulseCounter = this.add(new SliderSetting("HudPulseCtr", 10, 1, 50));
   public final ColorSetting hudBgColor = this.add(new ColorSetting("HudBgColor", new Color(0, 0, 0, 100), this::showHudCustomColors));
   public final ColorSetting hudRectColor = this.add(new ColorSetting("HudRectColor", new Color(-7699730, true), this::showHudCustomColors));
   public final ColorSetting hudGlowColor = this.add(new ColorSetting("HudGlowColor", new Color(-7699730, true), this::showHudCustomColors));
   public final ColorSetting hudWatermarkPulse = this.add(new ColorSetting("HudWatermarkPulse", new Color(-7699730, true), this::showHudPulseColors));

   public ColorsModule() {
      super("Colors", Module.Category.Client);
      this.setChinese("\u989c\u8272");
      this.panelBg.setChinese("\u9762\u677f\u80cc\u666f");
      this.accent.setChinese("\u4e3b\u9898\u8272");
      this.moduleBg.setChinese("\u529f\u80fd\u80cc\u666f");
      this.moduleBgOn.setChinese("\u5f00\u542f\u80cc\u666f");
      this.moduleHover.setChinese("\u60ac\u505c\u80cc\u666f");
      this.moduleText.setChinese("\u529f\u80fd\u6587\u5b57");
      this.moduleTextOn.setChinese("\u5f00\u542f\u6587\u5b57");
      this.toggleOn.setChinese("iOS\u5f00\u542f\u586b\u5145");
      this.toggleOff.setChinese("iOS\u5f00\u5173\u80cc\u666f");
      this.toggleCircle.setChinese("iOS\u5f00\u5173\u5706\u5708");
      this.headerBg.setChinese("\u6807\u9898\u80cc\u666f");
      this.stringSetting.setChinese("\u6587\u672c\u8f93\u5165");
      this.bindSetting.setChinese("\u6309\u952e\u7ed1\u5b9a");
      this.hexInput.setChinese("Hex\u8f93\u5165");
      this.enumBg.setChinese("\u6a21\u5f0f\u5c55\u5f00\u80cc\u666f");
      this.enumHover.setChinese("\u6a21\u5f0f\u5c55\u5f00\u60ac\u505c");
      this.enumText.setChinese("\u6a21\u5f0f\u5c55\u5f00\u6587\u5b57");
      this.enumSelectedText.setChinese("\u6a21\u5f0f\u9009\u4e2d\u6587\u5b57");
      this.clickColor.setChinese("ClickGUI\u989c\u8272");
      this.clickActiveColor.setChinese("ClickGUI\u5f00\u542f\u8272");
      this.clickHoverColor.setChinese("ClickGUI\u60ac\u505c\u8272");
      this.clickDefaultColor.setChinese("ClickGUI\u9ed8\u8ba4\u80cc\u666f");
      this.clickDefaultTextColor.setChinese("ClickGUI\u9ed8\u8ba4\u6587\u5b57");
      this.clickEnableTextColor.setChinese("ClickGUI\u5f00\u542f\u6587\u5b57");
      this.clickBackGround.setChinese("ClickGUI\u5217\u80cc\u666f");
      this.clickLineColor.setChinese("ClickGUI\u8fb9\u7ebf");
      this.clickGear.setChinese("ClickGUI\u5c55\u5f00\u56fe\u6807");
      this.clickTint.setChinese("ClickGUI\u754c\u9762\u67d3\u8272");
      this.clickTintEnd.setChinese("ClickGUI\u67d3\u8272\u7ed3\u675f");
      this.clickBackgroundAlpha.setChinese("ClickGUI\u80cc\u666f\u900f\u660e");
      this.hudColorMode.setChinese("HUD\u989c\u8272\u6a21\u5f0f");
      this.hudColor.setChinese("HUD\u4e3b\u989c\u8272");
      this.hudEndColor.setChinese("HUD\u7ed3\u675f\u989c\u8272");
      this.hudRainbowSpeed.setChinese("HUD\u5f69\u8679\u901f\u5ea6");
      this.hudSaturation.setChinese("HUD\u9971\u548c\u5ea6");
      this.hudRainbowDelay.setChinese("HUD\u5f69\u8679\u5ef6\u8fdf");
      this.hudPulseSpeed.setChinese("HUD\u8109\u51b2\u901f\u5ea6");
      this.hudPulseCounter.setChinese("HUD\u8109\u51b2\u8ba1\u6570");
      this.hudBgColor.setChinese("HUD\u80cc\u666f\u989c\u8272");
      this.hudRectColor.setChinese("HUD\u77e9\u5f62\u989c\u8272");
      this.hudGlowColor.setChinese("HUD\u5149\u6655\u989c\u8272");
      this.hudWatermarkPulse.setChinese("HUD\u6c34\u5370\u8109\u51b2");
      this.setChinese("\u989c\u8272");
      this.setChinese("\u989c\u8272");
      INSTANCE = this;
      this.syncClickGuiColorMode();
   }

   public Color getHudColor(double delay) {
      return switch ((ColorsModule.HudColorMode)this.hudColorMode.getValue()) {
         case Custom -> this.hudColor.getValue();
         case Rainbow -> this.getHudRainbow(delay);
         case Pulse -> this.getHudPulse(delay);
      };
   }

   public int getHudColorRGB(double delay) {
      return this.getHudColor(delay).getRGB();
   }

   public Color getHudBackgroundColor(double fade) {
      return injectAlpha(this.hudBgColor.getValue(), fade);
   }

   public Color getHudRectColor(double fade) {
      return injectAlpha(this.hudRectColor.getValue(), fade);
   }

   public Color getHudGlowColor(double fade) {
      return injectAlpha(this.hudGlowColor.getValue(), fade);
   }

   @Override
   public void enable() {
      this.state = true;
   }

   @Override
   public void disable() {
      this.state = true;
   }

   @Override
   public boolean isOn() {
      return true;
   }

   @Override
   public void onRender2D(DrawContext ctx, float tickDelta) {
      this.syncClickGuiColorMode();
      EpsilonTheme.syncFromColors();
   }

   private boolean isStaticMode() {
      return this.colorMode.getValue() == ColorsModule.ColorMode.Static;
   }

   private boolean showHudCustomColors() {
      return this.hudColorMode.getValue() == ColorsModule.HudColorMode.Custom;
   }

   private boolean showHudPulseColors() {
      return this.hudColorMode.getValue() == ColorsModule.HudColorMode.Pulse;
   }

   private Color getHudRainbow(double delay) {
      double var3 = Math.max(0.1, this.hudRainbowSpeed.getValue());
      double var5 = Math.max(1.0, this.hudRainbowDelay.getValue());
      float var7 = (float)(((double)System.currentTimeMillis() / (4200.0 / var3) + delay / var5) % 1.0);
      float var8 = Math.max(0.0F, Math.min(1.0F, this.hudSaturation.getValueFloat() / 255.0F));
      return new Color(Color.HSBtoRGB(var7, var8, 1.0F));
   }

   private Color getHudPulse(double delay) {
      double var3 = this.hudPulseSpeed.getValue();
      if (var3 <= 0.0) {
         return this.hudColor.getValue();
      } else {
         double var5 = Math.max(1.0, this.hudPulseCounter.getValue());
         float var7 = (float)((Math.sin((double)System.currentTimeMillis() / (520.0 / var3) + delay / var5) + 1.0) * 0.5);
         return blend(this.hudColor.getValue(), this.hudEndColor.getValue(), var7);
      }
   }

   private static Color injectAlpha(Color color, double fade) {
      int var3 = (int)Math.max(0.0, Math.min(255.0, (double)color.getAlpha() * fade));
      return new Color(color.getRed(), color.getGreen(), color.getBlue(), var3);
   }

   private static Color blend(Color a, Color b, float t) {
      t = Math.max(0.0F, Math.min(1.0F, t));
      int var3 = (int)((float)a.getRed() + (float)(b.getRed() - a.getRed()) * t);
      int var4 = (int)((float)a.getGreen() + (float)(b.getGreen() - a.getGreen()) * t);
      int var5 = (int)((float)a.getBlue() + (float)(b.getBlue() - a.getBlue()) * t);
      int var6 = (int)((float)a.getAlpha() + (float)(b.getAlpha() - a.getAlpha()) * t);
      return new Color(var3, var4, var5, var6);
   }

   private void syncClickGuiColorMode() {
      ClickGui var1 = ClickGui.getInstance();
      if (var1 != null) {
         var1.colorMode.setValue(switch ((ColorsModule.ColorMode)this.colorMode.getValue()) {
            case Rainbow, RainbowDelay, SimpleRainbow -> ClickGui.ColorMode.Rainbow;
            default -> ClickGui.ColorMode.Static;
         });
      }
   }
@Environment(EnvType.CLIENT)
   public static enum ColorMode {
      Static,
      Rainbow,
      RainbowDelay,
      SimpleRainbow,
      Pulse,
      Ocean,
      Aurora,
      Cotton,
      Sunset,
      Ice,
      Sakura,
      Mint,
      Cyber;

}

   @Environment(EnvType.CLIENT)
   public static enum HudColorMode {
      Custom,
      Rainbow,
      Pulse;

}
}
