package mapped;

import java.awt.Polygon;
import java.awt.Shape;
import java.util.Iterator;
import net.runelite.api.Perspective;
import net.runelite.api.coords.LocalPoint;
import net.runelite.api.events.NpcChanged;
import net.runelite.api.events.NpcDespawned;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSModel;
import net.runelite.rs.api.RSNPC;
import net.runelite.rs.api.RSNPCComposition;

@ObfuscatedName("dn")
public final class NPC extends Actor implements RSNPC {
   @ObfuscatedName("aa")
   @ObfuscatedGetter(
      intValue = 814526813
   )
   static int field1311;
   @ObfuscatedName("al")
   @ObfuscatedGetter(
      intValue = -2039481379
   )
   static int field1305;
   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "Lhw;"
   )
   NPCComposition definition;
   @ObfuscatedName("ah")
   String field1306;
   @ObfuscatedName("ao")
   @ObfuscatedGetter(
      intValue = 761785473
   )
   int field1308;
   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "Lum;"
   )
   class527 field1309;
   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "Lhf;"
   )
   NpcOverrides modelOverrides;
   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "Lhf;"
   )
   NpcOverrides chatheadOverrides;
   public int npcIndex;

   static {
      field1311 = 1;
      field1305 = 1;
   }

   NPC() {
      this.field1306 = "";
      this.field1308 = 31;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;I)V",
      garbageValue = "-983100224"
   )
   void method2713_base(String var1) {
      this.field1306 = var1 == null?"":var1;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(B)Lka;",
      garbageValue = "1"
   )
   protected final Model vmethod5504() {
      if(!SpriteMask.client.isInterpolateNpcAnimations() || this.getAnimation() == 6566 || this.getAnimation() == 8270 || this.getAnimation() == 8271 || this.getPoseAnimation() == 5583 || this.getId() == 8610 && this.getAnimation() == -1 || this.getId() == 1163 && this.getAnimation() == -1 || this.getId() == 6380 && this.getAnimation() == -1 || this.getId() == 6319 && this.getAnimation() == -1) {
         return (Model)this.copy$getModel((byte)1);
      } else {
         int var1 = this.getActionFrame();
         int var2 = this.getPoseFrame();
         int var3 = this.getSpotAnimFrame();
         boolean var15 = false;

         Model var17;
         try {
            var15 = true;
            this.setActionFrame(Integer.MIN_VALUE | this.getActionFrameCycle() << 16 | var1);
            this.setPoseFrame(Integer.MIN_VALUE | this.getPoseFrameCycle() << 16 | var2);
            this.setSpotAnimFrame(Integer.MIN_VALUE | this.getSpotAnimationFrameCycle() << 16 | var3);
            Iterator var4 = this.getSpotAnims().iterator();

            while(true) {
               if(!var4.hasNext()) {
                  var17 = this.copy$getModel((byte)1);
                  var15 = false;
                  break;
               }

               net.runelite.api.ActorSpotAnim var5 = (net.runelite.api.ActorSpotAnim)var4.next();
               int var6 = var5.getFrame();
               if(var6 != -1) {
                  var5.setFrame(Integer.MIN_VALUE | var5.getCycle() << 16 | var6);
               }
            }
         } finally {
            if(var15) {
               this.setActionFrame(var1);
               this.setPoseFrame(var2);
               this.setSpotAnimFrame(var3);
               Iterator var10 = this.getSpotAnims().iterator();

               while(true) {
                  if(!var10.hasNext()) {
                     ;
                  } else {
                     net.runelite.api.ActorSpotAnim var11 = (net.runelite.api.ActorSpotAnim)var10.next();
                     int var12 = var11.getFrame();
                     if(var12 != -1) {
                        var11.setFrame(var12 & 65535);
                     }
                  }
               }
            }
         }

         this.setActionFrame(var1);
         this.setPoseFrame(var2);
         this.setSpotAnimFrame(var3);
         Iterator var13 = this.getSpotAnims().iterator();

         while(var13.hasNext()) {
            net.runelite.api.ActorSpotAnim var7 = (net.runelite.api.ActorSpotAnim)var13.next();
            int var8 = var7.getFrame();
            if(var8 != -1) {
               var7.setFrame(var8 & 65535);
            }
         }

         return (Model)var17;
      }
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "804243007"
   )
   void method2660_base(int var1) {
      this.field1308 = var1;
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(II)Z",
      garbageValue = "1535698170"
   )
   boolean method2661_base(int var1) {
      return var1 >= 0 && var1 <= 4?(this.field1308 & 1 << var1) != 0:true;
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(B)Ljava/lang/String;",
      garbageValue = "0"
   )
   final String method2662_base() {
      if(!this.field1306.isEmpty()) {
         return this.field1306;
      } else {
         NPCComposition var1 = this.definition;
         if(var1.transforms != null) {
            var1 = var1.method3698();
            if(var1 == null) {
               var1 = this.definition;
            }
         }

         return var1.name;
      }
   }

   @ObfuscatedName("az")
   @ObfuscatedSignature(
      descriptor = "(ILiz;I)V",
      garbageValue = "844868343"
   )
   final void method2664_base(int var1, class228 var2) {
      int var3 = super.pathX[0];
      int var4 = super.pathY[0];
      if(var1 == 0) {
         --var3;
         ++var4;
      }

      if(var1 == 1) {
         ++var4;
      }

      if(var1 == 2) {
         ++var3;
         ++var4;
      }

      if(var1 == 3) {
         --var3;
      }

      if(var1 == 4) {
         ++var3;
      }

      if(var1 == 5) {
         --var3;
         --var4;
      }

      if(var1 == 6) {
         --var4;
      }

      if(var1 == 7) {
         ++var3;
         --var4;
      }

      if(super.sequence != -1 && DynamicObject.method2245(super.sequence).field2205 == 1) {
         super.sequence = -1;
         this.animationChanged(-1);
      }

      if(super.pathLength < 9) {
         ++super.pathLength;
      }

      for(int var5 = super.pathLength; var5 > 0; --var5) {
         super.pathX[var5] = super.pathX[var5 - 1];
         super.pathY[var5] = super.pathY[var5 - 1];
         super.pathTraversed[var5] = super.pathTraversed[var5 - 1];
      }

      super.pathX[0] = var3;
      super.pathY[0] = var4;
      super.pathTraversed[0] = var2;
   }

   @ObfuscatedName("ap")
   @ObfuscatedSignature(
      descriptor = "(IIZB)V",
      garbageValue = "75"
   )
   final void method2702_base(int var1, int var2, boolean var3) {
      if(super.sequence != -1 && DynamicObject.method2245(super.sequence).field2205 == 1) {
         super.sequence = -1;
         this.animationChanged(-1);
      }

      if(!var3) {
         int var4 = var1 - super.pathX[0];
         int var5 = var2 - super.pathY[0];
         if(var4 >= -8 && var4 <= 8 && var5 >= -8 && var5 <= 8) {
            if(super.pathLength < 9) {
               ++super.pathLength;
            }

            for(int var6 = super.pathLength; var6 > 0; --var6) {
               super.pathX[var6] = super.pathX[var6 - 1];
               super.pathY[var6] = super.pathY[var6 - 1];
               super.pathTraversed[var6] = super.pathTraversed[var6 - 1];
            }

            super.pathX[0] = var1;
            super.pathY[0] = var2;
            super.pathTraversed[0] = class228.field2409;
            return;
         }
      }

      super.pathLength = 0;
      super.field1220 = 0;
      super.field1245 = 0;
      super.pathX[0] = var1;
      super.pathY[0] = var2;
      super.x = super.field1198 * 64 + super.pathX[0] * 128;
      super.y = super.field1198 * 64 + super.pathY[0] * 128;
   }

   @ObfuscatedName("av")
   @ObfuscatedSignature(
      descriptor = "(S)[I",
      garbageValue = "-23200"
   )
   int[] method2701_base() {
      return this.field1309 != null?this.field1309.method9531_base():this.definition.method3733_base();
   }

   @ObfuscatedName("ax")
   @ObfuscatedSignature(
      descriptor = "(B)[S",
      garbageValue = "120"
   )
   short[] method2667_base() {
      return this.field1309 != null?this.field1309.method9532_base():this.definition.method3694_base();
   }

   @ObfuscatedName("as")
   @ObfuscatedSignature(
      descriptor = "(IISI)V",
      garbageValue = "-558766672"
   )
   void method2666_base(int var1, int var2, short var3) {
      if(this.field1309 == null) {
         this.field1309 = new class527(this.definition);
      }

      this.field1309.method9533_base(var1, var2, var3);
   }

   @ObfuscatedName("ay")
   @ObfuscatedSignature(
      descriptor = "([I[SI)V",
      garbageValue = "-1790507927"
   )
   void method2669_base(int[] var1, short[] var2) {
      if(this.field1309 == null) {
         this.field1309 = new class527(this.definition);
      }

      this.field1309.method9534_base(var1, var2);
   }

   @ObfuscatedName("ak")
   @ObfuscatedSignature(
      descriptor = "(B)V",
      garbageValue = "0"
   )
   void method2670_base() {
      this.field1309 = null;
   }

   @ObfuscatedName("aj")
   @ObfuscatedSignature(
      descriptor = "(Lhf;I)V",
      garbageValue = "-906762953"
   )
   void method2671(NpcOverrides var1) {
      this.chatheadOverrides = var1;
   }

   @ObfuscatedName("am")
   @ObfuscatedSignature(
      descriptor = "(B)Lhf;",
      garbageValue = "2"
   )
   NpcOverrides method2672() {
      return this.chatheadOverrides;
   }

   @ObfuscatedName("aq")
   @ObfuscatedSignature(
      descriptor = "(Lhf;I)V",
      garbageValue = "-977381781"
   )
   void method2673(NpcOverrides var1) {
      this.modelOverrides = var1;
   }

   @ObfuscatedName("ai")
   @ObfuscatedSignature(
      descriptor = "(I)Z",
      garbageValue = "1221291483"
   )
   final boolean isVisible_base() {
      return this.definition != null;
   }

   @ObfuscatedName("aw")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-434488190"
   )
   void method2674_base() {
      this.chatheadOverrides = null;
   }

   @ObfuscatedName("ae")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-923674269"
   )
   void method2675_base() {
      this.modelOverrides = null;
   }

   public net.runelite.api.NPCComposition getTransformedComposition() {
      RSNPCComposition var1 = this.getComposition();
      if(var1 != null && var1.getConfigs() != null) {
         var1 = var1.transform();
      }

      return var1;
   }

   public void setComposition(RSNPCComposition var1) {
      this.definition = (NPCComposition)var1;
   }

   public RSNPCComposition getComposition() {
      return this.definition;
   }

   public int getId() {
      RSNPCComposition var1 = this.getComposition();
      if(var1 != null && var1.getConfigs() != null) {
         var1 = var1.transform();
      }

      return var1 == null?-1:var1.getId();
   }

   @ObfuscatedSignature(
      descriptor = "(B)Lka;",
      garbageValue = "1"
   )
   public final Model copy$getModel(byte var1) {
      if(this.definition == null) {
         return null;
      } else {
         SequenceDefinition var2 = super.sequence != -1 && super.sequenceDelay == 0?DynamicObject.method2245(super.sequence):null;
         SequenceDefinition var3 = super.movementSequence != -1 && (super.movementSequence != super.idleSequence || var2 == null)?DynamicObject.method2245(super.movementSequence):null;
         Model var4 = null;
         if(this.modelOverrides != null && this.modelOverrides.useLocalPlayer) {
            var4 = class229.localPlayer.appearance.method6322(var2, super.sequenceFrame, var3, super.movementFrame);
         } else {
            var4 = this.definition.method3735(var2, super.sequenceFrame, var3, super.movementFrame, this.modelOverrides);
         }

         if(var4 == null) {
            return null;
         } else {
            var4.calculateBoundsCylinder_base();
            super.defaultHeight = var4.height;
            int var5 = var4.indicesCount;
            var4 = this.method2469(var4);
            if(this.definition.size == 1) {
               var4.isSingleTile = true;
            }

            if(super.field1237 != 0 && Client.cycle >= super.field1232 && Client.cycle < super.field1215) {
               var4.overrideHue = super.field1234;
               var4.overrideSaturation = super.field1235;
               var4.overrideLuminance = super.field1171;
               var4.overrideAmount = super.field1237;
               var4.field3006 = (short)var5;
            } else {
               var4.overrideAmount = 0;
            }

            return var4;
         }
      }
   }

   public void onDefinitionChanged(RSNPCComposition var1) {
      if(var1 == null) {
         SpriteMask.client.getCallbacks().post(new NpcDespawned(this));
      } else if(this.getId() != -1) {
         RSNPCComposition var2 = this.getComposition();
         if(var2 == null) {
            return;
         }

         if(var1.getId() == var2.getId()) {
            return;
         }

         this.setDead(false);
         SpriteMask.client.getCallbacks().postDeferred(new NpcChanged(this, var2));
      }

   }

   public void onDefinitionChanged(net.runelite.api.NPCComposition composition) {
      this.onDefinitionChanged((NPCComposition) composition);
   }

   public String getName() {
      RSNPCComposition var1 = this.getComposition();
      if(var1 != null && var1.getConfigs() != null) {
         var1 = var1.transform();
      }

      return var1 == null?null:var1.getName().replace(' ', ' ');
   }

   public int getCombatLevel() {
      RSNPCComposition var1 = this.getComposition();
      if(var1 != null && var1.getConfigs() != null) {
         var1 = var1.transform();
      }

      return var1 == null?-1:var1.getCombatLevel();
   }

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

   public void setIndex(int var1) {
      this.npcIndex = var1;
   }

   public void setComposition(net.runelite.api.NPCComposition var1) {
      this.setComposition((RSNPCComposition)var1);
   }

   public Polygon getCanvasTilePoly() {
      net.runelite.api.NPCComposition var1 = this.getTransformedComposition();
      if(var1 == null) {
         return null;
      } else {
         int var2 = var1.getSize();
         return Perspective.getCanvasTileAreaPoly(SpriteMask.client, this.getLocalLocation(), var2);
      }
   }

   public Shape getConvexHull() {
      RSModel var1 = this.getModel();
      if(var1 == null) {
         return null;
      } else {
         int var2 = this.getComposition().getSize();
         LocalPoint var3 = new LocalPoint(var2 * 64 - 64 + this.getX(), var2 * 64 - 64 + this.getY());
         int var4 = Perspective.getTileHeight(SpriteMask.client, var3, SpriteMask.client.getPlane());
         return var1.getConvexHull(this.getX(), this.getY(), this.getCurrentOrientation(), var4);
      }
   }

   public net.runelite.api.NpcOverrides getModelOverrides() {
      return this.modelOverrides;
   }

   public net.runelite.api.NpcOverrides getChatheadOverrides() {
      return this.chatheadOverrides;
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/CharSequence;IZB)I",
      garbageValue = "-22"
   )
   public static int method2721_base(CharSequence var0, int var1, boolean var2) {
      if(var1 >= 2 && var1 <= 36) {
         boolean var3 = false;
         boolean var4 = false;
         int var5 = 0;
         int var6 = var0.length();

         for(int var7 = 0; var7 < var6; ++var7) {
            char var8 = var0.charAt(var7);
            if(var7 == 0) {
               if(var8 == '-') {
                  var3 = true;
                  continue;
               }

               if(var8 == '+') {
                  continue;
               }
            }

            int var10;
            if(var8 >= '0' && var8 <= '9') {
               var10 = var8 - '0';
            } else if(var8 >= 'A' && var8 <= 'Z') {
               var10 = var8 - '7';
            } else {
               if(var8 < 'a' || var8 > 'z') {
                  throw new NumberFormatException();
               }

               var10 = var8 - 'W';
            }

            if(var10 >= var1) {
               throw new NumberFormatException();
            }

            if(var3) {
               var10 = -var10;
            }

            int var9 = var5 * var1 + var10;
            if(var9 / var1 != var5) {
               throw new NumberFormatException();
            }

            var5 = var9;
            var4 = true;
         }

         if(!var4) {
            throw new NumberFormatException();
         } else {
            return var5;
         }
      } else {
         throw new IllegalArgumentException("" + var1);
      }
   }

   @ObfuscatedName("aj")
   @ObfuscatedSignature(
      descriptor = "(III)I",
      garbageValue = "-1588641318"
   )
   static final int method2719_base(int var0, int var1) {
      int var2 = var0 + var1 * 57;
      var2 ^= var2 << 13;
      int var3 = (var2 * var2 * 15731 + 789221) * var2 + 1376312589 & Integer.MAX_VALUE;
      return var3 >> 19 & 255;
   }

   @ObfuscatedName("bv")
   @ObfuscatedSignature(
      descriptor = "(Lbk;I)V",
      garbageValue = "2109670231"
   )
   static final void method2720(PcmStream var0) {
      var0.active = false;
      if(var0.sound != null) {
         var0.sound.position = 0;
      }

      for(PcmStream var1 = var0.vmethod6210(); var1 != null; var1 = var0.vmethod6222()) {
         method2720(var1);
      }

   }
}
