package mapped;

import java.io.EOFException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import net.runelite.api.events.VarClientIntChanged;
import net.runelite.api.events.VarClientStrChanged;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSVarcs;

@ObfuscatedName("em")
public class Varcs implements RSVarcs {
   @ObfuscatedName("jw")
   static int[][] xteaKeys;
   @ObfuscatedName("lf")
   @ObfuscatedGetter(
      intValue = -1204156157
   )
   static int cameraPitch;
   @ObfuscatedName("ar")
   boolean[] intsPersistence;
   @ObfuscatedName("ao")
   Map map;
   /** @deprecated */
   @Deprecated
   @ObfuscatedName("ab")
   String[] strings;
   @ObfuscatedName("au")
   boolean unwrittenChanges;
   @ObfuscatedName("aa")
   @ObfuscatedGetter(
      longValue = -1753585760681832167L
   )
   long field1393;

   Varcs() {
      this.unwrittenChanges = false;
      int var1 = class344.archive2.getGroupFileCount_base(19);
      this.map = new HashMap();
      this.intsPersistence = new boolean[var1];

      int var2;
      for(var2 = 0; var2 < var1; ++var2) {
         VarcInt var3 = class140.method3195(var2);
         this.intsPersistence[var2] = var3.persist;
      }

      var2 = 0;
      if(class344.archive2.method6909_base(15)) {
         var2 = class344.archive2.getGroupFileCount_base(15);
      }

      this.strings = new String[var2];
      this.read_base();
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(III)V",
      garbageValue = "-1897497270"
   )
   void setInt_base(int var1, int var2) {
      this.map.put(Integer.valueOf(var1), Integer.valueOf(var2));
      if(this.intsPersistence[var1]) {
         this.unwrittenChanges = true;
      }

      this.onVarCIntChanged(var1, var2);
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(II)I",
      garbageValue = "-1991515717"
   )
   int getInt_base(int var1) {
      Object var2 = this.map.get(Integer.valueOf(var1));
      return var2 instanceof Integer?((Integer)var2).intValue():-1;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(ILjava/lang/String;B)V",
      garbageValue = "-59"
   )
   void setString_base(int var1, String var2) {
      this.map.put(Integer.valueOf(var1), var2);
      this.onVarCStrChanged(var1, var2);
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(IB)Ljava/lang/String;",
      garbageValue = "32"
   )
   String getString_base(int var1) {
      Object var2 = this.map.get(Integer.valueOf(var1));
      return var2 instanceof String?(String)var2:"";
   }

