package mapped;

import net.runelite.api.IndexDataBase;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSTexture;
import net.runelite.rs.api.RSTextureProvider;

@ObfuscatedName("kg")
public class TextureProvider implements TextureLoader, RSTextureProvider {
   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "[Lks;"
   )
   Texture[] textures;
   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "Lon;"
   )
   NodeDeque deque;
   @ObfuscatedName("ar")
   @ObfuscatedGetter(
      intValue = 2010680135
   )
   int capacity;
   @ObfuscatedName("ao")
   @ObfuscatedGetter(
      intValue = -1765545141
   )
   int remaining;
   @ObfuscatedName("ab")
   double brightness;
   @ObfuscatedName("au")
   @ObfuscatedGetter(
      intValue = -414641329
   )
   int textureSize;
   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "Lol;"
   )
   AbstractArchive archive;

   @ObfuscatedSignature(
      descriptor = "(Lol;Lol;IDI)V"
   )
   public TextureProvider(AbstractArchive var1, AbstractArchive var2, int var3, double var4, int var6) {
      this.deque = new NodeDeque();
      this.remaining = 0;
      this.brightness = 1.0D;
      this.textureSize = 128;
      this.archive = var2;
      this.capacity = var3;
      this.remaining = this.capacity;
      this.brightness = var4;
      this.textureSize = var6;
      int[] var7 = var1.getGroupFileIds_base(0);
      if(var7 != null) {
         int var8 = var7.length;
         this.textures = new Texture[var1.getGroupFileCount_base(0)];

         for(int var9 = 0; var9 < var8; ++var9) {
            Buffer var10 = new Buffer(var1.takeFile_base(0, var7[var9]));
            this.textures[var7[var9]] = new Texture(var10);
         }
      } else {
         this.textures = new Texture[0];
      }

      this.rl$init(var1, var2, var3, var4, var6);
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(B)I",
      garbageValue = "81"
   )
   public int getLoadedPercentage_base() {
      if(this.textures.length == 0) {
         return 100;
      } else {
         int var1 = 0;
         int var2 = 0;
         Texture[] var3 = this.textures;

         for(int var4 = 0; var4 < var3.length; ++var4) {
            Texture var5 = var3[var4];
            if(var5 != null && var5.fileIds != null) {
               var1 += var5.fileIds.length;
               int[] var6 = var5.fileIds;

               for(int var7 = 0; var7 < var6.length; ++var7) {
                  int var8 = var6[var7];
                  if(this.archive.method6954_base(var8)) {
                     ++var2;
                  }
               }
            }
         }

         if(var1 == 0) {
            return 0;
         } else {
            return var2 * 100 / var1;
         }
      }
   }

   @ObfuscatedName("ah")
   public void setBrightness_base(double var1) {
      this.brightness = var1;
      this.clear_base();
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(II)[I",
      garbageValue = "2146690386"
   )
   public int[] getTexturePixels_base(int var1) {
      Texture var2 = this.textures[var1];
      if(var2 != null) {
         if(var2.pixels != null) {
            this.deque.method7261(var2);
            var2.isLoaded = true;
            return var2.pixels;
         }

         boolean var3 = var2.method5180(this.brightness, this.textureSize, this.archive);
         if(var3) {
            if(this.remaining == 0) {
               Texture var4 = (Texture)this.deque.method7232();
               var4.reset_base();
            } else {
               --this.remaining;
            }

            this.deque.method7261(var2);
            var2.isLoaded = true;
            return var2.pixels;
         }
      }

      return null;
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(II)I",
      garbageValue = "-799593144"
   )
   public int getAverageTextureRGB_base(int var1) {
      return this.textures[var1] != null?this.textures[var1].averageRGB:0;
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(II)Z",
      garbageValue = "-293103198"
   )
   public boolean vmethod5823_base(int var1) {
      return this.textures[var1].field2725;
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(IS)Z",
      garbageValue = "32621"
   )
   public boolean isLowDetail_base(int var1) {
      return this.textureSize == 64;
   }

   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "1469594745"
   )
   public void clear_base() {
      for(int var1 = 0; var1 < this.textures.length; ++var1) {
         if(this.textures[var1] != null) {
            this.textures[var1].reset_base();
         }
      }

      this.deque = new NodeDeque();
      this.remaining = this.capacity;
   }

   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "2003638345"
   )
   public void animate_base(int var1) {
      for(int var2 = 0; var2 < this.textures.length; ++var2) {
         Texture var3 = this.textures[var2];
         if(var3 != null && var3.animationDirection != 0 && var3.isLoaded) {
            var3.animate_base(var1);
            var3.isLoaded = false;
         }
      }

      this.checkTextures(var1);
   }

   public void setMaxSize(int var1) {
      this.capacity = var1;
   }

   public void setSize(int var1) {
      this.remaining = var1;
   }

   public void checkTextures(int var1) {
      SpriteMask.client.getCallbacks().drawAboveOverheads();
   }

   public void rl$init(IndexDataBase var1, IndexDataBase var2, int var3, double var4, int var6) {
      this.setMaxSize(128);
      this.setSize(128);
   }

   public RSTexture[] getTextures() {
      return this.textures;
   }

   public double getBrightness() {
      return this.brightness;
   }

   public void setBrightness(double var1) {
      this.setBrightness_base(var1);
   }

   public int[] load(int var1) {
      return this.getTexturePixels_base(var1);
   }

   public int getDefaultColor(int var1) {
      return this.getAverageTextureRGB_base(var1);
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-1703102437"
   )
   static void savePreferences_base() {
      AccessFile var0 = null;

      try {
         var0 = class131.method3099("", Messages.field1401.name, true);
         Buffer var1 = WorldMapIcon_1.clientPreferences.method2603();
         var0.write_base(var1.array, 0, var1.offset);
      } catch (Exception var3) {
         ;
      }

      try {
         if(var0 != null) {
            var0.closeSync_base(true);
         }
      } catch (Exception var2) {
         ;
      }

   }

   @ObfuscatedName("iu")
   @ObfuscatedSignature(
      descriptor = "(B)V",
      garbageValue = "-73"
   )
   static final void method5533_base() {
      class314.method5953_base(FloorOverlayDefinition.field2194, class323.field3490, Projectile.field937);
      class303.method5908_base(class127.field1494, CollisionMap.field2370);
      if(FloorOverlayDefinition.field2194 == ViewportMouse.cameraX && SecureRandomFuture.cameraY == class323.field3490 && class36.cameraZ == Projectile.field937 && class127.field1494 == Varcs.cameraPitch && CollisionMap.field2370 == class192.cameraYaw) {
         Client.field482 = false;
         Client.isCameraLocked = false;
         Client.field748 = false;
         Client.field749 = false;
         Messages.field1400 = 0;
         AbstractWorldMapData.field2575 = 0;
         class302.field3119 = 0;
         class60.field421 = 0;
         class134.field1581 = 0;
         class1.field2 = 0;
         WorldMapIcon_0.field2529 = 0;
         ScriptFrame.field456 = 0;
         class216.field2314 = 0;
         class127.field1488 = 0;
         Client.field751 = null;
         Client.field672 = null;
         Client.field752 = null;
      }

   }
}
