package mapped;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import java.util.Queue;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSUrlRequester;

@ObfuscatedName("el")
public abstract class UrlRequester implements Runnable, RSUrlRequester {
   @ObfuscatedName("at")
   final Thread field1418;
   @ObfuscatedName("ah")
   volatile boolean isClosed;
   @ObfuscatedName("ar")
   Queue requests;
   @ObfuscatedName("ao")
   @ObfuscatedGetter(
      intValue = -108410615
   )
   int field1419;

   UrlRequester(int var1) {
      this.requests = new LinkedList();
      this.field1418 = new Thread(this);
      this.field1418.setPriority(1);
      this.field1418.start();
      this.field1419 = var1;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(Lea;I)V",
      garbageValue = "422697771"
   )
   abstract void vmethod2910(UrlRequest var1) throws IOException;

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(Ljava/net/URLConnection;B)I",
      garbageValue = "117"
   )
   int method2888_base(URLConnection var1) {
      int var2 = UrlRequest.field1432;
      if(var1 != null) {
         try {
            if(var1 instanceof HttpURLConnection) {
               var2 = ((HttpURLConnection)var1).getResponseCode();
            }
         } catch (IOException var4) {
            ;
         }
      }

      return var2;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(Ljava/net/URLConnection;I)V",
      garbageValue = "-727405176"
   )
   void method2905_base(URLConnection var1) {
      var1.setConnectTimeout(5000);
      var1.setReadTimeout(5000);
      var1.setUseCaches(false);
      var1.setRequestProperty("Connection", "close");
      var1.setRequestProperty("User-Agent", "BoomScape/" + this.field1419);
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(Ljava/net/URLConnection;Lea;I)V",
      garbageValue = "-1518278388"
   )
   void method2890(URLConnection var1, UrlRequest var2) {
      DataInputStream var3 = null;

      try {
         int var5 = var1.getContentLength();
         var3 = new DataInputStream(var1.getInputStream());
         byte[] var4;
         if(var5 >= 0) {
            var4 = new byte[var5];
            var3.readFully(var4);
         } else {
            var4 = new byte[0];
            byte[] var6 = class362.ByteArrayPool_getArray_base(5000);

            for(int var7 = var3.read(var6); var7 > -1; var7 = var3.read(var6)) {
               byte[] var8 = new byte[var4.length + var7];
               System.arraycopy(var4, 0, var8, 0, var4.length);
               System.arraycopy(var6, 0, var8, var4.length, var7);
               var4 = var8;
            }

            SwapSongTask.ByteArrayPool_release_base(var6);
         }

         var2.response0 = var4;
      } catch (IOException var14) {
         var2.response0 = null;
      } finally {
         var2.field1427 = this.method2888_base(var1);
      }

      if(var3 != null) {
         try {
            var3.close();
         } catch (IOException var13) {
            ;
         }
      }

   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(Ljava/net/URL;I)Lea;",
      garbageValue = "-511964616"
   )
   public UrlRequest method2889(URL var1) {
      UrlRequest var2 = new UrlRequest(var1);
      synchronized(this) {
         this.requests.add(var2);
         this.notify();
      }

      return var2;
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-264971545"
   )
   public void close_base() {
      this.isClosed = true;

      try {
         synchronized(this) {
            this.notify();
         }

         this.field1418.join();
      } catch (InterruptedException var4) {
         ;
      }

   }

   public void run() {
      while(!this.isClosed) {
         try {
            UrlRequest var1;
            synchronized(this) {
               var1 = (UrlRequest)this.requests.poll();
               if(var1 == null) {
                  try {
                     this.wait();
                  } catch (InterruptedException var5) {
                     ;
                  }
                  continue;
               }
            }

            this.vmethod2910(var1);
         } catch (Exception var7) {
            GrandExchangeOfferWorldComparator.RunException_sendStackTrace_base((String)null, var7);
         }
      }

   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(IIILhs;II)V",
      garbageValue = "221128154"
   )
   static void method2900(int var0, int var1, int var2, ObjectComposition var3, int var4) {
      ObjectSound var5 = new ObjectSound();
      var5.plane = var0;
      var5.x = var1 * 128;
      var5.y = var2 * 128;
      int var6 = var3.sizeX;
      int var7 = var3.sizeY;
      if(var4 == 1 || var4 == 3) {
         var6 = var3.sizeY;
         var7 = var3.sizeX;
      }

      var5.maxX = (var6 + var1) * 128;
      var5.maxY = (var7 + var2) * 128;
      var5.soundEffectId = var3.ambientSoundId;
      var5.field824 = var3.int7 * 128;
      var5.field827 = var3.int5;
      var5.field828 = var3.int6;
      var5.soundEffectIds = var3.soundEffectIds;
      if(var3.transforms != null) {
         var5.obj = var3;
         var5.set_base();
      }

      ObjectSound.objectSounds.method7233(var5);
      if(var5.soundEffectIds != null) {
         var5.field832 = var5.field827 + (int)(Math.random() * (double)(var5.field828 - var5.field827));
      }

      Client.onAmbientSoundEffect(var0, var1, var2, var3, var4);
   }

   @ObfuscatedName("ag")
   @ObfuscatedSignature(
      descriptor = "(ILdt;ZS)I",
      garbageValue = "-175"
   )
   static int method2901(int var0, Script var1, boolean var2) {
      Widget var3 = HealthBarDefinition.widgetDefinition.method6287(Interpreter.Interpreter_intStack[--DbTableType.Interpreter_intStackSize]);
      if(var0 == 2800) {
         Interpreter.Interpreter_intStack[++DbTableType.Interpreter_intStackSize - 1] = WorldMapData_0.Widget_unpackTargetMask_base(class361.method6871(var3));
         return 1;
      } else if(var0 != 2801) {
         if(var0 == 2802) {
            if(var3.dataText == null) {
               Interpreter.Interpreter_stringStack[++class127.Interpreter_stringStackSize - 1] = "";
            } else {
               Interpreter.Interpreter_stringStack[++class127.Interpreter_stringStackSize - 1] = var3.dataText;
            }

            return 1;
         } else {
            return 2;
         }
      } else {
         int var4 = Interpreter.Interpreter_intStack[--DbTableType.Interpreter_intStackSize];
         --var4;
         if(var3.actions != null && var4 < var3.actions.length && var3.actions[var4] != null) {
            Interpreter.Interpreter_stringStack[++class127.Interpreter_stringStackSize - 1] = var3.actions[var4];
         } else {
            Interpreter.Interpreter_stringStack[++class127.Interpreter_stringStackSize - 1] = "";
         }

         return 1;
      }
   }
}
