package mapped;

import java.util.ArrayList;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSFloorOverlayDefinition;
import net.runelite.rs.api.RSInterpreter;

@ObfuscatedName("cc")
public class Interpreter implements RSInterpreter {
   @ObfuscatedName("ao")
   static int[] Interpreter_intLocals;
   @ObfuscatedName("ab")
   static short[][][] Tiles_overlays;
   @ObfuscatedName("au")
   static int[] Interpreter_arrayLengths;
   @ObfuscatedName("aa")
   static int[][] Interpreter_arrays;
   @ObfuscatedName("ac")
   static int[] Interpreter_intStack;
   @ObfuscatedName("az")
   static String[] Interpreter_stringStack;
   @ObfuscatedName("av")
   @ObfuscatedGetter(
      intValue = -1870568525
   )
   static int Interpreter_frameDepth;
   @ObfuscatedName("ax")
   @ObfuscatedSignature(
      descriptor = "[Lcs;"
   )
   static ScriptFrame[] Interpreter_frames;
   @ObfuscatedName("ay")
   @ObfuscatedSignature(
      descriptor = "Lnn;"
   )
   static Widget scriptDotWidget;
   @ObfuscatedName("aq")
   static java.util.Calendar Interpreter_calendar;
   @ObfuscatedName("ai")
   static final String[] Interpreter_MONTHS;
   @ObfuscatedName("ae")
   static boolean field846;
   @ObfuscatedName("an")
   static boolean field842;
   @ObfuscatedName("ag")
   static ArrayList field847;
   @ObfuscatedName("ad")
   @ObfuscatedGetter(
      intValue = -1157651787
   )
   static int field849;
   @ObfuscatedName("bl")
   static final double field852;
   @ObfuscatedName("of")
   @ObfuscatedSignature(
      descriptor = "Lnn;"
   )
   static Widget mousedOverWidgetIf1;