   /** @deprecated */
   @Deprecated
   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(ILjava/lang/String;I)V",
      garbageValue = "-1603974039"
   )
   void setStringOld_base(int var1, String var2) {
      this.strings[var1] = var2;
   }

   /** @deprecated */
   @Deprecated
   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(IB)Ljava/lang/String;",
      garbageValue = "0"
   )
   String getStringOld_base(int var1) {
      return this.strings[var1];
   }

   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "19136992"
   )
   void clearTransient_base() {
      int var1;
      for(var1 = 0; var1 < this.intsPersistence.length; ++var1) {
         if(!this.intsPersistence[var1]) {
            this.map.remove(Integer.valueOf(var1));
         }
      }

      for(var1 = 0; var1 < this.strings.length; ++var1) {
         this.strings[var1] = null;
      }

   }

   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "(ZI)Lto;",
      garbageValue = "-1859436086"
   )
   AccessFile method2805(boolean var1) {
      return class131.method3099("2", Messages.field1401.name, var1);
   }

   @ObfuscatedName("al")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-2104867526"
   )
   void write_base() {
      AccessFile var1 = this.method2805(true);

      try {
         int var2 = 3;
         int var3 = 0;
         Iterator var4 = this.map.entrySet().iterator();

         while(var4.hasNext()) {
            Entry var5 = (Entry)var4.next();
            int var6 = ((Integer)var5.getKey()).intValue();
            if(this.intsPersistence[var6]) {
               Object var7 = var5.getValue();
               var2 += 3;
               if(var7 instanceof Integer) {
                  var2 += 4;
               } else if(var7 instanceof String) {
                  var2 += ClanChannel.stringCp1252NullTerminatedByteSize_base((String)var7);
               }

               ++var3;
            }
         }

         Buffer var26 = new Buffer(var2);
         var26.writeByte_base(2);
         var26.writeShort_base(var3);
         Iterator var27 = this.map.entrySet().iterator();

         while(var27.hasNext()) {
            Entry var15 = (Entry)var27.next();
            int var16 = ((Integer)var15.getKey()).intValue();
            if(this.intsPersistence[var16]) {
               var26.writeShort_base(var16);
               Object var8 = var15.getValue();
               class514 var9 = class514.method9124_base(var8.getClass());
               var26.writeByte_base(var9.field5063);
               Class var11 = var8.getClass();
               class514 var12 = class514.method9124_base(var11);
               if(var12 == null) {
                  throw new IllegalArgumentException();
               }

               class510 var10 = var12.field5062;
               var10.vmethod9141(var8, var26);
            }
         }

         var1.write_base(var26.array, 0, var26.offset);
      } catch (Exception var24) {
         ;
      } finally {
         try {
            var1.close_base();
         } catch (Exception var23) {
            ;
         }

      }

      this.unwrittenChanges = false;
      this.field1393 = SpotAnimationDefinition.method3775_base();
   }

   @ObfuscatedName("az")
   @ObfuscatedSignature(
      descriptor = "(B)V",
      garbageValue = "121"
   )
   void read_base() {
      AccessFile var1 = this.method2805(false);

      try {
         byte[] var2 = new byte[(int)var1.length_base()];

         int var4;
         for(int var3 = 0; var3 < var2.length; var3 += var4) {
            var4 = var1.read_base(var2, var3, var2.length - var3);
            if(var4 == -1) {
               throw new EOFException();
            }
         }

         Buffer var14 = new Buffer(var2);
         if(var14.array.length - var14.offset < 1) {
            return;
         }

         int var15 = var14.readUnsignedByte_base();
         if(var15 < 0 || var15 > 2) {
            return;
         }

         int var7;
         int var8;
         int var9;
         int var16;
         if(var15 >= 2) {
            var16 = var14.readUnsignedShort_base();

            for(var7 = 0; var7 < var16; ++var7) {
               var8 = var14.readUnsignedShort_base();
               var9 = var14.readUnsignedByte_base();
               class514 var10 = (class514)class12.method170(class514.method9122_base(), var9);
               Object var11 = var10.method9126(var14);
               if(var8 >= 0 && var8 < this.intsPersistence.length && this.intsPersistence[var8]) {
                  this.map.put(Integer.valueOf(var8), var11);
               }
            }
         } else {
            var16 = var14.readUnsignedShort_base();

            for(var7 = 0; var7 < var16; ++var7) {
               var8 = var14.readUnsignedShort_base();
               var9 = var14.readInt_base();
               if(var8 >= 0 && var8 < this.intsPersistence.length && this.intsPersistence[var8]) {
                  this.map.put(Integer.valueOf(var8), Integer.valueOf(var9));
               }
            }

            var7 = var14.readUnsignedShort_base();

            for(var8 = 0; var8 < var7; ++var8) {
               var14.readUnsignedShort_base();
               var14.readStringCp1252NullTerminated_base();
            }
         }
      } catch (Exception var25) {
         ;
      } finally {
         try {
            var1.close_base();
         } catch (Exception var24) {
            ;
         }

      }

      this.unwrittenChanges = false;
   }

   @ObfuscatedName("ap")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "1055402619"
   )
   void tryWrite_base() {
      if(this.unwrittenChanges && this.field1393 < SpotAnimationDefinition.method3775_base() - 60000L) {
         this.write_base();
      }

   }

   @ObfuscatedName("av")
   @ObfuscatedSignature(
      descriptor = "(B)Z",
      garbageValue = "3"
   )
   boolean hasUnwrittenChanges_base() {
      return this.unwrittenChanges;
   }

   public void onVarCStrChanged(int var1, String var2) {
      SpriteMask.client.getCallbacks().post(new VarClientStrChanged(var1));
   }

   public void onVarCIntChanged(int var1, int var2) {
      SpriteMask.client.getCallbacks().post(new VarClientIntChanged(var1));
   }

   public Map getVarcMap() {
      return this.map;
   }

   @ObfuscatedName("lg")
   @ObfuscatedSignature(
      descriptor = "(IIIIII)V",
      garbageValue = "2070284698"
   )
   static final void drawScrollBar_base(int var0, int var1, int var2, int var3, int var4) {
      class31.scrollBarSprites[0].drawAt_base(var0, var1);
      class31.scrollBarSprites[1].drawAt_base(var0, var3 + var1 - 16);
      Rasterizer2D.Rasterizer2D_fillRectangle_base(var0, var1 + 16, 16, var3 - 32, Client.field565);
      int var5 = var3 * (var3 - 32) / var4;
      if(var5 < 8) {
         var5 = 8;
      }

      int var6 = (var3 - 32 - var5) * var2 / (var4 - var3);
      Rasterizer2D.Rasterizer2D_fillRectangle_base(var0, var6 + var1 + 16, 16, var5, Client.field670);
      Rasterizer2D.Rasterizer2D_drawVerticalLine_base(var0, var6 + var1 + 16, var5, Client.field568);
      Rasterizer2D.Rasterizer2D_drawVerticalLine_base(var0 + 1, var6 + var1 + 16, var5, Client.field568);
      Rasterizer2D.Rasterizer2D_drawHorizontalLine_base(var0, var6 + var1 + 16, 16, Client.field568);
      Rasterizer2D.Rasterizer2D_drawHorizontalLine_base(var0, var6 + var1 + 17, 16, Client.field568);
      Rasterizer2D.Rasterizer2D_drawVerticalLine_base(var0 + 15, var6 + var1 + 16, var5, Client.field567);
      Rasterizer2D.Rasterizer2D_drawVerticalLine_base(var0 + 14, var6 + var1 + 17, var5 - 1, Client.field567);
      Rasterizer2D.Rasterizer2D_drawHorizontalLine_base(var0, var5 + var6 + var1 + 15, 16, Client.field567);
      Rasterizer2D.Rasterizer2D_drawHorizontalLine_base(var0 + 1, var6 + var5 + var1 + 14, 15, Client.field567);
   }
}
