package mapped;

import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSWorldMapArchiveLoader;

@ObfuscatedName("sl")
public class WorldMapArchiveLoader implements RSWorldMapArchiveLoader {
   @ObfuscatedName("ab")
   String cacheName;
   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "Lol;"
   )
   AbstractArchive archive;
   @ObfuscatedName("aa")
   @ObfuscatedGetter(
      intValue = -1424135489
   )
   int percentLoaded;
   @ObfuscatedName("ac")
   boolean loaded;

   @ObfuscatedSignature(
      descriptor = "(Lol;)V"
   )
   WorldMapArchiveLoader(AbstractArchive var1) {
      this.percentLoaded = 0;
      this.loaded = false;
      this.archive = var1;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;I)V",
      garbageValue = "2039026903"
   )
   void reset_base(String var1) {
      if(var1 != null && !var1.isEmpty()) {
         if(var1 != this.cacheName) {
            this.cacheName = var1;
            this.percentLoaded = 0;
            this.loaded = false;
            this.load_base();
         }
      }
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(B)I",
      garbageValue = "72"
   )
   int load_base() {
      if(this.cacheName == null) {
         this.percentLoaded = 100;
         this.loaded = true;
      } else {
         if(this.percentLoaded < 33) {
            if(!this.archive.tryLoadFileByNames_base(WorldMapCacheName.field2626.name, this.cacheName)) {
               return this.percentLoaded;
            }

            this.percentLoaded = 33;
         }

         if(this.percentLoaded == 33) {
            if(this.archive.isValidFileName_base(WorldMapCacheName.field2628.name, this.cacheName) && !this.archive.tryLoadFileByNames_base(WorldMapCacheName.field2628.name, this.cacheName)) {
               return this.percentLoaded;
            }

            this.percentLoaded = 66;
         }

         if(this.percentLoaded == 66) {
            if(!this.archive.tryLoadFileByNames_base(this.cacheName, WorldMapCacheName.field2630.name)) {
               return this.percentLoaded;
            }

            this.percentLoaded = 100;
            this.loaded = true;
         }
      }

      return this.percentLoaded;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(B)Z",
      garbageValue = "47"
   )
   boolean isLoaded_base() {
      return this.loaded;
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(I)I",
      garbageValue = "301299850"
   )
   int getPercentLoaded_base() {
      return this.percentLoaded;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "([BI)Luz;",
      garbageValue = "1918465427"
   )
   public static final SpritePixels method8712(byte[] var0) {
      BufferedImage var1 = null;

      try {
         Class var2 = ImageIO.class;
         synchronized(ImageIO.class) {
            var1 = ImageIO.read(new ByteArrayInputStream(var0));
         }

         int var6 = var1.getWidth();
         int var7 = var1.getHeight();
         int[] var4 = new int[var7 * var6];
         PixelGrabber var5 = new PixelGrabber(var1, 0, 0, var6, var7, var4, 0, var6);
         var5.grabPixels();
         SpritePixels var10000 = new SpritePixels(var4, var6, var7);
         return var10000;
      } catch (IOException var9) {
         ;
      } catch (InterruptedException var10) {
         ;
      }

      return new SpritePixels(0, 0);
   }

   @ObfuscatedName("gj")
   @ObfuscatedSignature(
      descriptor = "(I)Ltn;",
      garbageValue = "-472166633"
   )
   static WorldMap method8711() {
      return NpcOverrides.worldMap;
   }
}
