package mapped;

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.SyncFailedException;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSAccessFile;

@ObfuscatedName("to")
public final class AccessFile implements RSAccessFile {
   @ObfuscatedName("at")
   RandomAccessFile file;
   @ObfuscatedName("ah")
   @ObfuscatedGetter(
      longValue = -5580478468447993759L
   )
   final long maxSize;
   @ObfuscatedName("ar")
   @ObfuscatedGetter(
      longValue = 1665217932623654273L
   )
   long offset;

   public AccessFile(File var1, String var2, long var3) throws IOException {
      if(-1L == var3) {
         var3 = Long.MAX_VALUE;
      }

      if(var1.length() > var3) {
         var1.delete();
      }

      this.file = new RandomAccessFile(var1, var2);
      this.maxSize = var3;
      this.offset = 0L;
      int var5 = this.file.read();
      if(var5 != -1 && !var2.equals("r")) {
         this.file.seek(0L);
         this.file.write(var5);
      }

      this.file.seek(0L);
   }

   @ObfuscatedName("at")
   final void seek_base(long var1) throws IOException {
      this.file.seek(var1);
      this.offset = var1;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "([BIII)V",
      garbageValue = "382643150"
   )
   public final void write_base(byte[] var1, int var2, int var3) throws IOException {
      if((long)var3 + this.offset > this.maxSize) {
         this.file.seek(this.maxSize);
         this.file.write(1);
         throw new EOFException();
      } else {
         this.file.write(var1, var2, var3);
         this.offset += (long)var3;
      }
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(B)V",
      garbageValue = "97"
   )
   public final void close_base() throws IOException {
      this.closeSync_base(false);
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(ZB)V",
      garbageValue = "52"
   )
   public final void closeSync_base(boolean var1) throws IOException {
      if(this.file != null) {
         if(var1) {
            try {
               this.file.getFD().sync();
            } catch (SyncFailedException var3) {
               ;
            }
         }

         this.file.close();
         this.file = null;
      }

   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(I)J",
      garbageValue = "494268668"
   )
   public final long length_base() throws IOException {
      return this.file.length();
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "([BIII)I",
      garbageValue = "-1827594375"
   )
   public final int read_base(byte[] var1, int var2, int var3) throws IOException {
      int var4 = this.file.read(var1, var2, var3);
      if(var4 > 0) {
         this.offset += (long)var4;
      }

      return var4;
   }

   protected void finalize() throws Throwable {
      if(this.file != null) {
         System.out.println("");
         this.close_base();
      }

   }

   public RandomAccessFile getFile() {
      return this.file;
   }

   public long getLength() {
      return this.maxSize;
   }

   public long getPosition() {
      return this.offset;
   }

   @ObfuscatedName("jf")
   @ObfuscatedSignature(
      descriptor = "(Lds;ZB)V",
      garbageValue = "-57"
   )
   static void method9046(Player var0, boolean var1) {
      if(var0 != null && var0.isVisible_base() && !var0.isHidden) {
         var0.isUnanimated = false;
         if((Client.isLowDetail && Players.Players_count > 50 || Players.Players_count > 200) && var1 && var0.idleSequence == var0.movementSequence) {
            var0.isUnanimated = true;
         }

         int var2 = var0.x >> 7;
         int var3 = var0.y >> 7;
         if(var2 >= 0 && var2 < 104 && var3 >= 0 && var3 < 104) {
            long var4 = Player.calculateTag_base(0, 0, 0, false, var0.index);
            if(var0.model0 != null && Client.cycle >= var0.animationCycleStart && Client.cycle < var0.animationCycleEnd) {
               var0.isUnanimated = false;
               var0.tileHeight = WorldMapDecorationType.getTileHeight_base(var0.x, var0.y, ItemLayer.Client_plane);
               var0.playerCycle = Client.cycle;
               class36.scene.method5373(ItemLayer.Client_plane, var0.x, var0.y, var0.tileHeight, 60, var0, var0.rotation, var4, var0.minX, var0.minY, var0.maxX, var0.maxY);
            } else {
               if((var0.x & 127) == 64 && (var0.y & 127) == 64) {
                  if(Client.tileLastDrawnActor[var2][var3] == Client.viewportDrawCount) {
                     return;
                  }

                  Client.tileLastDrawnActor[var2][var3] = Client.viewportDrawCount;
               }

               var0.tileHeight = WorldMapDecorationType.getTileHeight_base(var0.x, var0.y, ItemLayer.Client_plane);
               var0.playerCycle = Client.cycle;
               class36.scene.method5444(ItemLayer.Client_plane, var0.x, var0.y, var0.tileHeight, 60, var0, var0.rotation, var4, var0.isWalking);
            }
         }
      }

   }
}
