package mapped;

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

@ObfuscatedName("bh")
public class SoundEnvelope implements RSSoundEnvelope {
   @ObfuscatedName("at")
   int segments;
   @ObfuscatedName("ah")
   int[] durations;
   @ObfuscatedName("ar")
   int[] phases;
   @ObfuscatedName("ao")
   int start;
   @ObfuscatedName("ab")
   int end;
   @ObfuscatedName("au")
   int form;
   @ObfuscatedName("aa")
   int ticks;
   @ObfuscatedName("ac")
   int phaseIndex;
   @ObfuscatedName("al")
   int step;
   @ObfuscatedName("az")
   int amplitude;
   @ObfuscatedName("ap")
   int max;

   SoundEnvelope() {
      this.segments = 2;
      this.durations = new int[2];
      this.phases = new int[2];
      this.durations[0] = 0;
      this.durations[1] = 65535;
      this.phases[0] = 0;
      this.phases[1] = 65535;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(Luj;)V"
   )
   final void method849(Buffer var1) {
      this.form = var1.readUnsignedByte_base();
      this.start = var1.readInt_base();
      this.end = var1.readInt_base();
      this.method861(var1);
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(Luj;)V"
   )
   final void method861(Buffer var1) {
      this.segments = var1.readUnsignedByte_base();
      this.durations = new int[this.segments];
      this.phases = new int[this.segments];

      for(int var2 = 0; var2 < this.segments; ++var2) {
         this.durations[var2] = var1.readUnsignedShort_base();
         this.phases[var2] = var1.readUnsignedShort_base();
      }

   }

   @ObfuscatedName("ar")
   final void reset_base() {
      this.ticks = 0;
      this.phaseIndex = 0;
      this.step = 0;
      this.amplitude = 0;
      this.max = 0;
   }

   @ObfuscatedName("ao")
   final int doStep_base(int var1) {
      if(this.max >= this.ticks) {
         this.amplitude = this.phases[this.phaseIndex++] << 15;
         if(this.phaseIndex >= this.segments) {
            this.phaseIndex = this.segments - 1;
         }

         this.ticks = (int)((double)this.durations[this.phaseIndex] / 65536.0D * (double)var1);
         if(this.ticks > this.max) {
            this.step = ((this.phases[this.phaseIndex] << 15) - this.amplitude) / (this.ticks - this.max);
         }
      }

      this.amplitude += this.step;
      ++this.max;
      return this.amplitude - this.step >> 15;
   }
}