   static {
      Interpreter_arrayLengths = new int[5];
      Interpreter_arrays = new int[5][5000];
      Interpreter_intStack = new int[1000];
      Interpreter_stringStack = new String[1000];
      Interpreter_frameDepth = 0;
      Interpreter_frames = new ScriptFrame[50];
      Interpreter_calendar = java.util.Calendar.getInstance();
      Interpreter_MONTHS = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
      field846 = false;
      field842 = false;
      field847 = new ArrayList();
      field849 = 0;
      field852 = Math.log(2.0D);
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(Luo;II)V",
      garbageValue = "593247130"
   )
   static final void method2009(PacketBuffer var0, int var1) {
      int var2 = var0.offset;
      Players.Players_pendingUpdateCount = 0;
      class221.method4304(var0);

      for(int var3 = 0; var3 < Players.Players_pendingUpdateCount; ++var3) {
         int var4 = Players.Players_pendingUpdateIndices[var3];
         Player var5 = Client.players[var4];
         int var6 = var0.readUnsignedByte_base();
         if((var6 & 128) != 0) {
            var6 += var0.readUnsignedByte_base() << 8;
         }

         if((var6 & 16384) != 0) {
            var6 += var0.readUnsignedByte_base() << 16;
         }

         byte var7 = class228.field2412.field2414;
         if((var6 & 512) != 0) {
            var7 = var0.readByteSub_base();
         }

         if((var6 & 1024) != 0) {
            class228[] var8 = Players.field1356;
            class228[] var10 = new class228[]{class228.field2409, class228.field2412, class228.field2408, class228.field2410};
            var8[var4] = (class228)class12.method170(var10, var0.readByteNeg_base());
         }

         if((var6 & 4) != 0) {
            var5.field1208 = var0.readUnsignedShort_base();
            if(var5.pathLength == 0) {
               var5.orientation = var5.field1208;
               var5.method2480_base();
            }
         }

         if((var6 & 4096) != 0) {
            var5.field1209 = var0.readByteNeg_base();
            var5.field1194 = var0.readByteNeg_base();
            var5.field1224 = var0.readByte_base();
            var5.field1203 = var0.readByteNeg_base();
            var5.spotAnimation = var0.readUnsignedShort_base() + Client.cycle;
            var5.field1187 = var0.readUnsignedShort_base() + Client.cycle;
            var5.field1229 = var0.readUnsignedShort_base();
            if(var5.field1122) {
               var5.field1209 += var5.tileX;
               var5.field1194 += var5.tileY;
               var5.field1224 += var5.tileX;
               var5.field1203 += var5.tileY;
               var5.pathLength = 0;
            } else {
               var5.field1209 += var5.pathX[0];
               var5.field1194 += var5.pathY[0];
               var5.field1224 += var5.pathX[0];
               var5.field1203 += var5.pathY[0];
               var5.pathLength = 1;
            }

            var5.field1220 = 0;
         }

         int var23;
         if((var6 & 8192) != 0) {
            for(var23 = 0; var23 < 3; ++var23) {
               var5.actions[var23] = var0.readStringCp1252NullTerminated_base();
            }
         }

         int var9;
         int var13;
         int var14;
         int var24;
         if((var6 & 2048) != 0) {
            var23 = var0.readUnsignedShortAddLE_base();
            var9 = var23 >> 8;
            var24 = var9 >= 13 && var9 <= 20?var9 - 12:0;
            PlayerType var11 = (PlayerType)class12.method170(class320.method6191(), var0.readUnsignedByteAdd_base());
            boolean var12 = var0.readUnsignedByte_base() == 1;
            var13 = var0.readUnsignedByte_base();
            var14 = var0.offset;
            if(var5.username != null && var5.appearance != null) {
               boolean var15 = false;
               if(var11.isUser && class12.friendSystem.method1908(var5.username)) {
                  var15 = true;
               }

               if(!var15 && Client.field736 == 0 && !var5.isHidden) {
                  Players.field1367.offset = 0;
                  var0.method9321_base(Players.field1367.array, 0, var13);
                  Players.field1367.offset = 0;
                  String var16 = AbstractFont.escapeBrackets_base(class20.method303_base(class128.method3076(Players.field1367)));
                  var5.overheadText = var16.trim();
                  var5.overheadTextChanged(-1);
                  var5.field1196 = var23 >> 8;
                  var5.field1197 = var23 & 255;
                  var5.overheadTextCyclesRemaining = 150;
                  byte[] var17 = null;
                  if(var24 > 0 && var24 <= 8) {
                     var17 = new byte[var24];

                     for(int var18 = 0; var18 < var24; ++var18) {
                        var17[var18] = var0.readByte_base();
                     }
                  }

                  byte[] var20 = var17;
                  int[] var19;
                  if(var17 != null && var17.length != 0 && var17.length <= 8) {
                     int[] var29 = new int[var17.length];
                     int var22 = 0;

                     while(true) {
                        if(var22 >= var20.length) {
                           var19 = var29;
                           break;
                        }

                        if(var20[var22] < 0 || var20[var22] > class550.field5294.length) {
                           var19 = null;
                           break;
                        }

                        var29[var22] = class550.field5294[var20[var22]];
                        ++var22;
                     }
                  } else {
                     var19 = null;
                  }

                  var5.field1236 = var19;
                  var5.field1206 = var12;
                  var5.field1174 = var5 != class229.localPlayer && var11.isUser && !Client.field694.isEmpty() && var16.toLowerCase().indexOf(Client.field694) == -1;
                  int var21;
                  if(var11.isPrivileged) {
                     var21 = var12?91:1;
                  } else {
                     var21 = var12?90:2;
                  }

                  if(var11.modIcon != -1) {
                     GrandExchangeOfferUnitPriceComparator.addGameMessage_base(var21, FloorUnderlayDefinition.method3799_base(var11.modIcon) + var5.username.getName_base(), var16);
                  } else {
                     GrandExchangeOfferUnitPriceComparator.addGameMessage_base(var21, var5.username.getName_base(), var16);
                  }
               }
            }

            var0.offset = var24 + var13 + var14;
         }

         int var25;
         int var32;
         if((var6 & 65536) != 0) {
            var23 = var0.readUnsignedByteSub_base();

            for(var9 = 0; var9 < var23; ++var9) {
               var24 = var0.readUnsignedByteNeg_base();
               var25 = var0.readUnsignedShort_base();
               var32 = var0.readUnsignedIntIME_base();
               var5.updateSpotAnimation_base(var24, var25, var32 >> 16, var32 & 65535);
            }
         }

         if((var6 & 32) != 0) {
            var5.overheadText = var0.readStringCp1252NullTerminated_base();
            var5.overheadTextChanged(-1);
            if(var5.overheadText.charAt(0) == '~') {
               var5.overheadText = var5.overheadText.substring(1);
               var5.overheadTextChanged(-1);
               GrandExchangeOfferUnitPriceComparator.addGameMessage_base(2, var5.username.getName_base(), var5.overheadText);
            } else if(var5 == class229.localPlayer) {
               GrandExchangeOfferUnitPriceComparator.addGameMessage_base(2, var5.username.getName_base(), var5.overheadText);
            }

            var5.field1206 = false;
            var5.field1196 = 0;
            var5.field1197 = 0;
            var5.overheadTextCyclesRemaining = 150;
         }

         int var34;
         if((var6 & 16) != 0) {
            var23 = var0.readUnsignedByteAdd_base();
            if(var23 > 0) {
               for(var9 = 0; var9 < var23; ++var9) {
                  var25 = -1;
                  var32 = -1;
                  var13 = -1;
                  var24 = var0.readUShortSmart_base();
                  if(var24 == 32767) {
                     var24 = var0.readUShortSmart_base();
                     var32 = var0.readUShortSmart_base();
                     var25 = var0.readUShortSmart_base();
                     var13 = var0.readUShortSmart_base();
                  } else if(var24 != 32766) {
                     var32 = var0.readUShortSmart_base();
                  } else {
                     var24 = -1;
                  }

                  var14 = var0.readUShortSmart_base();
                  var5.addHitSplat_base(var24, var32, var25, var13, Client.cycle, var14);
               }
            }

            var9 = var0.readUnsignedByte_base();
            if(var9 > 0) {
               for(var24 = 0; var24 < var9; ++var24) {
                  var25 = var0.readUShortSmart_base();
                  var32 = var0.readUShortSmart_base();
                  if(var32 != 32767) {
                     var13 = var0.readUShortSmart_base();
                     var14 = var0.readUnsignedByteAdd_base();
                     var34 = var32 > 0?var0.readUnsignedByteSub_base():var14;
                     var5.addHealthBar_base(var25, Client.cycle, var32, var13, var14, var34);
                  } else {
                     var5.removeHealthBar_base(var25);
                  }
               }
            }
         }

         if((var6 & 2) != 0) {
            var23 = var0.readUnsignedByteAdd_base();
            byte[] var26 = new byte[var23];
            Buffer var31 = new Buffer(var26);
            var0.method9375_base(var26, 0, var23);
            Players.field1357[var4] = var31;
            var5.method2387(var31);
         }

         if((var6 & 1) != 0) {
            var23 = var0.readUnsignedShortAddLE_base();
            if(var23 == 65535) {
               var23 = -1;
            }

            var9 = var0.readUnsignedByteNeg_base();
            WorldMapLabelSize.method4493(var5, var23, var9);
         }

         if((var6 & 64) != 0) {
            var5.targetIndex = var0.readUnsignedShort_base();
            var5.interactingChanged(-1);
            var5.targetIndex += var0.readUnsignedByteSub_base() << 16;
            var5.interactingChanged(-1);
            var23 = 16777215;
            if(var23 == var5.targetIndex) {
               var5.targetIndex = -1;
               var5.interactingChanged(-1);
            }
         }

         if((var6 & 8) != 0) {
            var23 = var0.readUnsignedShortLE_base();
            PlayerType var35 = (PlayerType)class12.method170(class320.method6191(), var0.readUnsignedByteNeg_base());
            boolean var36 = var0.readUnsignedByteAdd_base() == 1;
            var25 = var0.readUnsignedByte_base();
            var32 = var0.offset;
            if(var5.username != null && var5.appearance != null) {
               boolean var33 = false;
               if(var35.isUser && class12.friendSystem.method1908(var5.username)) {
                  var33 = true;
               }

               if(!var33 && Client.field736 == 0 && !var5.isHidden) {
                  Players.field1367.offset = 0;
                  var0.readBytes_base(Players.field1367.array, 0, var25);
                  Players.field1367.offset = 0;
                  String var27 = AbstractFont.escapeBrackets_base(class20.method303_base(class128.method3076(Players.field1367)));
                  var5.overheadText = var27.trim();
                  var5.overheadTextChanged(-1);
                  var5.field1196 = var23 >> 8;
                  var5.field1197 = var23 & 255;
                  var5.overheadTextCyclesRemaining = 150;
                  var5.field1236 = null;
                  var5.field1206 = var36;
                  var5.field1174 = var5 != class229.localPlayer && var35.isUser && !Client.field694.isEmpty() && var27.toLowerCase().indexOf(Client.field694) == -1;
                  if(var35.isPrivileged) {
                     var34 = var36?91:1;
                  } else {
                     var34 = var36?90:2;
                  }

                  if(var35.modIcon != -1) {
                     GrandExchangeOfferUnitPriceComparator.addGameMessage_base(var34, FloorUnderlayDefinition.method3799_base(var35.modIcon) + var5.username.getName_base(), var27);
                  } else {
                     GrandExchangeOfferUnitPriceComparator.addGameMessage_base(var34, var5.username.getName_base(), var27);
                  }
               }
            }

            var0.offset = var25 + var32;
         }

         if((var6 & 256) != 0) {
            var5.field1232 = Client.cycle + var0.readUnsignedShortLE_base();
            var5.field1215 = Client.cycle + var0.readUnsignedShortAddLE_base();
            var5.field1234 = var0.readByteAdd_base();
            var5.field1235 = var0.readByteNeg_base();
            var5.field1171 = var0.readByteSub_base();
            var5.field1237 = (byte)var0.readUnsignedByteSub_base();
         }

         if(var5.field1122) {
            if(var7 == 127) {
               var5.resetPath_base(var5.tileX, var5.tileY);
            } else {
               class228 var30;
               if(var7 != class228.field2412.field2414) {
                  class228[] var37 = new class228[]{class228.field2409, class228.field2412, class228.field2408, class228.field2410};
                  var30 = (class228)class12.method170(var37, var7);
               } else {
                  var30 = Players.field1356[var4];
               }

               var5.method2398_base(var5.tileX, var5.tileY, var30);
            }
         }
      }

      if(var0.offset - var2 != var1) {
         throw new RuntimeException(var0.offset - var2 + " " + var1);
      }
   }

