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

import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.mod.modules.impl.client.ColorsModule;
import dev.crucifixbeta.mod.modules.settings.Setting;
import java.awt.Color;
import java.util.function.BooleanSupplier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class ColorSetting extends Setting {
   public static final Timer timer;
   public static final float effectSpeed;
   private final Color defaultValue;
   public boolean rainbow = false;
   public boolean injectBoolean = false;
   public boolean booleanValue = false;
   private Color value;
   private boolean defaultRainbow = false;
   private boolean defaultBooleanValue = false;
   private boolean allowClientColor = true;

   public ColorSetting(String name) {
      this(name, new Color(255, 255, 255));
      this.defaultRainbow = true;
   }

   public ColorSetting(String name, BooleanSupplier visibilityIn) {
      super(name, visibilityIn);
      this.defaultValue = this.value = new Color(255, 255, 255);
      this.defaultRainbow = true;
   }

   public ColorSetting(String name, Color defaultValue) {
      super(name);
      this.value = defaultValue;
      this.defaultValue = defaultValue;
   }

   public ColorSetting(String name, Color defaultValue, BooleanSupplier visibilityIn) {
      super(name, visibilityIn);
      this.value = defaultValue;
      this.defaultValue = defaultValue;
   }

   public ColorSetting(String name, int defaultValue) {
      this(name, new Color(defaultValue, true));
   }

   public ColorSetting(String name, int defaultValue, BooleanSupplier visibilityIn) {
      this(name, new Color(defaultValue, true), visibilityIn);
   }

   public Color getValue() {
      if (this.rainbow) {
         ColorsModule var1 = ColorsModule.INSTANCE;
         if (this.allowClientColor && var1 != null && var1.colorMode.getValue() == ColorsModule.ColorMode.Rainbow) {
            double var2 = Math.ceil(((double)System.currentTimeMillis() * var1.rainbowSpeed.getValue() + var1.rainbowDelay.getValue()) / 20.0);
            Color var7 = Color.getHSBColor((float)(var2 % 360.0 / 360.0), var1.saturation.getValueFloat() / 255.0F, 1.0F);
            return new Color(var7.getRed(), var7.getGreen(), var7.getBlue(), this.value.getAlpha());
         } else {
            float[] var5 = Color.RGBtoHSB(this.value.getRed(), this.value.getGreen(), this.value.getBlue(), null);
            float var6 = (float)((double)timer.getMs() * 0.36 * 4.0 / 20.0 % 361.0 / 360.0);
            Color var4 = Color.getHSBColor(var6, var5[1], var5[2]);
            return new Color(var4.getRed(), var4.getGreen(), var4.getBlue(), this.value.getAlpha());
         }
      } else {
         return this.value;
      }
   }

   public Color getRawValue() {
      return this.value;
   }

   public void setValue(Color value) {
      this.value = value;
   }

   public void setValue(int value) {
      this.value = new Color(value, true);
   }

   public void setValueFromString(String value) {
      if (value != null && !value.isBlank()) {
         String var2 = value.trim();
         if (var2.startsWith("#")) {
            long var3 = Long.parseLong(var2.substring(1), 16);
            if (var2.length() == 9) {
               this.value = new Color((int)var3, true);
            } else {
               this.value = new Color((int)var3 | 0xFF000000, true);
            }
         } else {
            String[] var5 = var2.split(",");
            if (var5.length >= 3) {
               int var6 = Integer.parseInt(var5[0].trim());
               int var7 = Integer.parseInt(var5[1].trim());
               int var8 = Integer.parseInt(var5[2].trim());
               int var9 = var5.length >= 4 ? Integer.parseInt(var5[3].trim()) : this.value.getAlpha();
               this.value = new Color(var6, var7, var8, var9);
            }
         }
      }
   }

   public void setRainbow(boolean rainbow) {
      this.rainbow = rainbow;
   }

   public ColorSetting defaultRainbow(boolean defaultRainbow) {
      this.defaultRainbow = defaultRainbow;
      this.rainbow = defaultRainbow;
      return this;
   }

   public ColorSetting injectBoolean(boolean value) {
      this.injectBoolean = true;
      this.defaultBooleanValue = value;
      this.booleanValue = value;
      return this;
   }

   public ColorSetting allowClientColor(boolean value) {
      this.allowClientColor = value;
      return this;
   }

   public Color getDefaultValue() {
      return this.defaultValue;
   }

   public boolean getDefaultBooleanValue() {
      return this.defaultBooleanValue;
   }

   public boolean getDefaultRainbow() {
      return this.defaultRainbow;
   }

   static {

      effectSpeed = 4.0F;
      timer = new Timer();
   }
}
