package mapped;

import net.runelite.api.events.ItemQuantityChanged;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSTile;
import net.runelite.rs.api.RSTileItem;

@ObfuscatedName("ec")
public final class TileItem extends Renderable implements RSTileItem {
   @ObfuscatedName("at")
   @ObfuscatedGetter(
      intValue = -1683425433
   )
   int id;
   @ObfuscatedName("ah")
   @ObfuscatedGetter(
      intValue = 232562963
   )
   int quantity;
   @ObfuscatedName("ao")
   @ObfuscatedGetter(
      intValue = 553790659
   )
   int field1342;
   public int rl$sceneY;
   public int rl$sceneX;

   TileItem() {
      this.field1342 = 31;
      this.rl$$init();
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "-2048402866"
   )
   void method2754_base(int var1) {
      this.field1342 = var1;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(B)Lka;",
      garbageValue = "1"
   )
   protected final Model vmethod5504() {
      return class214.method4226(this.id).method3956(this.quantity);
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(II)Z",
      garbageValue = "117367628"
   )
   boolean method2755_base(int var1) {
      return var1 >= 0 && var1 <= 4?(this.field1342 & 1 << var1) != 0:true;
   }

   public void onUnlink() {
      if(this.rl$sceneX != -1 && SpriteMask.client.getLastItemDespawn() == null) {
         SpriteMask.client.setLastItemDespawn(this);
      }

   }

   private void rl$$init() {
      this.rl$sceneX = -1;
      this.rl$sceneY = -1;
   }

   public int getId() {
      return this.id;
   }

   public int getQuantity() {
      return this.quantity;
   }

   public net.runelite.api.Tile getTile() {
      int var1 = this.rl$sceneX;
      int var2 = this.rl$sceneY;
      if(var1 != -1 && var2 != -1) {
         RSTile[][][] var3 = SpriteMask.client.getScene().getTiles();
         return var3[SpriteMask.client.getPlane()][var1][var2];
      } else {
         return null;
      }
   }

   public void quantityChanged(int var1) {
      if(this.rl$sceneX != -1) {
         SpriteMask.client.getLogger().debug("Item quantity changed: {} ({} -> {})", new Object[]{Integer.valueOf(this.getId()), Integer.valueOf(this.getQuantity()), Integer.valueOf(var1)});
         ItemQuantityChanged var2 = new ItemQuantityChanged(this, this.getTile(), this.getQuantity(), var1);
         SpriteMask.client.getCallbacks().post(var2);
      }

   }

   public int getX() {
      return this.rl$sceneX;
   }

   public void setX(int var1) {
      this.rl$sceneX = var1;
   }

   public int getY() {
      return this.rl$sceneY;
   }

   public void setY(int var1) {
      this.rl$sceneY = var1;
   }

   public void setId(int var1) {
      this.id = var1;
   }

   public void setQuantity(int var1) {
      this.quantity = var1;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(CLof;I)C",
      garbageValue = "938832228"
   )
   static char method2765(char var0, Language var1) {
      if(var0 >= 192 && var0 <= 255) {
         if(var0 >= 192 && var0 <= 198) {
            return 'A';
         }

         if(var0 == 199) {
            return 'C';
         }

         if(var0 >= 200 && var0 <= 203) {
            return 'E';
         }

         if(var0 >= 204 && var0 <= 207) {
            return 'I';
         }

         if(var0 == 209 && var1 != Language.Language_ES) {
            return 'N';
         }

         if(var0 >= 210 && var0 <= 214) {
            return 'O';
         }

         if(var0 >= 217 && var0 <= 220) {
            return 'U';
         }

         if(var0 == 221) {
            return 'Y';
         }

         if(var0 == 223) {
            return 's';
         }

         if(var0 >= 224 && var0 <= 230) {
            return 'a';
         }

         if(var0 == 231) {
            return 'c';
         }

         if(var0 >= 232 && var0 <= 235) {
            return 'e';
         }

         if(var0 >= 236 && var0 <= 239) {
            return 'i';
         }

         if(var0 == 241 && var1 != Language.Language_ES) {
            return 'n';
         }

         if(var0 >= 242 && var0 <= 246) {
            return 'o';
         }

         if(var0 >= 249 && var0 <= 252) {
            return 'u';
         }

         if(var0 == 253 || var0 == 255) {
            return 'y';
         }
      }

      if(var0 == 338) {
         return 'O';
      } else if(var0 == 339) {
         return 'o';
      } else if(var0 == 376) {
         return 'Y';
      } else {
         return var0;
      }
   }

   @ObfuscatedName("kq")
   @ObfuscatedSignature(
      descriptor = "(IIIIIIIIIII)V",
      garbageValue = "2136163663"
   )
   static final void method2766_base(int var0, int var1, int var2, int var3, int var4, int var5, int var6, int var7, int var8, int var9) {
      PendingSpawn var10 = null;

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

      if(var10 == null) {
         var10 = new PendingSpawn();
         var10.plane = var0;
         var10.type = var3;
         var10.x = var1;
         var10.y = var2;
         var10.field1152 = -1;
         ScriptFrame.method1212(var10);
         Client.pendingSpawns.method7233(var10);
      }

      var10.field1162 = var4;
      var10.field1161 = var5;
      var10.field1160 = var6;
      var10.delay = var8;
      var10.hitpoints = var9;
      var10.method2453_base(var7);
   }
}
