package mapped;

import net.runelite.api.Item;
import net.runelite.api.events.ItemContainerChanged;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSItemContainer;
import net.runelite.rs.api.RSNodeHashTable;

@ObfuscatedName("du")
public class ItemContainer extends Node implements RSItemContainer {
   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "Ltw;"
   )
   static NodeHashTable itemContainers;
   @ObfuscatedName("tz")
   @ObfuscatedSignature(
      descriptor = "Lgn;"
   )
   static ClanChannel guestClanChannel;
   @ObfuscatedName("ah")
   int[] ids;
   @ObfuscatedName("ar")
   int[] quantities;

   static {
      itemContainers = new NodeHashTable(32);
   }

   ItemContainer() {
      this.ids = new int[]{-1};
      this.quantities = new int[]{0};
   }

   public int[] getItemIds() {
      return this.ids;
   }

   public int[] getStackSizes() {
      return this.quantities;
   }

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

   public Item[] getItems() {
      int[] var1 = this.getItemIds();
      int[] var2 = this.getStackSizes();
      Item[] var3 = new Item[var1.length];

      for(int var4 = 0; var4 < var1.length; ++var4) {
         Item var5 = new Item(var1[var4], var2[var4]);
         var3[var4] = var5;
      }

      return var3;
   }

   public Item getItem(int var1) {
      int[] var2 = this.getItemIds();
      int[] var3 = this.getStackSizes();
      return var1 >= 0 && var1 < var2.length && var2[var1] != -1?new Item(var2[var1], var3[var1]):null;
   }

   public boolean contains(int var1) {
      int[] var2 = this.getItemIds();
      int[] var3 = var2;
      int var4 = var2.length;

      for(int var5 = 0; var5 < var4; ++var5) {
         int var6 = var3[var5];
         if(var6 == var1) {
            return true;
         }
      }

      return false;
   }

   public int count(int var1) {
      int[] var2 = this.getItemIds();
      int[] var3 = this.getStackSizes();
      int var4 = 0;

      for(int var5 = 0; var5 < var2.length; ++var5) {
         if(var2[var5] == var1) {
            int var6 = var3[var5];
            if(var6 > 1) {
               return var6;
            }

            ++var4;
         }
      }

      return var4;
   }

   public int size() {
      return this.getItemIds().length;
   }

   public int count() {
      int var1 = 0;
      int[] var2 = this.getItemIds();
      int var3 = var2.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         int var5 = var2[var4];
         if(var5 != -1) {
            ++var1;
         }
      }

      return var1;
   }

   public static void onItemContainerUpdate(int var0) {
      if(var0 != -1) {
         int var1 = var0 - 1 & 31;
         int var2 = Client.changedItemContainers[var1];
         RSNodeHashTable var3 = SpriteMask.client.getItemContainers();
         RSItemContainer var4 = (RSItemContainer)var3.get((long)var2);
         RSItemContainer var5 = (RSItemContainer)var3.get((long)(var2 | 32768));
         ItemContainerChanged var6;
         if(var4 != null) {
            var6 = new ItemContainerChanged(var2, var4);
            SpriteMask.client.getCallbacks().postDeferred(var6);
         }

         if(var5 != null) {
            var6 = new ItemContainerChanged(var2 | 32768, var5);
            SpriteMask.client.getCallbacks().postDeferred(var6);
         }
      }

   }
}
