package mapped;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine.Info;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSDevicePcmPlayer;

@ObfuscatedName("ad")
public class DevicePcmPlayer extends PcmPlayer implements RSDevicePcmPlayer {
   @ObfuscatedName("at")
   AudioFormat format;
   @ObfuscatedName("ah")
   SourceDataLine line;
   @ObfuscatedName("ar")
   @ObfuscatedGetter(
      intValue = 679660463
   )
   int capacity2;
   @ObfuscatedName("ao")
   byte[] byteSamples;

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(B)V",
      garbageValue = "95"
   )
   public void init_base() {
      this.format = new AudioFormat((float)PcmPlayer.field275, 16, WorldMapRectangle.PcmPlayer_stereo?2:1, true, false);
      this.byteSamples = new byte[256 << (WorldMapRectangle.PcmPlayer_stereo?2:1)];
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "851046877"
   )
   public void open_base(int var1) throws LineUnavailableException {
      try {
         Info var2 = new Info(SourceDataLine.class, this.format, var1 << (WorldMapRectangle.PcmPlayer_stereo?2:1));
         this.line = (SourceDataLine)AudioSystem.getLine(var2);
         this.line.open();
         this.line.start();
         this.capacity2 = var1;
      } catch (LineUnavailableException var3) {
         if(UserComparator4.method2943_base(var1) != 1) {
            this.open_base(class184.method3680_base(var1));
            return;
         }

         this.line = null;
         throw var3;
      }

   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(I)I",
      garbageValue = "-79725090"
   )
   protected int position_base() {
      return this.capacity2 - (this.line.available() >> (WorldMapRectangle.PcmPlayer_stereo?2:1));
   }

   @ObfuscatedName("ao")
   protected void write_base() {
      int var1 = 256;
      if(WorldMapRectangle.PcmPlayer_stereo) {
         var1 <<= 1;
      }

      for(int var2 = 0; var2 < var1; ++var2) {
         int var3 = super.samples[var2];
         if((var3 + 8388608 & -16777216) != 0) {
            var3 = 8388607 ^ var3 >> 31;
         }

         this.byteSamples[var2 * 2] = (byte)(var3 >> 8);
         this.byteSamples[var2 * 2 + 1] = (byte)(var3 >> 16);
      }

      this.line.write(this.byteSamples, 0, var1 << 1);
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "1254913818"
   )
   protected void close_base() {
      if(this.line != null) {
         this.line.close();
         this.line = null;
      }

   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-264971545"
   )
   protected void discard_base() {
      this.line.flush();
   }
}
