package mapped;

import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSNanoClock;

@ObfuscatedName("he")
public class NanoClock extends Clock implements RSNanoClock {
   @ObfuscatedName("at")
   @ObfuscatedGetter(
      longValue = 8274921140651979769L
   )
   long lastTimeNano;
   public long tmpNanoTime;

   NanoClock() {
      this.lastTimeNano = System.nanoTime();
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-1366277024"
   )
   public void mark_base() {
      this.lastTimeNano = System.nanoTime();
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(III)I",
      garbageValue = "-2001388980"
   )
   public int wait_base(int var1, int var2) {
      if(SpriteMask.client.isUnlockedFps() && SpriteMask.client.getRSGameState() >= 25) {
         long var3 = System.nanoTime();
         if(var3 >= this.getLastTimeNano() && var3 >= this.tmpNanoTime) {
            long var5;
            long var7;
            if(SpriteMask.client.getUnlockedFpsTarget() > 0L) {
               var5 = var3 - this.tmpNanoTime;
               var7 = SpriteMask.client.getUnlockedFpsTarget() - var5;
               var7 /= 1000000L;
               if(var7 > 0L) {
                  try {
                     if(var7 % 10L == 0L) {
                        Thread.sleep(var7 - 1L);
                        Thread.sleep(1L);
                     } else {
                        Thread.sleep(var7);
                     }
                  } catch (InterruptedException var11) {
                     ;
                  }

                  var3 = System.nanoTime();
               }
            }

            this.tmpNanoTime = var3;
            var5 = (long)var1 * 1000000L;
            var7 = var3 - this.getLastTimeNano();
            int var10 = (int)(var7 / var5);
            this.setLastTimeNano(this.getLastTimeNano() + (long)var10 * var5);
            if(var10 > 10) {
               var10 = 10;
            }

            return var10;
         } else {
            this.setLastTimeNano(this.tmpNanoTime = var3);
            return 1;
         }
      } else {
         return this.copy$wait(var1, var2, -2001388980);
      }
   }

   @ObfuscatedSignature(
      descriptor = "(III)I",
      garbageValue = "-2001388980"
   )
   public int copy$wait(int var1, int var2, int var3) {
      long var4 = 1000000L * (long)var2;
      long var6 = this.lastTimeNano - System.nanoTime();
      if(var6 < var4) {
         var6 = var4;
      }

      class219.method4260_base(var6 / 1000000L);
      long var8 = System.nanoTime();

      int var10;
      for(var10 = 0; var10 < 10 && (var10 < 1 || this.lastTimeNano < var8); this.lastTimeNano += (long)var1 * 1000000L) {
         ++var10;
      }

      if(this.lastTimeNano < var8) {
         this.lastTimeNano = var8;
      }

      return var10;
   }

   public long getLastTimeNano() {
      return this.lastTimeNano;
   }

   public void setLastTimeNano(long var1) {
      this.lastTimeNano = var1;
   }
}
