package mapped;

import java.io.IOException;
import java.io.OutputStream;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSBufferedSink;

@ObfuscatedName("rs")
public class BufferedSink implements Runnable, RSBufferedSink {
   @ObfuscatedName("at")
   Thread thread;
   @ObfuscatedName("ah")
   OutputStream outputStream;
   @ObfuscatedName("ar")
   @ObfuscatedGetter(
      intValue = 159807561
   )
   int capacity;
   @ObfuscatedName("ao")
   byte[] buffer;
   @ObfuscatedName("ab")
   @ObfuscatedGetter(
      intValue = 2045851071
   )
   int position;
   @ObfuscatedName("au")
   @ObfuscatedGetter(
      intValue = 1690012111
   )
   int limit;
   @ObfuscatedName("aa")
   IOException exception;
   @ObfuscatedName("ac")
   boolean closed;

   BufferedSink(OutputStream var1, int var2) {
      this.position = 0;
      this.limit = 0;
      this.outputStream = var1;
      this.capacity = var2 + 1;
      this.buffer = new byte[this.capacity];
      this.thread = new Thread(this);
      this.thread.setDaemon(true);
      this.thread.start();
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(I)Z",
      garbageValue = "-1028287550"
   )
   boolean isClosed_base() {
      if(this.closed) {
         try {
            this.outputStream.close();
            if(this.exception == null) {
               this.exception = new IOException("");
            }
         } catch (IOException var2) {
            if(this.exception == null) {
               this.exception = new IOException(var2);
            }
         }

         return true;
      } else {
         return false;
      }
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "([BIIB)V",
      garbageValue = "-81"
   )
   void write_base(byte[] var1, int var2, int var3) throws IOException {
      if(var3 >= 0 && var2 >= 0 && var3 + var2 <= var1.length) {
         synchronized(this) {
            if(this.exception != null) {
               throw new IOException(this.exception.toString());
            }

            int var5;
            if(this.position <= this.limit) {
               var5 = this.capacity - this.limit + this.position - 1;
            } else {
               var5 = this.position - this.limit - 1;
            }

            if(var5 < var3) {
               throw new IOException("");
            }

            if(var3 + this.limit <= this.capacity) {
               System.arraycopy(var1, var2, this.buffer, this.limit, var3);
            } else {
               int var6 = this.capacity - this.limit;
               System.arraycopy(var1, var2, this.buffer, this.limit, var6);
               System.arraycopy(var1, var6 + var2, this.buffer, 0, var3 - var6);
            }

            this.limit = (var3 + this.limit) % this.capacity;
            this.notifyAll();
         }

      } else {
         throw new IOException();
      }
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "1504320255"
   )
   void close_base() {
      synchronized(this) {
         this.closed = true;
         this.notifyAll();
      }

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

   }

   public void run() {
      while(true) {
         synchronized(this){
            while(true) {
               boolean var14 = false;

               int var1;
               try {
                  var14 = true;
                  if(this.exception != null) {
                     var14 = false;
                     return;
                  }

                  if(this.position <= this.limit) {
                     var1 = this.limit - this.position;
                  } else {
                     var1 = this.capacity - this.position + this.limit;
                  }

                  if(var1 <= 0) {
                     try {
                        this.outputStream.flush();
                     } catch (IOException var19) {
                        this.exception = var19;
                        var14 = false;
                        return;
                     }

                     if(this.isClosed_base()) {
                        var14 = false;
                        return;
                     }

                     try {
                        this.wait();
                     } catch (InterruptedException var18) {
                        ;
                     }
                     continue;
                  }

                  var14 = false;
               } finally {
                  if(var14) {
                     ;
                  }
               }

               try {
                  if(var1 + this.position <= this.capacity) {
                     this.outputStream.write(this.buffer, this.position, var1);
                  } else {
                     int var7 = this.capacity - this.position;
                     this.outputStream.write(this.buffer, this.position, var7);
                     this.outputStream.write(this.buffer, 0, var1 - var7);
                  }
               } catch (IOException var17) {
                  IOException var2 = var17;
                  synchronized(this) {
                     this.exception = var2;
                  }

                  return;
               }

               synchronized(this) {
                  this.position = (var1 + this.position) % this.capacity;
               }

               if(!this.isClosed_base()) {
                  break;
               }

               return;
            }
         }
      }
   }
}
