package mapped;

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

@ObfuscatedName("mj")
public class MusicPatchPcmStream extends PcmStream implements RSMusicPatchPcmStream {
   @ObfuscatedName("tf")
   @ObfuscatedSignature(
      descriptor = "Lby;"
   )
   static PcmPlayer pcmPlayer1;
   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "Lmn;"
   )
   MidiPcmStream superStream;
   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "Lon;"
   )
   NodeDeque queue;
   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "Lbn;"
   )
   PcmStreamMixer mixer;

   @ObfuscatedSignature(
      descriptor = "(Lmn;)V"
   )
   MusicPatchPcmStream(MidiPcmStream var1) {
      this.queue = new NodeDeque();
      this.mixer = new PcmStreamMixer();
      this.superStream = var1;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(Lmw;[IIIII)V",
      garbageValue = "-1281472428"
   )
   void method6216(MusicPatchNode var1, int[] var2, int var3, int var4, int var5) {
      if((this.superStream.field3450[var1.field3515] & 4) != 0 && var1.field3498 < 0) {
         int var6 = this.superStream.field3442[var1.field3515] / PcmPlayer.field275;

         while(true) {
            int var7 = (var6 + 1048575 - var1.field3516) / var6;
            if(var7 > var4) {
               var1.field3516 += var4 * var6;
               break;
            }

            var1.stream.fill_base(var2, var3, var7);
            var3 += var7;
            var4 -= var7;
            var1.field3516 += var6 * var7 - 1048576;
            int var8 = PcmPlayer.field275 / 100;
            int var9 = 262144 / var6;
            if(var9 < var8) {
               var8 = var9;
            }

            RawPcmStream var10 = var1.stream;
            if(this.superStream.field3440[var1.field3515] == 0) {
               var1.stream = RawPcmStream.method904(var1.rawSound, var10.method919_base(), var10.method994_base(), var10.method988_base());
            } else {
               var1.stream = RawPcmStream.method904(var1.rawSound, var10.method919_base(), 0, var10.method988_base());
               this.superStream.method6022(var1, var1.table.field3478[var1.field3517] < 0);
               var1.stream.method915_base(var8, var10.method994_base());
            }

            if(var1.table.field3478[var1.field3517] < 0) {
               var1.stream.setNumLoops_base(-1);
            }

            var10.method917_base(var8);
            var10.fill_base(var2, var3, var5 - var3);
            if(var10.method921_base()) {
               this.mixer.method713(var10);
            }
         }
      }

      var1.stream.fill_base(var2, var3, var4);
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(Lmw;II)V",
      garbageValue = "-1821439633"
   )
   void method6229(MusicPatchNode var1, int var2) {
      if((this.superStream.field3450[var1.field3515] & 4) != 0 && var1.field3498 < 0) {
         int var3 = this.superStream.field3442[var1.field3515] / PcmPlayer.field275;
         int var4 = (var3 + 1048575 - var1.field3516) / var3;
         var1.field3516 = var3 * var2 + var1.field3516 & 1048575;
         if(var4 <= var2) {
            if(this.superStream.field3440[var1.field3515] == 0) {
               var1.stream = RawPcmStream.method904(var1.rawSound, var1.stream.method919_base(), var1.stream.method994_base(), var1.stream.method988_base());
            } else {
               var1.stream = RawPcmStream.method904(var1.rawSound, var1.stream.method919_base(), 0, var1.stream.method988_base());
               this.superStream.method6022(var1, var1.table.field3478[var1.field3517] < 0);
            }

            if(var1.table.field3478[var1.field3517] < 0) {
               var1.stream.setNumLoops_base(-1);
            }

            var2 = var1.field3516 / var3;
         }
      }

      var1.stream.skip_base(var2);
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "()Lbk;"
   )
   protected PcmStream vmethod6210() {
      MusicPatchNode var1 = (MusicPatchNode)this.queue.method7265();
      return (PcmStream)(var1 == null?null:(var1.stream != null?var1.stream:this.vmethod6222()));
   }

   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "()Lbk;"
   )
   protected PcmStream vmethod6222() {
      MusicPatchNode var1;
      do {
         var1 = (MusicPatchNode)this.queue.method7235();
         if(var1 == null) {
            return null;
         }
      } while(var1.stream == null);

      return var1.stream;
   }

   @ObfuscatedName("ac")
   protected int vmethod6210_base() {
      return 0;
   }

   @ObfuscatedName("al")
   protected void fill_base(int[] var1, int var2, int var3) {
      this.mixer.fill_base(var1, var2, var3);

      for(MusicPatchNode var6 = (MusicPatchNode)this.queue.method7265(); var6 != null; var6 = (MusicPatchNode)this.queue.method7235()) {
         if(!this.superStream.method6043(var6)) {
            int var4 = var2;
            int var5 = var3;

            do {
               if(var5 <= var6.field3513) {
                  this.method6216(var6, var1, var4, var5, var5 + var4);
                  var6.field3513 -= var5;
                  break;
               }

               this.method6216(var6, var1, var4, var6.field3513, var5 + var4);
               var4 += var6.field3513;
               var5 -= var6.field3513;
            } while(!this.superStream.method6044(var6, var1, var4, var5));
         }
      }

   }

   @ObfuscatedName("ap")
   protected void skip_base(int var1) {
      this.mixer.skip_base(var1);

      for(MusicPatchNode var3 = (MusicPatchNode)this.queue.method7265(); var3 != null; var3 = (MusicPatchNode)this.queue.method7235()) {
         if(!this.superStream.method6043(var3)) {
            int var2 = var1;

            do {
               if(var2 <= var3.field3513) {
                  this.method6229(var3, var2);
                  var3.field3513 -= var2;
                  break;
               }

               this.method6229(var3, var3.field3513);
               var2 -= var3.field3513;
            } while(!this.superStream.method6044(var3, (int[])null, 0, var2));
         }
      }

   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/CharSequence;I)[B",
      garbageValue = "125922381"
   )
   public static byte[] method6233_base(CharSequence var0) {
      int var1 = var0.length();
      byte[] var2 = new byte[var1];

      for(int var3 = 0; var3 < var1; ++var3) {
         char var4 = var0.charAt(var3);
         if(var4 > 0 && var4 < 128 || var4 >= 160 && var4 <= 255) {
            var2[var3] = (byte)var4;
         } else if(var4 == 8364) {
            var2[var3] = -128;
         } else if(var4 == 8218) {
            var2[var3] = -126;
         } else if(var4 == 402) {
            var2[var3] = -125;
         } else if(var4 == 8222) {
            var2[var3] = -124;
         } else if(var4 == 8230) {
            var2[var3] = -123;
         } else if(var4 == 8224) {
            var2[var3] = -122;
         } else if(var4 == 8225) {
            var2[var3] = -121;
         } else if(var4 == 710) {
            var2[var3] = -120;
         } else if(var4 == 8240) {
            var2[var3] = -119;
         } else if(var4 == 352) {
            var2[var3] = -118;
         } else if(var4 == 8249) {
            var2[var3] = -117;
         } else if(var4 == 338) {
            var2[var3] = -116;
         } else if(var4 == 381) {
            var2[var3] = -114;
         } else if(var4 == 8216) {
            var2[var3] = -111;
         } else if(var4 == 8217) {
            var2[var3] = -110;
         } else if(var4 == 8220) {
            var2[var3] = -109;
         } else if(var4 == 8221) {
            var2[var3] = -108;
         } else if(var4 == 8226) {
            var2[var3] = -107;
         } else if(var4 == 8211) {
            var2[var3] = -106;
         } else if(var4 == 8212) {
            var2[var3] = -105;
         } else if(var4 == 732) {
            var2[var3] = -104;
         } else if(var4 == 8482) {
            var2[var3] = -103;
         } else if(var4 == 353) {
            var2[var3] = -102;
         } else if(var4 == 8250) {
            var2[var3] = -101;
         } else if(var4 == 339) {
            var2[var3] = -100;
         } else if(var4 == 382) {
            var2[var3] = -98;
         } else if(var4 == 376) {
            var2[var3] = -97;
         } else {
            var2[var3] = 63;
         }
      }

      return var2;
   }
}
