package mapped;

import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSAudioFilter;

@ObfuscatedName("ci")
public class AudioFilter implements RSAudioFilter {
   @ObfuscatedName("au")
   static float[][] field400;
   @ObfuscatedName("aa")
   static int[][] coefficients;
   @ObfuscatedName("ac")
   static float field395;
   @ObfuscatedName("al")
   static int forwardMultiplier;
   @ObfuscatedName("at")
   int[] pairs;
   @ObfuscatedName("ar")
   int[][][] field397;
   @ObfuscatedName("ao")
   int[][][] field398;
   @ObfuscatedName("ab")
   int[] field399;

   static {
      field400 = new float[2][8];
      coefficients = new int[2][8];
   }

   AudioFilter() {
      this.pairs = new int[2];
      this.field397 = new int[2][2][4];
      this.field398 = new int[2][2][4];
      this.field399 = new int[2];
   }

   @ObfuscatedName("at")
   float method1156_base(int var1, int var2, float var3) {
      float var4 = (float)this.field398[var1][0][var2] + var3 * (float)(this.field398[var1][1][var2] - this.field398[var1][0][var2]);
      var4 *= 0.0015258789F;
      return 1.0F - (float)Math.pow(10.0D, (double)(-var4 / 20.0F));
   }

   @ObfuscatedName("ar")
   float method1164_base(int var1, int var2, float var3) {
      float var4 = (float)this.field397[var1][0][var2] + var3 * (float)(this.field397[var1][1][var2] - this.field397[var1][0][var2]);
      var4 *= 1.2207031E-4F;
      return normalize_base(var4);
   }

   @ObfuscatedName("ao")
   int compute_base(int var1, float var2) {
      float var3;
      if(var1 == 0) {
         var3 = (float)this.field399[0] + (float)(this.field399[1] - this.field399[0]) * var2;
         var3 *= 0.0030517578F;
         field395 = (float)Math.pow(0.1D, (double)(var3 / 20.0F));
         forwardMultiplier = (int)(field395 * 65536.0F);
      }

      if(this.pairs[var1] == 0) {
         return 0;
      } else {
         var3 = this.method1156_base(var1, 0, var2);
         field400[var1][0] = -2.0F * var3 * (float)Math.cos((double)this.method1164_base(var1, 0, var2));
         field400[var1][1] = var3 * var3;

         int var4;
         for(var4 = 1; var4 < this.pairs[var1]; ++var4) {
            var3 = this.method1156_base(var1, var4, var2);
            float var5 = -2.0F * var3 * (float)Math.cos((double)this.method1164_base(var1, var4, var2));
            float var6 = var3 * var3;
            field400[var1][var4 * 2 + 1] = field400[var1][var4 * 2 - 1] * var6;
            field400[var1][var4 * 2] = field400[var1][var4 * 2 - 1] * var5 + field400[var1][var4 * 2 - 2] * var6;

            for(int var7 = var4 * 2 - 1; var7 >= 2; --var7) {
               field400[var1][var7] += field400[var1][var7 - 1] * var5 + field400[var1][var7 - 2] * var6;
            }

            field400[var1][1] += field400[var1][0] * var5 + var6;
            field400[var1][0] += var5;
         }

         if(var1 == 0) {
            for(var4 = 0; var4 < this.pairs[0] * 2; ++var4) {
               field400[0][var4] *= field395;
            }
         }

         for(var4 = 0; var4 < this.pairs[var1] * 2; ++var4) {
            coefficients[var1][var4] = (int)(field400[var1][var4] * 65536.0F);
         }

         return this.pairs[var1] * 2;
      }
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(Luj;Lbh;)V"
   )
   final void method1158(Buffer var1, SoundEnvelope var2) {
      int var3 = var1.readUnsignedByte_base();
      this.pairs[0] = var3 >> 4;
      this.pairs[1] = var3 & 15;
      if(var3 != 0) {
         this.field399[0] = var1.readUnsignedShort_base();
         this.field399[1] = var1.readUnsignedShort_base();
         int var4 = var1.readUnsignedByte_base();

         int var5;
         int var6;
         for(var5 = 0; var5 < 2; ++var5) {
            for(var6 = 0; var6 < this.pairs[var5]; ++var6) {
               this.field397[var5][0][var6] = var1.readUnsignedShort_base();
               this.field398[var5][0][var6] = var1.readUnsignedShort_base();
            }
         }

         for(var5 = 0; var5 < 2; ++var5) {
            for(var6 = 0; var6 < this.pairs[var5]; ++var6) {
               if((var4 & 1 << var5 * 4 << var6) != 0) {
                  this.field397[var5][1][var6] = var1.readUnsignedShort_base();
                  this.field398[var5][1][var6] = var1.readUnsignedShort_base();
               } else {
                  this.field397[var5][1][var6] = this.field397[var5][0][var6];
                  this.field398[var5][1][var6] = this.field398[var5][0][var6];
               }
            }
         }

         if(var4 != 0 || this.field399[1] != this.field399[0]) {
            var2.method861(var1);
         }
      } else {
         int[] var7 = this.field399;
         this.field399[1] = 0;
         var7[0] = 0;
      }

   }

   @ObfuscatedName("ah")
   static float normalize_base(float var0) {
      float var1 = 32.703197F * (float)Math.pow(2.0D, (double)var0);
      return var1 * 3.1415927F / 11025.0F;
   }
}
