package mapped;

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

@ObfuscatedName("mx")
public class MidiFileReader implements RSMidiFileReader {
   @ObfuscatedName("az")
   static final byte[] field3466;
   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "Luj;"
   )
   Buffer buffer;
   @ObfuscatedName("ah")
   int division;
   @ObfuscatedName("ar")
   int[] trackStarts;
   @ObfuscatedName("ao")
   int[] trackPositions;
   @ObfuscatedName("ab")
   int[] trackLengths;
   @ObfuscatedName("au")
   int[] field3462;
   @ObfuscatedName("aa")
   int field3464;
   @ObfuscatedName("al")
   long field3463;

   static {
      field3466 = new byte[]{(byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)2, (byte)0, (byte)1, (byte)2, (byte)1, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
   }

   MidiFileReader() {
      this.buffer = new Buffer((byte[])null);
   }

   @ObfuscatedName("at")
   void parse_base(byte[] var1) {
      this.buffer.array = var1;
      this.buffer.offset = 10;
      int var2 = this.buffer.readUnsignedShort_base();
      this.division = this.buffer.readUnsignedShort_base();
      this.field3464 = 500000;
      this.trackStarts = new int[var2];

      int var3;
      int var5;
      for(var3 = 0; var3 < var2; this.buffer.offset += var5) {
         int var4 = this.buffer.readInt_base();
         var5 = this.buffer.readInt_base();
         if(var4 == 1297379947) {
            this.trackStarts[var3] = this.buffer.offset;
            ++var3;
         }
      }

      this.field3463 = 0L;
      this.trackPositions = new int[var2];

      for(var3 = 0; var3 < var2; ++var3) {
         this.trackPositions[var3] = this.trackStarts[var3];
      }

      this.trackLengths = new int[var2];
      this.field3462 = new int[var2];
   }

   @ObfuscatedName("ah")
   void clear_base() {
      this.buffer.array = null;
      this.trackStarts = null;
      this.trackPositions = null;
      this.trackLengths = null;
      this.field3462 = null;
   }

   @ObfuscatedName("ar")
   boolean isReady_base() {
      return this.buffer.array != null;
   }

   @ObfuscatedName("ao")
   int trackCount_base() {
      return this.trackPositions.length;
   }

   @ObfuscatedName("ab")
   void gotoTrack_base(int var1) {
      this.buffer.offset = this.trackPositions[var1];
   }

   @ObfuscatedName("au")
   void markTrackPosition_base(int var1) {
      this.trackPositions[var1] = this.buffer.offset;
   }

   @ObfuscatedName("aa")
   void setTrackDone_base() {
      this.buffer.offset = -1;
   }

   @ObfuscatedName("ac")
   void readTrackLength_base(int var1) {
      int var2 = this.buffer.readVarInt_base();
      this.trackLengths[var1] += var2;
   }

   @ObfuscatedName("al")
   int readMessage_base(int var1) {
      int var2 = this.readMessage0_base(var1);
      return var2;
   }

   @ObfuscatedName("az")
   int readMessage0_base(int var1) {
      byte var2 = this.buffer.array[this.buffer.offset];
      int var5;
      if(var2 < 0) {
         var5 = var2 & 255;
         this.field3462[var1] = var5;
         ++this.buffer.offset;
      } else {
         var5 = this.field3462[var1];
      }

      if(var5 != 240 && var5 != 247) {
         return this.method6144_base(var1, var5);
      } else {
         int var3 = this.buffer.readVarInt_base();
         if(var5 == 247 && var3 > 0) {
            int var4 = this.buffer.array[this.buffer.offset] & 255;
            if(var4 >= 241 && var4 <= 243 || var4 == 246 || var4 == 248 || var4 >= 250 && var4 <= 252 || var4 == 254) {
               ++this.buffer.offset;
               this.field3462[var1] = var4;
               return this.method6144_base(var1, var4);
            }
         }

         this.buffer.offset += var3;
         return 0;
      }
   }

   @ObfuscatedName("ap")
   int method6144_base(int var1, int var2) {
      int var4;
      if(var2 == 255) {
         int var7 = this.buffer.readUnsignedByte_base();
         var4 = this.buffer.readVarInt_base();
         if(var7 == 47) {
            this.buffer.offset += var4;
            return 1;
         } else if(var7 == 81) {
            int var5 = this.buffer.readUnsignedMedium_base();
            var4 -= 3;
            int var6 = this.trackLengths[var1];
            this.field3463 += (long)var6 * (long)(this.field3464 - var5);
            this.field3464 = var5;
            this.buffer.offset += var4;
            return 2;
         } else {
            this.buffer.offset += var4;
            return 3;
         }
      } else {
         byte var3 = field3466[var2 - 128];
         var4 = var2;
         if(var3 >= 1) {
            var4 = var2 | this.buffer.readUnsignedByte_base() << 8;
         }

         if(var3 >= 2) {
            var4 |= this.buffer.readUnsignedByte_base() << 16;
         }

         return var4;
      }
   }

   @ObfuscatedName("av")
   long method6145_base(int var1) {
      return this.field3463 + (long)var1 * (long)this.field3464;
   }

   @ObfuscatedName("ax")
   int getPrioritizedTrack_base() {
      int var1 = this.trackPositions.length;
      int var2 = -1;
      int var3 = Integer.MAX_VALUE;

      for(int var4 = 0; var4 < var1; ++var4) {
         if(this.trackPositions[var4] >= 0 && this.trackLengths[var4] < var3) {
            var2 = var4;
            var3 = this.trackLengths[var4];
         }
      }

      return var2;
   }

   @ObfuscatedName("as")
   boolean isDone_base() {
      int var1 = this.trackPositions.length;

      for(int var2 = 0; var2 < var1; ++var2) {
         if(this.trackPositions[var2] >= 0) {
            return false;
         }
      }

      return true;
   }

   @ObfuscatedName("ay")
   void reset_base(long var1) {
      this.field3463 = var1;
      int var3 = this.trackPositions.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         this.trackLengths[var4] = 0;
         this.field3462[var4] = 0;
         this.buffer.offset = this.trackStarts[var4];
         this.readTrackLength_base(var4);
         this.trackPositions[var4] = this.buffer.offset;
      }

   }
}
