package mapped;

import net.runelite.api.DialogOption;
import net.runelite.api.events.DialogProcessed;
import net.runelite.api.events.DynamicObjectAnimationChanged;
import net.runelite.api.events.PostAnimation;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSDynamicObject;
import net.runelite.rs.api.RSRenderable;
import net.runelite.rs.api.RSSequenceDefinition;

@ObfuscatedName("di")
public class DynamicObject extends Renderable implements RSDynamicObject {
   @ObfuscatedName("at")
   @ObfuscatedGetter(
      intValue = 397033549
   )
   int id;
   @ObfuscatedName("ah")
   @ObfuscatedGetter(
      intValue = 907568557
   )
   int type;
   @ObfuscatedName("ar")
   @ObfuscatedGetter(
      intValue = 1972714689
   )
   int orientation;
   @ObfuscatedName("ao")
   @ObfuscatedGetter(
      intValue = -1138644923
   )
   int plane;
   @ObfuscatedName("ab")
   @ObfuscatedGetter(
      intValue = -267615049
   )
   int x;
   @ObfuscatedName("au")
   @ObfuscatedGetter(
      intValue = -1043709331
   )
   int y;
   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "Lhx;"
   )
   SequenceDefinition sequenceDefinition;
   @ObfuscatedName("ac")
   @ObfuscatedGetter(
      intValue = -2062693637
   )
   int frame;
   @ObfuscatedName("al")
   @ObfuscatedGetter(
      intValue = 1708122471
   )
   int cycleStart;

   @ObfuscatedSignature(
      descriptor = "(IIIIIIIZLkt;)V"
   )
   DynamicObject(int var1, int var2, int var3, int var4, int var5, int var6, int var7, boolean var8, Renderable var9) {
      this.id = var1;
      this.type = var2;
      this.orientation = var3;
      this.plane = var4;
      this.x = var5;
      this.y = var6;
      if(var7 != -1) {
         this.sequenceDefinition = method2245(var7);
         this.frame = 0;
         this.cycleStart = Client.cycle - 1;
         this.onAnimCycleCountChanged(-1);
         if(this.sequenceDefinition.restartMode == 0 && var9 != null && var9 instanceof DynamicObject) {
            DynamicObject var10 = (DynamicObject)var9;
            if(var10.sequenceDefinition == this.sequenceDefinition) {
               this.frame = var10.frame;
               this.cycleStart = var10.cycleStart;
               this.onAnimCycleCountChanged(-1);
               this.rl$init(var1, var2, var3, var4, var5, var6, var7, var8, var9);
               return;
            }
         }

         if(var8 && this.sequenceDefinition.frameCount != -1) {
            if(!this.sequenceDefinition.isCachedModelIdSet_base()) {
               this.frame = (int)(Math.random() * (double)this.sequenceDefinition.frameIds.length);
               this.cycleStart -= (int)(Math.random() * (double)this.sequenceDefinition.frameLengths[this.frame]);
               this.onAnimCycleCountChanged(-1);
            } else {
               this.frame = (int)(Math.random() * (double)this.sequenceDefinition.method4064_base());
            }
         }
      }

      this.rl$init(var1, var2, var3, var4, var5, var6, var7, var8, var9);
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(B)Lka;",
      garbageValue = "1"
   )
   protected final Model vmethod5504() {
      boolean var7 = false;

      Model var2;
      try {
         var7 = true;
         int var1 = this.getAnimFrame();
         if(var1 < 0) {
            this.setAnimFrame((var1 ^ Integer.MIN_VALUE) & 65535);
         }

         var2 = this.copy$getModel((byte)1);
         var7 = false;
      } finally {
         if(var7) {
            int var5 = this.getAnimFrame();
            if(var5 < 0) {
               this.setAnimFrame((var5 ^ Integer.MIN_VALUE) & 65535);
            }

         }
      }

      int var3 = this.getAnimFrame();
      if(var3 < 0) {
         this.setAnimFrame((var3 ^ Integer.MIN_VALUE) & 65535);
      }

      return (Model)var2;
   }

   public RSSequenceDefinition getAnimation() {
      return this.sequenceDefinition;
   }

   public int getAnimFrame() {
      return this.frame;
   }

   public int getId() {
      return this.id;
   }

   @ObfuscatedSignature(
      descriptor = "(B)Lka;",
      garbageValue = "1"
   )
   public final Model copy$getModel(byte var1) {
      int var3;
      if(this.sequenceDefinition != null) {
         int var2 = Client.cycle - this.cycleStart;
         if(var2 > 100 && this.sequenceDefinition.frameCount > 0) {
            var2 = 100;
         }

         if(this.sequenceDefinition.isCachedModelIdSet_base()) {
            var3 = this.sequenceDefinition.method4064_base();
            this.frame += var2;
            var2 = 0;
            if(this.frame >= var3) {
               this.frame = var3 - this.sequenceDefinition.frameCount;
               if(this.frame < 0 || this.frame > var3) {
                  this.sequenceDefinition = null;
               }
            }
         } else {
            label83: {
               do {
                  do {
                     if(var2 <= this.sequenceDefinition.frameLengths[this.frame]) {
                        break label83;
                     }

                     var2 -= this.sequenceDefinition.frameLengths[this.frame];
                     ++this.frame;
                  } while(this.frame < this.sequenceDefinition.frameIds.length);

                  this.frame -= this.sequenceDefinition.frameCount;
               } while(this.frame >= 0 && this.frame < this.sequenceDefinition.frameIds.length);

               this.sequenceDefinition = null;
            }
         }

         this.cycleStart = Client.cycle - var2;
         this.onAnimCycleCountChanged(-1);
      }

      ObjectComposition var13 = WorldMapSection2.method4515(this.id);
      if(var13.transforms != null) {
         var13 = var13.method3907();
      }

      if(var13 == null) {
         return null;
      } else {
         int var4;
         if(this.orientation != 1 && this.orientation != 3) {
            var3 = var13.sizeX;
            var4 = var13.sizeY;
         } else {
            var3 = var13.sizeY;
            var4 = var13.sizeX;
         }

         int var5 = (var3 >> 1) + this.x;
         int var6 = (var3 + 1 >> 1) + this.x;
         int var7 = (var4 >> 1) + this.y;
         int var8 = (var4 + 1 >> 1) + this.y;
         int[][] var9 = Tiles.Tiles_heights[this.plane];
         int var10 = var9[var6][var8] + var9[var6][var7] + var9[var5][var7] + var9[var5][var8] >> 2;
         int var11 = (this.x << 7) + (var3 << 6);
         int var12 = (this.y << 7) + (var4 << 6);
         return var13.method3896(this.type, this.orientation, var9, var11, var10, var12, this.sequenceDefinition, this.frame);
      }
   }

   public void setAnimFrame(int var1) {
      this.frame = var1;
   }

   public int getAnimCycle() {
      return this.cycleStart;
   }

   public void rl$init(int var1, int var2, int var3, int var4, int var5, int var6, int var7, boolean var8, RSRenderable var9) {
      if(var7 != -1) {
         DynamicObjectAnimationChanged var10 = new DynamicObjectAnimationChanged();
         var10.setObject(var1);
         var10.setAnimation(var7);
         SpriteMask.client.getCallbacks().post(var10);
      }

   }

   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-1"
   )
   public void onAnimCycleCountChanged(int var1) {
      if(SpriteMask.client.isInterpolateObjectAnimations() && this.getId() != 26671) {
         int var2 = SpriteMask.client.getGameCycle() - this.getAnimCycle();
         this.setAnimFrame(Integer.MIN_VALUE | var2 << 16 | this.getAnimFrame());
      }

   }

   public int getAnimationID() {
      return (int)(this.getAnimation() == null?-1L:this.getAnimation().getHash());
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(II)Lhx;",
      garbageValue = "824750959"
   )
   public static SequenceDefinition method2245(int var0) {
      SequenceDefinition var10000;
      synchronized(SpriteMask.client.getSequenceDefinitionCache()) {
         SequenceDefinition var2 = SequenceDefinition.copy$SequenceDefinition_get(var0, -1047374686);
         if(var2.getId() == -1) {
            var2.setId(var0);
            SpriteMask.client.getCallbacks().post(new PostAnimation(var2));
         }

         var10000 = var2;
      }

      return (SequenceDefinition)var10000;
   }

   @ObfuscatedName("hn")
   @ObfuscatedSignature(
      descriptor = "(Lhx;IIILdf;I)V",
      garbageValue = "732979691"
   )
   static void method2244(SequenceDefinition var0, int var1, int var2, int var3, Actor var4) {
      if(Client.soundEffectCount < 50) {
         if(var0.soundEffects != null && var1 < var0.soundEffects.length) {
            int var5 = var0.soundEffects[var1] & 15;
            if((var5 <= 0 || WorldMapIcon_1.clientPreferences.method2605_base() != 0) && (var5 != 0 || WorldMapIcon_1.clientPreferences.method2556_base() != 0)) {
               int var6 = var0.soundEffects[var1];
               boolean var9 = var4 == class229.localPlayer;
               if(var6 != 0) {
                  int var10 = var6 & 15;
                  int var11;
                  int var12;
                  if(var10 == 0) {
                     if(!var9) {
                        return;
                     }

                     Client.soundLocations[Client.soundEffectCount] = 0;
                  } else {
                     var11 = (var2 - 64) / 128;
                     var12 = (var3 - 64) / 128;
                     Client.soundLocations[Client.soundEffectCount] = var10 + (var12 << 8) + (var11 << 16);
                  }

                  var11 = var6 >> 8;
                  var12 = var6 >> 4 & 7;
                  Client.soundEffectIds[Client.soundEffectCount] = var11;
                  Client.queuedSoundEffectLoops[Client.soundEffectCount] = var12;
                  Client.queuedSoundEffectDelays[Client.soundEffectCount] = 0;
                  Client.soundEffects[Client.soundEffectCount] = null;
                  ++Client.soundEffectCount;
                  Client.queuedSoundEffectCountChanged(-1);
               }

            }
         }
      }
   }

   public static void onDialogProcessed(int var0, int var1) {
      DialogOption var2 = DialogOption.of(var0, var1);
      if(var2 != null) {
         SpriteMask.client.getCallbacks().post(new DialogProcessed(var2));
      } else {
         SpriteMask.client.getLogger().debug("Unknown or unmapped dialog option for widgetUid: {} and menuIndex {}", Integer.valueOf(var0), Integer.valueOf(var1));
      }

   }
}
