package mapped;

import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSPacketBuffer;

@ObfuscatedName("uo")
public class PacketBuffer extends Buffer implements RSPacketBuffer {
   @ObfuscatedName("ah")
   static final int[] field5134;
   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "Luy;"
   )
   IsaacCipher isaacCipher;
   @ObfuscatedName("ar")
   @ObfuscatedGetter(
      intValue = -1984139847
   )
   int bitIndex;

   static {
      field5134 = new int[]{0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823, Integer.MAX_VALUE, -1};
   }

   public PacketBuffer(int var1) {
      super(var1);
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "([II)V",
      garbageValue = "986454226"
   )
   public void newIsaacCipher_base(int[] var1) {
      this.isaacCipher = new IsaacCipher(var1);
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(Luy;I)V",
      garbageValue = "1815702468"
   )
   public void method9209(IsaacCipher var1) {
      this.isaacCipher = var1;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "264966043"
   )
   public void writeByteIsaac_base(int var1) {
      super.array[++super.offset - 1] = (byte)(var1 + this.isaacCipher.nextInt_base());
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(I)I",
      garbageValue = "-1536095737"
   )
   public int readByteIsaac_base() {
      return super.array[++super.offset - 1] - this.isaacCipher.nextInt_base() & 255;
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(B)Z",
      garbageValue = "52"
   )
   public boolean method9206_base() {
      int var1 = super.array[super.offset] - this.isaacCipher.method9899_base() & 255;
      return var1 >= 128;
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(B)I",
      garbageValue = "-41"
   )
   public int readSmartByteShortIsaac_base() {
      int var1 = super.array[++super.offset - 1] - this.isaacCipher.nextInt_base() & 255;
      return var1 < 128?var1:(var1 - 128 << 8) + (super.array[++super.offset - 1] - this.isaacCipher.nextInt_base() & 255);
   }

   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "([BIII)V",
      garbageValue = "947660207"
   )
   public void method9208_base(byte[] var1, int var2, int var3) {
      for(int var4 = 0; var4 < var3; ++var4) {
         var1[var4 + var2] = (byte)(super.array[++super.offset - 1] - this.isaacCipher.nextInt_base());
      }

   }

   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "(B)V",
      garbageValue = "28"
   )
   public void importIndex_base() {
      this.bitIndex = super.offset * 8;
   }

   @ObfuscatedName("al")
   @ObfuscatedSignature(
      descriptor = "(II)I",
      garbageValue = "-551105504"
   )
   public int readBits_base(int var1) {
      int var2 = this.bitIndex >> 3;
      int var3 = 8 - (this.bitIndex & 7);
      int var4 = 0;

      for(this.bitIndex += var1; var1 > var3; var3 = 8) {
         var4 += (super.array[var2++] & field5134[var3]) << var1 - var3;
         var1 -= var3;
      }

      if(var3 == var1) {
         var4 += super.array[var2] & field5134[var3];
      } else {
         var4 += super.array[var2] >> var3 - var1 & field5134[var1];
      }

      return var4;
   }

   @ObfuscatedName("az")
   @ObfuscatedSignature(
      descriptor = "(S)V",
      garbageValue = "20580"
   )
   public void exportIndex_base() {
      super.offset = (this.bitIndex + 7) / 8;
   }

   @ObfuscatedName("ap")
   @ObfuscatedSignature(
      descriptor = "(II)I",
      garbageValue = "1836154384"
   )
   public int bitsRemaining_base(int var1) {
      return var1 * 8 - this.bitIndex;
   }
}
