package mapped;

import java.util.zip.CRC32;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSArchive;
import net.runelite.rs.api.RSArchiveDisk;
import net.runelite.rs.api.RSJagNetThread;

@ObfuscatedName("oh")
public class Archive extends AbstractArchive implements RSArchive {
   @ObfuscatedName("av")
   static CRC32 Archive_crc;
   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "Lrg;"
   )
   ArchiveDisk archiveDisk;
   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "Lrg;"
   )
   ArchiveDisk masterDisk;
   @ObfuscatedName("ac")
   @ObfuscatedGetter(
      intValue = -1859087545
   )
   int index;
   @ObfuscatedName("al")
   @ObfuscatedGetter(
      intValue = 1884228769
   )
   volatile int field4280;
   @ObfuscatedName("az")
   boolean field4289;
   @ObfuscatedName("ap")
   volatile boolean[] validGroups;
   @ObfuscatedName("ax")
   @ObfuscatedGetter(
      intValue = -881107247
   )
   int indexCrc;
   @ObfuscatedName("as")
   @ObfuscatedGetter(
      intValue = 1090956953
   )
   int indexVersion;
   @ObfuscatedName("ay")
   @ObfuscatedGetter(
      intValue = 1160325827
   )
   int field4288;
   @ObfuscatedName("ak")
   boolean field4283;
   @ObfuscatedName("aj")
   boolean field4290;
   @ObfuscatedName("am")
   @ObfuscatedSignature(
      descriptor = "Loz;"
   )
   JagNetThread field4276;

   static {
      Archive_crc = new CRC32();
   }

   @ObfuscatedSignature(
      descriptor = "(Lrg;Lrg;Loz;IZZZZZ)V"
   )
   public Archive(ArchiveDisk var1, ArchiveDisk var2, JagNetThread var3, int var4, boolean var5, boolean var6, boolean var7, boolean var8, boolean var9) {
      super(var5, var6);
      this.field4280 = 0;
      this.field4289 = false;
      this.field4288 = -1;
      this.field4283 = false;
      this.field4290 = false;
      this.archiveDisk = var1;
      this.masterDisk = var2;
      this.index = var4;
      this.field4289 = var7;
      this.field4283 = var8;
      this.field4290 = var9;
      this.field4276 = var3;
      this.field4276.method7049(this, this.index);
      this.rl$init(var1, var2, var3, var4, var5, var6, var7, var8, var9);
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(I)Z",
      garbageValue = "2141667286"
   )
   public boolean method6898_base() {
      return this.field4280 == 1;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(I)I",
      garbageValue = "-2079566791"
   )
   public int percentage_base() {
      if(this.field4280 == 1 || this.field4283 && this.field4280 == 2) {
         return 100;
      } else if(super.groups != null) {
         return 99;
      } else {
         int var1 = this.field4276.method7051_base(255, this.index);
         if(var1 >= 100) {
            var1 = 99;
         }

         return var1;
      }
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "606874642"
   )
   void loadRegionFromGroup_base(int var1) {
      this.field4276.method7050_base(this.index, var1);
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "-412384319"
   )
   void loadGroup_base(int var1) {
      if(this.archiveDisk != null && this.validGroups != null && this.validGroups[var1]) {
         ArchiveDisk var2 = this.archiveDisk;
         byte[] var4 = null;
         NodeDeque var5 = ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue;
         synchronized(ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue) {
            for(ArchiveDiskAction var6 = (ArchiveDiskAction)ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue.method7265(); var6 != null; var6 = (ArchiveDiskAction)ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue.method7235()) {
               if((long)var1 == var6.key && var2 == var6.archiveDisk && var6.type == 0) {
                  var4 = var6.data;
                  break;
               }
            }
         }

         if(var4 != null) {
            this.method6907(var2, var1, var4, true);
         } else {
            byte[] var9 = var2.read_base(var1);
            this.method6907(var2, var1, var9, true);
         }
      } else {
         this.field4276.method7056(this, this.index, var1, super.groupCrcs[var1], (byte)2, true);
      }

   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "152861270"
   )
   void method6902_base() {
      this.field4280 = 2;
      super.groupIds = new int[0];
      super.groupCrcs = new int[0];
      super.groupVersions = new int[0];
      super.fileCounts = new int[0];
      super.fileIds = new int[0][];
      super.groups = new Object[0];
      super.files = new Object[0][];
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(III)V",
      garbageValue = "949324306"
   )
   void loadIndex_base(int var1, int var2) {
      this.indexCrc = var1;
      this.indexVersion = var2;
      if(this.masterDisk != null) {
         int var3 = this.index;
         ArchiveDisk var4 = this.masterDisk;
         byte[] var6 = null;
         NodeDeque var7 = ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue;
         synchronized(ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue) {
            for(ArchiveDiskAction var8 = (ArchiveDiskAction)ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue.method7265(); var8 != null; var8 = (ArchiveDiskAction)ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue.method7235()) {
               if(var8.key == (long)var3 && var4 == var8.archiveDisk && var8.type == 0) {
                  var6 = var8.data;
                  break;
               }
            }
         }

         if(var6 != null) {
            this.method6907(var4, var3, var6, true);
         } else {
            byte[] var11 = var4.read_base(var3);
            this.method6907(var4, var3, var11, true);
         }
      } else {
         this.field4276.method7056(this, 255, this.index, this.indexCrc, (byte)0, true);
      }

   }

   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "(I[BZZI)V",
      garbageValue = "1611170598"
   )
   void write_base(int var1, byte[] var2, boolean var3, boolean var4) {
      if(var3) {
         if(this.field4280 == 1) {
            throw new RuntimeException();
         }

         if(this.masterDisk != null) {
            CollisionMap.method4381(this.index, var2, this.masterDisk);
         }

         this.decodeIndex_base(var2);
         this.loadAllLocal_base();
      } else {
         var2[var2.length - 2] = (byte)(super.groupVersions[var1] >> 8);
         var2[var2.length - 1] = (byte)super.groupVersions[var1];
         if(this.archiveDisk != null) {
            CollisionMap.method4381(var1, var2, this.archiveDisk);
            this.validGroups[var1] = true;
         }

         if(var4) {
            super.groups[var1] = VarcInt.method3598_base(var2, false);
         }
      }

   }

   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "(Lrg;I[BZB)V",
      garbageValue = "0"
   )
   public void method6907(ArchiveDisk var1, int var2, byte[] var3, boolean var4) {
      int var5;
      if(var1 == this.masterDisk) {
         if(this.field4280 == 1) {
            throw new RuntimeException();
         }

         if(var3 == null) {
            this.field4276.method7056(this, 255, this.index, this.indexCrc, (byte)0, true);
            return;
         }

         Archive_crc.reset();
         Archive_crc.update(var3, 0, var3.length);
         var5 = (int)Archive_crc.getValue();
         if(var5 != this.indexCrc) {
            this.field4276.method7056(this, 255, this.index, this.indexCrc, (byte)0, true);
            return;
         }

         Buffer var9 = new Buffer(class30.decompressBytes_base(var3));
         int var7 = var9.readUnsignedByte_base();
         if(var7 != 5 && var7 != 6) {
            throw new RuntimeException(var7 + "," + this.index + "," + var2);
         }

         int var8 = 0;
         if(var7 >= 6) {
            var8 = var9.readInt_base();
         }

         if(var8 != this.indexVersion) {
            this.field4276.method7056(this, 255, this.index, this.indexCrc, (byte)0, true);
            return;
         }

         this.decodeIndex_base(var3);
         this.loadAllLocal_base();
      } else {
         if(!var4 && var2 == this.field4288) {
            this.field4280 = 1;
         }

         if(var3 == null || var3.length <= 2) {
            this.validGroups[var2] = false;
            if(this.field4289 || var4) {
               this.field4276.method7056(this, this.index, var2, super.groupCrcs[var2], (byte)2, var4);
            }

            return;
         }

         Archive_crc.reset();
         Archive_crc.update(var3, 0, var3.length - 2);
         var5 = (int)Archive_crc.getValue();
         int var6 = ((var3[var3.length - 2] & 255) << 8) + (var3[var3.length - 1] & 255);
         if(var5 != super.groupCrcs[var2] || var6 != super.groupVersions[var2]) {
            this.validGroups[var2] = false;
            if(this.field4289 || var4) {
               this.field4276.method7056(this, this.index, var2, super.groupCrcs[var2], (byte)2, var4);
            }

            return;
         }

         this.validGroups[var2] = true;
         if(var4) {
            super.groups[var2] = VarcInt.method3598_base(var3, false);
         }
      }

   }

   @ObfuscatedName("al")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-65241547"
   )
   void loadAllLocal_base() {
      this.validGroups = new boolean[super.groups.length];

      int var1;
      for(var1 = 0; var1 < this.validGroups.length; ++var1) {
         this.validGroups[var1] = false;
      }

      if(this.archiveDisk == null) {
         this.field4280 = 1;
      } else {
         this.field4288 = -1;

         for(var1 = 0; var1 < this.validGroups.length; ++var1) {
            if(super.fileCounts[var1] > 0) {
               ArchiveDisk var2 = this.archiveDisk;
               ArchiveDiskAction var4 = new ArchiveDiskAction();
               var4.type = 1;
               var4.key = (long)var1;
               var4.archiveDisk = var2;
               var4.archive = this;
               NodeDeque var5 = ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue;
               synchronized(ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue) {
                  ArchiveDiskActionHandler.ArchiveDiskActionHandler_requestQueue.method7233(var4);
               }

               Object var10 = ArchiveDiskActionHandler.field4272;
               synchronized(ArchiveDiskActionHandler.field4272) {
                  if(ArchiveDiskActionHandler.field4271 == 0) {
                     ArchiveDiskActionHandler.ArchiveDiskActionHandler_thread = new Thread(new ArchiveDiskActionHandler());
                     ArchiveDiskActionHandler.ArchiveDiskActionHandler_thread.setDaemon(true);
                     ArchiveDiskActionHandler.ArchiveDiskActionHandler_thread.start();
                     ArchiveDiskActionHandler.ArchiveDiskActionHandler_thread.setPriority(5);
                  }

                  ArchiveDiskActionHandler.field4271 = 600;
                  ArchiveDiskActionHandler.field4274 = false;
               }

               this.field4288 = var1;
            }
         }

         if(this.field4288 == -1) {
            this.field4280 = 1;
         }

      }
   }

   @ObfuscatedName("az")
   @ObfuscatedSignature(
      descriptor = "(II)I",
      garbageValue = "-788412100"
   )
   int groupLoadPercent_base(int var1) {
      return super.groups[var1] != null?100:(this.validGroups[var1]?100:this.field4276.method7051_base(this.index, var1));
   }

   @ObfuscatedName("ap")
   @ObfuscatedSignature(
      descriptor = "(II)Z",
      garbageValue = "1250672154"
   )
   public boolean method6908_base(int var1) {
      return this.validGroups[var1];
   }

   @ObfuscatedName("av")
   @ObfuscatedSignature(
      descriptor = "(IB)Z",
      garbageValue = "48"
   )
   public boolean method6909_base(int var1) {
      return this.getGroupFileIds_base(var1) != null;
   }

   @ObfuscatedName("ax")
   @ObfuscatedSignature(
      descriptor = "(I)I",
      garbageValue = "939033209"
   )
   public int loadPercent_base() {
      int var1 = 0;
      int var2 = 0;

      int var3;
      for(var3 = 0; var3 < super.groups.length; ++var3) {
         if(super.fileCounts[var3] > 0) {
            var1 += 100;
            var2 += this.groupLoadPercent_base(var3);
         }
      }

      if(var1 == 0) {
         return 100;
      } else {
         var3 = var2 * 100 / var1;
         return var3;
      }
   }

   public void rl$init(RSArchiveDisk var1, RSArchiveDisk var2, RSJagNetThread var3, int var4, boolean var5, boolean var6, boolean var7, boolean var8, boolean var9) {
      if(var4 >= 0 && var4 < 21) {
         Client.archives[var4] = this;
      }

   }

   public int getIndex() {
      return this.index;
   }
}
