package mapped;

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.RSSpotAnimationDefinition;

@ObfuscatedName("ha")
public class SpotAnimationDefinition extends DualNode implements RSSpotAnimationDefinition {
   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "Lol;"
   )
   static AbstractArchive SpotAnimationDefinition_archive;
   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "Lol;"
   )
   static AbstractArchive SpotAnimationDefinition_modelArchive;
   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "Llk;"
   )
   public static EvictingDualNodeHashTable SpotAnimationDefinition_cached;
   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "Llk;"
   )
   public static EvictingDualNodeHashTable SpotAnimationDefinition_cachedModels;
   @ObfuscatedName("gm")
   @ObfuscatedSignature(
      descriptor = "Loh;"
   )
   static Archive field2007;
   @ObfuscatedName("ab")
   @ObfuscatedGetter(
      intValue = 1008653175
   )
   int id;
   @ObfuscatedName("au")
   @ObfuscatedGetter(
      intValue = -1629473129
   )
   int archive;
   @ObfuscatedName("aa")
   @ObfuscatedGetter(
      intValue = -92053015
   )
   public int sequence;
   @ObfuscatedName("ac")
   short[] recolorFrom;
   @ObfuscatedName("al")
   short[] recolorTo;
   @ObfuscatedName("az")
   short[] retextureFrom;
   @ObfuscatedName("ap")
   short[] retextureTo;
   @ObfuscatedName("av")
   @ObfuscatedGetter(
      intValue = 1335565971
   )
   int widthScale;
   @ObfuscatedName("ax")
   @ObfuscatedGetter(
      intValue = 214243153
   )
   int heightScale;
   @ObfuscatedName("as")
   @ObfuscatedGetter(
      intValue = -1513265209
   )
   int orientation;
   @ObfuscatedName("ay")
   @ObfuscatedGetter(
      intValue = 1824679643
   )
   int ambient;
   @ObfuscatedName("ak")
   @ObfuscatedGetter(
      intValue = 1346787261
   )
   int contrast;

   static {
      SpotAnimationDefinition_cached = new EvictingDualNodeHashTable(64);
      SpotAnimationDefinition_cachedModels = new EvictingDualNodeHashTable(30);
   }

   SpotAnimationDefinition() {
      this.sequence = -1;
      this.widthScale = 128;
      this.heightScale = 128;
      this.orientation = 0;
      this.ambient = 0;
      this.contrast = 0;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(Luj;I)V",
      garbageValue = "-1590582359"
   )
   void method3765(Buffer var1) {
      while(true) {
         int var2 = var1.readUnsignedByte_base();
         if(var2 == 0) {
            return;
         }

         this.method3761(var1, var2);
      }
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(Luj;IB)V",
      garbageValue = "-22"
   )
   void method3761(Buffer var1, int var2) {
      if(var2 == 1) {
         this.archive = var1.readUnsignedShort_base();
      } else if(var2 == 2) {
         this.sequence = var1.readUnsignedShort_base();
      } else if(var2 == 4) {
         this.widthScale = var1.readUnsignedShort_base();
      } else if(var2 == 5) {
         this.heightScale = var1.readUnsignedShort_base();
      } else if(var2 == 6) {
         this.orientation = var1.readUnsignedShort_base();
      } else if(var2 == 7) {
         this.ambient = var1.readUnsignedByte_base();
      } else if(var2 == 8) {
         this.contrast = var1.readUnsignedByte_base();
      } else {
         int var3;
         int var4;
         if(var2 == 40) {
            var3 = var1.readUnsignedByte_base();
            this.recolorFrom = new short[var3];
            this.recolorTo = new short[var3];

            for(var4 = 0; var4 < var3; ++var4) {
               this.recolorFrom[var4] = (short)var1.readUnsignedShort_base();
               this.recolorTo[var4] = (short)var1.readUnsignedShort_base();
            }
         } else if(var2 == 41) {
            var3 = var1.readUnsignedByte_base();
            this.retextureFrom = new short[var3];
            this.retextureTo = new short[var3];

            for(var4 = 0; var4 < var3; ++var4) {
               this.retextureFrom[var4] = (short)var1.readUnsignedShort_base();
               this.retextureTo[var4] = (short)var1.readUnsignedShort_base();
            }
         }
      }

   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(IB)Lka;",
      garbageValue = "-10"
   )
   public final Model method3762(int var1) {
      Model var2 = this.method3766();
      Model var3;
      if(this.sequence != -1 && var1 != -1) {
         var3 = DynamicObject.method2245(this.sequence).method4049(var2, var1);
      } else {
         var3 = var2.method5636(true);
      }

      if(this.widthScale != 128 || this.heightScale != 128) {
         var3.scale_base(this.widthScale, this.heightScale, this.widthScale);
      }

      if(this.orientation != 0) {
         if(this.orientation == 90) {
            var3.rotateY90Ccw_base();
         }

         if(this.orientation == 180) {
            var3.rotateY90Ccw_base();
            var3.rotateY90Ccw_base();
         }

         if(this.orientation == 270) {
            var3.rotateY90Ccw_base();
            var3.rotateY90Ccw_base();
            var3.rotateY90Ccw_base();
         }
      }

      return var3;
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(B)Lka;",
      garbageValue = "115"
   )
   public final Model method3766() {
      Model var1 = (Model)SpotAnimationDefinition_cachedModels.get((long)this.id);
      if(var1 == null) {
         ModelData var2 = ModelData.method5100(SpotAnimationDefinition_modelArchive, this.archive, 0);
         if(var2 == null) {
            return null;
         }

         int var3;
         if(this.recolorFrom != null) {
            for(var3 = 0; var3 < this.recolorFrom.length; ++var3) {
               var2.recolor_base(this.recolorFrom[var3], this.recolorTo[var3]);
            }
         }

         if(this.retextureFrom != null) {
            for(var3 = 0; var3 < this.retextureFrom.length; ++var3) {
               var2.retexture_base(this.retextureFrom[var3], this.retextureTo[var3]);
            }
         }

         var1 = var2.method5122(this.ambient + 64, this.contrast + 850, -30, -50, -30);
         SpotAnimationDefinition_cachedModels.put(var1, (long)this.id);
      }

      return var1;
   }

   public int getSequence() {
      return this.sequence;
   }

   public void setRecolorFrom(short[] var1) {
      this.recolorFrom = var1;
   }

   public void setRecolorTo(short[] var1) {
      this.recolorTo = var1;
   }

   public RSModel getModel(int var1) {
      return this.method3762(var1);
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(B)J",
      garbageValue = "43"
   )
   public static final synchronized long method3775_base() {
      long var0 = System.currentTimeMillis();
      if(var0 < class312.field3394) {
         class312.field3395 += class312.field3394 - var0;
      }

      class312.field3394 = var0;
      return class312.field3395 + var0;
   }
}