   @ObfuscatedName("ap")
   @ObfuscatedSignature(
      descriptor = "(IIIIIILkf;Liw;B)V",
      garbageValue = "-60"
   )
   static final void method2123(int var0, int var1, int var2, int var3, int var4, int var5, Scene var6, CollisionMap var7) {
      boolean var8 = false;
      byte var9 = SpriteMask.client.getTileSettings()[var0][var1][var2];
      if(SpriteMask.client.isLowMemory()) {
         byte[] var10 = SpriteMask.client.getTileSettings()[var0][var1];
         var10[var2] &= -17;
         if(var5 == 22) {
            int var11 = SpriteMask.client.getTileOverlays()[var0][var1][var2] & 255;
            if(var11 > 0) {
               RSFloorOverlayDefinition var12 = Client.loadFloorOverlay(var11 - 1);
               if(var12.getTexture() < 0 && var12.getPrimaryRgb() == 16711935) {
                  SpriteMask.client.setLowMemory(false);
                  var8 = true;
               }
            }
         }
      }

      Client.copy$addObjects(var0, var1, var2, var3, var4, var5, var6, var7, (byte)-60);
      SpriteMask.client.getTileSettings()[var0][var1][var2] = var9;
      if(var8) {
         SpriteMask.client.setLowMemory(true);
      }

   }
}
