package mapped;

import java.io.DataInputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSTaskHandler;

@ObfuscatedName("ih")
public class TaskHandler implements Runnable, RSTaskHandler {
   @ObfuscatedName("at")
   public static String javaVendor;
   @ObfuscatedName("ah")
   public static String javaVersion;
   @ObfuscatedName("ax")
   @ObfuscatedSignature(
      descriptor = "Lpf;"
   )
   public static Font ItemDefinition_fontPlain11;
   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "Liq;"
   )
   Task current;
   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "Liq;"
   )
   Task task;
   @ObfuscatedName("ab")
   Thread thread;
   @ObfuscatedName("au")
   boolean isClosed;

   public TaskHandler() {
      this.current = null;
      this.task = null;
      this.isClosed = false;
      javaVendor = "Unknown";
      javaVersion = "1.6";

      try {
         javaVendor = System.getProperty("java.vendor");
         javaVersion = System.getProperty("java.version");
      } catch (Exception var2) {
         ;
      }

      this.isClosed = false;
      this.thread = new Thread(this);
      this.thread.setPriority(10);
      this.thread.setDaemon(true);
      this.thread.start();
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-585427867"
   )
   public final void close_base() {
      synchronized(this) {
         this.isClosed = true;
         this.notifyAll();
      }

      try {
         this.thread.join();
      } catch (InterruptedException var3) {
         ;
      }

   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(IIILjava/lang/Object;I)Liq;",
      garbageValue = "-993585503"
   )
   final Task method4188(int var1, int var2, int var3, Object var4) {
      Task var5 = new Task();
      var5.type = var1;
      var5.intArgument = var2;
      var5.objectArgument = var4;
      synchronized(this) {
         if(this.task != null) {
            this.task.next = var5;
            this.task = var5;
         } else {
            this.task = this.current = var5;
         }

         this.notify();
      }

      return var5;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;II)Liq;",
      garbageValue = "-180890687"
   )
   public final Task method4185(String var1, int var2) {
      return this.method4188(1, var2, 0, var1);
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/Runnable;II)Liq;",
      garbageValue = "1323280137"
   )
   public final Task method4190(Runnable var1, int var2) {
      return this.method4188(2, var2, 0, var1);
   }

   public final void run() {
      while(true) {
         Task var1;
         synchronized(this) {
            while(true) {
               if(this.isClosed) {
                  return;
               }

               if(this.current != null) {
                  var1 = this.current;
                  this.current = this.current.next;
                  if(this.current == null) {
                     this.task = null;
                  }
                  break;
               }

               try {
                  this.wait();
               } catch (InterruptedException var8) {
                  ;
               }
            }
         }

         try {
            int var5 = var1.type;
            if(var5 == 1) {
               var1.result = new Socket(InetAddress.getByName((String)var1.objectArgument), var1.intArgument);
            } else if(var5 == 2) {
               Thread var3 = new Thread((Runnable)var1.objectArgument);
               var3.setDaemon(true);
               var3.start();
               var3.setPriority(var1.intArgument);
               var1.result = var3;
            } else if(var5 == 4) {
               var1.result = new DataInputStream(((URL)var1.objectArgument).openStream());
            }

            var1.status = 1;
         } catch (ThreadDeath var6) {
            throw var6;
         } catch (Throwable var7) {
            var1.status = 2;
         }
      }
   }

   @ObfuscatedName("km")
   @ObfuscatedSignature(
      descriptor = "(IIIIII)Z",
      garbageValue = "1243535616"
   )
   static final boolean method4201_base(int var0, int var1, int var2, int var3, int var4) {
      PendingSpawn var5 = null;

      for(PendingSpawn var6 = (PendingSpawn)Client.pendingSpawns.method7265(); var6 != null; var6 = (PendingSpawn)Client.pendingSpawns.method7235()) {
         if(var0 == var6.plane && var6.x == var1 && var2 == var6.y && var3 == var6.type) {
            var5 = var6;
            break;
         }
      }

      if(var5 != null) {
         var5.field1152 = var4;
         return true;
      } else {
         return false;
      }
   }
}
