package mapped;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSWorldMapRegion;
import net.runelite.rs.api.RSWorldMapRenderer;

@ObfuscatedName("jf")
public final class WorldMapRenderer implements RSWorldMapRenderer {
   @ObfuscatedName("at")
   boolean loaded;
   @ObfuscatedName("ah")
   boolean field2490;
   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "Ljx;"
   )
   WorldMapAreaData mapAreaData;
   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "Luz;"
   )
   SpritePixels compositeTextureSprite;
   @ObfuscatedName("ab")
   HashMap icons;
   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "[[Ljl;"
   )
   WorldMapRegion[][] regions;
   @ObfuscatedName("aa")
   HashMap scaleHandlers;
   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "[Lup;"
   )
   IndexedSprite[] mapSceneSprites;
   @ObfuscatedName("al")
   @ObfuscatedSignature(
      descriptor = "Lol;"
   )
   final AbstractArchive geographyArchive;
   @ObfuscatedName("az")
   @ObfuscatedSignature(
      descriptor = "Lol;"
   )
   final AbstractArchive groundArchive;
   @ObfuscatedName("ap")
   final HashMap fonts;
   @ObfuscatedName("av")
   @ObfuscatedGetter(
      intValue = -2120914503
   )
   int tileX;
   @ObfuscatedName("ax")
   @ObfuscatedGetter(
      intValue = 775584779
   )
   int tileY;
   @ObfuscatedName("as")
   @ObfuscatedGetter(
      intValue = -1810827253
   )
   int tileWidth;
   @ObfuscatedName("ay")
   @ObfuscatedGetter(
      intValue = -439773231
   )
   int tileHeight;
   @ObfuscatedName("ak")
   @ObfuscatedGetter(
      intValue = -1484309687
   )
   public int pixelsPerTile;

   @ObfuscatedSignature(
      descriptor = "([Lup;Ljava/util/HashMap;Lol;Lol;)V"
   )
   public WorldMapRenderer(IndexedSprite[] var1, HashMap var2, AbstractArchive var3, AbstractArchive var4) {
      this.loaded = false;
      this.field2490 = false;
      this.scaleHandlers = new HashMap();
      this.pixelsPerTile = 0;
      this.mapSceneSprites = var1;
      this.fonts = var2;
      this.geographyArchive = var3;
      this.groundArchive = var4;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(Lol;Ljava/lang/String;ZZB)V",
      garbageValue = "-4"
   )
   public void method4733(AbstractArchive var1, String var2, boolean var3, boolean var4) {
      if(!this.field2490) {
         this.loaded = false;
         this.field2490 = true;
         System.nanoTime();
         int var5 = var1.getGroupId_base(WorldMapCacheName.field2633.name);
         int var6 = var1.getFileId_base(var5, var2);
         Buffer var7 = new Buffer(var1.takeFileByNames_base(WorldMapCacheName.field2633.name, var2));
         Buffer var8 = new Buffer(var1.takeFileByNames_base(WorldMapCacheName.field2626.name, var2));
         System.nanoTime();
         System.nanoTime();
         this.mapAreaData = new WorldMapAreaData();

         try {
            this.mapAreaData.method5036(var7, var8, var6, var3, var4);
         } catch (IllegalStateException var20) {
            return;
         }

         this.mapAreaData.getOriginX_base();
         this.mapAreaData.getOriginPlane_base();
         this.mapAreaData.getOriginY_base();
         this.tileX = this.mapAreaData.getRegionLowX_base() * 64;
         this.tileY = this.mapAreaData.getRegionLowY_base() * 64;
         this.tileWidth = (this.mapAreaData.getRegionHighX_base() - this.mapAreaData.getRegionLowX_base() + 1) * 64;
         this.tileHeight = (this.mapAreaData.getRegionHighY_base() - this.mapAreaData.getRegionLowY_base() + 1) * 64;
         int var17 = this.mapAreaData.getRegionHighX_base() - this.mapAreaData.getRegionLowX_base() + 1;
         int var10 = this.mapAreaData.getRegionHighY_base() - this.mapAreaData.getRegionLowY_base() + 1;
         System.nanoTime();
         System.nanoTime();
         WorldMapRegion.WorldMapRegion_cachedSprites.clear_base();
         this.regions = new WorldMapRegion[var17][var10];
         Iterator var11 = this.mapAreaData.field2635.iterator();

         while(var11.hasNext()) {
            WorldMapData_0 var12 = (WorldMapData_0)var11.next();
            int var13 = var12.regionX;
            int var14 = var12.regionY;
            int var15 = var13 - this.mapAreaData.getRegionLowX_base();
            int var16 = var14 - this.mapAreaData.getRegionLowY_base();
            this.regions[var15][var16] = new WorldMapRegion(var13, var14, this.mapAreaData.method4573_base(), this.fonts);
            this.regions[var15][var16].method4671(var12, this.mapAreaData.iconList);
         }

         for(int var18 = 0; var18 < var17; ++var18) {
            for(int var19 = 0; var19 < var10; ++var19) {
               if(this.regions[var18][var19] == null) {
                  this.regions[var18][var19] = new WorldMapRegion(this.mapAreaData.getRegionLowX_base() + var18, this.mapAreaData.getRegionLowY_base() + var19, this.mapAreaData.method4573_base(), this.fonts);
                  this.regions[var18][var19].initWorldMapData1_base(this.mapAreaData.field2636, this.mapAreaData.iconList);
               }
            }
         }

         System.nanoTime();
         System.nanoTime();
         if(var1.isValidFileName_base(WorldMapCacheName.field2628.name, var2)) {
            byte[] var21 = var1.takeFileByNames_base(WorldMapCacheName.field2628.name, var2);
            this.compositeTextureSprite = WorldMapArchiveLoader.method8712(var21);
         }

         System.nanoTime();
         var1.clearGroups_base();
         var1.clearFiles_base();
         this.loaded = true;
      }
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-120523343"
   )
   public final void clearIcons_base() {
      this.icons = null;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(IIIIIIIII)V",
      garbageValue = "-737280912"
   )
   public final void drawTiles_base(int var1, int var2, int var3, int var4, int var5, int var6, int var7, int var8) {
      int[] var9 = Rasterizer2D.Rasterizer2D_pixels;
      int var10 = Rasterizer2D.Rasterizer2D_width;
      int var11 = Rasterizer2D.Rasterizer2D_height;
      float[] var12 = Rasterizer2D.field5209;
      int[] var13 = new int[4];
      Rasterizer2D.Rasterizer2D_getClipArray_base(var13);
      WorldMapRectangle var14 = this.method4739(var1, var2, var3, var4);
      float var15 = this.getPixelsPerTile_base(var7 - var5, var3 - var1);
      int var16 = (int)Math.ceil((double)var15);
      this.pixelsPerTile = var16;
      if(!this.scaleHandlers.containsKey(Integer.valueOf(var16))) {
         WorldMapScaleHandler var17 = new WorldMapScaleHandler(var16);
         var17.init_base();
         this.scaleHandlers.put(Integer.valueOf(var16), var17);
      }

      int var24 = var14.x + var14.width - 1;
      int var18 = var14.y + var14.height - 1;

      int var19;
      int var20;
      for(var19 = var14.x; var19 <= var24; ++var19) {
         for(var20 = var14.y; var20 <= var18; ++var20) {
            this.regions[var19][var20].method4633(var16, (WorldMapScaleHandler)this.scaleHandlers.get(Integer.valueOf(var16)), this.mapSceneSprites, this.geographyArchive, this.groundArchive);
         }
      }

      Rasterizer3D.method5263_base(var9, var10, var11, var12);
      Rasterizer2D.Rasterizer2D_setClipArray_base(var13);
      var19 = (int)(var15 * 64.0F);
      var20 = this.tileX + var1;
      int var21 = var2 + this.tileY;

      for(int var22 = var14.x; var22 < var14.x + var14.width; ++var22) {
         for(int var23 = var14.y; var23 < var14.y + var14.height; ++var23) {
            this.regions[var22][var23].method4625_base(var5 + var19 * (this.regions[var22][var23].regionX * 64 - var20) / 64, var8 - var19 * (this.regions[var22][var23].regionY * 64 - var21 + 64) / 64, var19);
         }
      }

   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(IIIIIIIILjava/util/HashSet;Ljava/util/HashSet;IIZI)V",
      garbageValue = "-1576808810"
   )
   public final void drawElements_base(int var1, int var2, int var3, int var4, int var5, int var6, int var7, int var8, HashSet var9, HashSet var10, int var11, int var12, boolean var13) {
      WorldMapRectangle var14 = this.method4739(var1, var2, var3, var4);
      float var15 = this.getPixelsPerTile_base(var7 - var5, var3 - var1);
      int var16 = (int)(64.0F * var15);
      int var17 = this.tileX + var1;
      int var18 = var2 + this.tileY;

      int var19;
      int var20;
      for(var19 = var14.x; var19 < var14.x + var14.width; ++var19) {
         for(var20 = var14.y; var20 < var14.height + var14.y; ++var20) {
            if(var13) {
               this.regions[var19][var20].initWorldMapIcon1s_base();
            }

            this.regions[var19][var20].method4634_base(var5 + var16 * (this.regions[var19][var20].regionX * 64 - var17) / 64, var8 - var16 * (this.regions[var19][var20].regionY * 64 - var18 + 64) / 64, var16, var9);
         }
      }

      if(var10 != null && var11 > 0) {
         for(var19 = var14.x; var19 < var14.x + var14.width; ++var19) {
            for(var20 = var14.y; var20 < var14.height + var14.y; ++var20) {
               this.regions[var19][var20].flashElements_base(var10, var11, var12);
            }
         }
      }

   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(IIIILjava/util/HashSet;III)V",
      garbageValue = "-1372538042"
   )
   public void drawOverview_base(int var1, int var2, int var3, int var4, HashSet var5, int var6, int var7) {
      if(this.compositeTextureSprite != null) {
         this.compositeTextureSprite.drawScaledAt_base(var1, var2, var3, var4);
         if(var6 > 0 && var6 % var7 < var7 / 2) {
            if(this.icons == null) {
               this.buildIcons0_base();
            }

            Iterator var8 = var5.iterator();

            while(true) {
               List var10;
               do {
                  if(!var8.hasNext()) {
                     return;
                  }

                  int var9 = ((Integer)var8.next()).intValue();
                  var10 = (List)this.icons.get(Integer.valueOf(var9));
               } while(var10 == null);

               Iterator var11 = var10.iterator();

               while(var11.hasNext()) {
                  AbstractWorldMapIcon var12 = (AbstractWorldMapIcon)var11.next();
                  int var13 = var3 * (var12.coord2.x - this.tileX) / this.tileWidth;
                  int var14 = var4 - (var12.coord2.y - this.tileY) * var4 / this.tileHeight;
                  Rasterizer2D.Rasterizer2D_drawCircleAlpha_base(var13 + var1, var14 + var2, 2, 16776960, 256);
               }
            }
         }
      }
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(IIIIIIIIIII)Ljava/util/List;",
      garbageValue = "-44973275"
   )
   public List method4764_base(int var1, int var2, int var3, int var4, int var5, int var6, int var7, int var8, int var9, int var10) {
      LinkedList var11 = new LinkedList();
      if(!this.loaded) {
         return var11;
      } else {
         WorldMapRectangle var12 = this.method4739(var1, var2, var3, var4);
         float var13 = this.getPixelsPerTile_base(var7, var3 - var1);
         int var14 = (int)(64.0F * var13);
         int var15 = this.tileX + var1;
         int var16 = var2 + this.tileY;

         for(int var17 = var12.x; var17 < var12.x + var12.width; ++var17) {
            for(int var18 = var12.y; var18 < var12.height + var12.y; ++var18) {
               List var19 = this.regions[var17][var18].method4699_base(var5 + var14 * (this.regions[var17][var18].regionX * 64 - var15) / 64, var8 + var6 - var14 * (this.regions[var17][var18].regionY * 64 - var16 + 64) / 64, var14, var9, var10);
               if(!var19.isEmpty()) {
                  var11.addAll(var19);
               }
            }
         }

         return var11;
      }
   }

   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "(IIIII)Ljy;",
      garbageValue = "1379853594"
   )
   WorldMapRectangle method4739(int var1, int var2, int var3, int var4) {
      WorldMapRectangle var5 = new WorldMapRectangle(this);
      int var6 = this.tileX + var1;
      int var7 = var2 + this.tileY;
      int var8 = var3 + this.tileX;
      int var9 = var4 + this.tileY;
      int var10 = var6 / 64;
      int var11 = var7 / 64;
      int var12 = var8 / 64;
      int var13 = var9 / 64;
      var5.width = var12 - var10 + 1;
      var5.height = var13 - var11 + 1;
      var5.x = var10 - this.mapAreaData.getRegionLowX_base();
      var5.y = var11 - this.mapAreaData.getRegionLowY_base();
      if(var5.x < 0) {
         var5.width += var5.x;
         var5.x = 0;
      }

      if(var5.x > this.regions.length - var5.width) {
         var5.width = this.regions.length - var5.x;
      }

      if(var5.y < 0) {
         var5.height += var5.y;
         var5.y = 0;
      }

      if(var5.y > this.regions[0].length - var5.height) {
         var5.height = this.regions[0].length - var5.y;
      }

      var5.width = Math.min(var5.width, this.regions.length);
      var5.height = Math.min(var5.height, this.regions[0].length);
      return var5;
   }

   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "(I)Z",
      garbageValue = "2083018743"
   )
   public boolean isLoaded_base() {
      return this.loaded;
   }

   @ObfuscatedName("al")
   @ObfuscatedSignature(
      descriptor = "(I)I",
      garbageValue = "-1029220515"
   )
   public int method4741_base() {
      return this.mapAreaData.method4620_base();
   }

   @ObfuscatedName("az")
   @ObfuscatedSignature(
      descriptor = "(B)Ljava/util/HashMap;",
      garbageValue = "-62"
   )
   public HashMap buildIcons_base() {
      this.buildIcons0_base();
      return this.icons;
   }

   @ObfuscatedName("ap")
   @ObfuscatedSignature(
      descriptor = "(S)V",
      garbageValue = "16638"
   )
   void buildIcons0_base() {
      if(this.icons == null) {
         this.icons = new HashMap();
      }

      this.icons.clear();

      for(int var1 = 0; var1 < this.regions.length; ++var1) {
         for(int var2 = 0; var2 < this.regions[var1].length; ++var2) {
            List var3 = this.regions[var1][var2].icons_base();
            Iterator var4 = var3.iterator();

            while(var4.hasNext()) {
               AbstractWorldMapIcon var5 = (AbstractWorldMapIcon)var4.next();
               if(var5.hasValidElement_base()) {
                  int var6 = var5.getElement_base();
                  if(!this.icons.containsKey(Integer.valueOf(var6))) {
                     LinkedList var7 = new LinkedList();
                     var7.add(var5);
                     this.icons.put(Integer.valueOf(var6), var7);
                  } else {
                     List var8 = (List)this.icons.get(Integer.valueOf(var6));
                     var8.add(var5);
                  }
               }
            }
         }
      }

   }

   @ObfuscatedName("av")
   @ObfuscatedSignature(
      descriptor = "(III)F",
      garbageValue = "847621694"
   )
   float getPixelsPerTile_base(int var1, int var2) {
      return SpriteMask.client.getWorldMap().getWorldMapZoom();
   }

   public RSWorldMapRegion[][] getMapRegions() {
      return this.regions;
   }

   public int getSurfaceOffsetX() {
      return this.tileX;
   }

   public int getSurfaceOffsetY() {
      return this.tileY;
   }

   public boolean isLoaded() {
      return this.isLoaded_base();
   }

   public float getPixelsPerTile(int var1, int var2) {
      return this.getPixelsPerTile_base(var1, var2);
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(III)V",
      garbageValue = "-129998298"
   )
   public static void method4769_base(int var0, int var1) {
      EnumComposition.method3678_base(var0, var1, 0, 0);
      class316.field3424.clear();
      class316.field3413.clear();
      if(class316.musicSongs.isEmpty() || var0 == 0 && var1 == 0) {
         SecureRandomCallable.method2326_base();
      } else {
         class316.field3413.add(new DelayFadeTask((SongTask)null, class316.musicPlayerStatus));
         class316.field3413.add(new FadeOutTask((SongTask)null, 0, false, class316.field3418));
         ArrayList var3 = new ArrayList();
         Iterator var4 = class316.musicSongs.iterator();

         while(var4.hasNext()) {
            MusicSong var5 = (MusicSong)var4.next();
            var3.add(var5);
         }

         class316.field3413.add(new class413((SongTask)null, var3));
      }

   }

   @ObfuscatedName("ba")
   @ObfuscatedSignature(
      descriptor = "(S)V",
      garbageValue = "1154"
   )
   protected static final void method4746_base() {
      ArchiveDiskAction.clock.mark_base();

      int var0;
      for(var0 = 0; var0 < 32; ++var0) {
         GameEngine.graphicsTickTimes[var0] = 0L;
      }

      for(var0 = 0; var0 < 32; ++var0) {
         GameEngine.clientTickTimes[var0] = 0L;
      }

      GameEngine.gameCyclesToDo = 0;
   }
}
