package mapped;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import net.runelite.api.events.ClanMemberJoined;
import net.runelite.api.events.ClanMemberLeft;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSClanChannel;
import net.runelite.rs.api.RSClanChannelMember;

@ObfuscatedName("gn")
public class ClanChannel extends Node implements RSClanChannel {
   @ObfuscatedName("aw")
   public static String field1764;
   @ObfuscatedName("at")
   boolean field1770;
   @ObfuscatedName("ah")
   boolean field1765;
   @ObfuscatedName("ar")
   public List members;
   @ObfuscatedName("ao")
   int[] sortedMembers;
   @ObfuscatedName("ab")
   @ObfuscatedGetter(
      longValue = -4051346480643886815L
   )
   public long field1772;
   @ObfuscatedName("au")
   public String name;
   @ObfuscatedName("aa")
   public byte field1766;
   @ObfuscatedName("ac")
   public byte field1771;

   static {
      new BitSet(65536);
   }

   @ObfuscatedSignature(
      descriptor = "(Luj;)V"
   )
   public ClanChannel(Buffer var1) {
      this.field1765 = true;
      this.name = null;
      this.method3481(var1);
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(B)[I",
      garbageValue = "85"
   )
   public int[] getSortedMembers_base() {
      if(this.sortedMembers == null) {
         String[] var1 = new String[this.members.size()];
         this.sortedMembers = new int[this.members.size()];

         for(int var2 = 0; var2 < this.members.size(); this.sortedMembers[var2] = var2++) {
            var1[var2] = ((ClanChannelMember)this.members.get(var2)).username.method9937_base();
         }

         int[] var3 = this.sortedMembers;
         class475.method8597_base(var1, var3, 0, var1.length - 1);
      }

      return this.sortedMembers;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(Lft;I)V",
      garbageValue = "1476937216"
   )
   void method3465(ClanChannelMember var1) {
      this.members.add(var1);
      this.sortedMembers = null;
      this.onMemberAdded(var1);
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(IB)V",
      garbageValue = "93"
   )
   void removeMember_base(int var1) {
      this.onMemberRemoved(var1);
      this.members.remove(var1);
      this.sortedMembers = null;
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(B)I",
      garbageValue = "0"
   )
   public int method3479_base() {
      return this.members.size();
   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;B)I",
      garbageValue = "-62"
   )
   public int method3484_base(String var1) {
      if(!this.field1765) {
         throw new RuntimeException("Displaynames not available");
      } else {
         for(int var2 = 0; var2 < this.members.size(); ++var2) {
            if(((ClanChannelMember)this.members.get(var2)).username.getName_base().equalsIgnoreCase(var1)) {
               return var2;
            }
         }

         return -1;
      }
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(Luj;B)V",
      garbageValue = "92"
   )
   void method3481(Buffer var1) {
      int var2 = var1.readUnsignedByte_base();
      if((var2 & 1) != 0) {
         this.field1770 = true;
      }

      if((var2 & 2) != 0) {
         this.field1765 = true;
      }

      int var3 = 2;
      if((var2 & 4) != 0) {
         var3 = var1.readUnsignedByte_base();
      }

      super.key = var1.readLong_base();
      this.field1772 = var1.readLong_base();
      this.name = var1.readStringCp1252NullTerminated_base();
      var1.readBoolean_base();
      this.field1771 = var1.readByte_base();
      this.field1766 = var1.readByte_base();
      int var4 = var1.readUnsignedShort_base();
      if(var4 > 0) {
         this.members = new ArrayList(var4);

         for(int var5 = 0; var5 < var4; ++var5) {
            ClanChannelMember var6 = new ClanChannelMember();
            if(this.field1770) {
               var1.readLong_base();
            }

            if(this.field1765) {
               var6.username = new Username(var1.readStringCp1252NullTerminated_base());
            }

            var6.rank = var1.readByte_base();
            var6.world = var1.readUnsignedShort_base();
            if(var3 >= 3) {
               var1.readBoolean_base();
            }

            this.members.add(var5, var6);
         }
      }

   }

   public void onMemberRemoved(int var1) {
      SpriteMask.client.getCallbacks().post(new ClanMemberLeft(this, (net.runelite.api.clan.ClanChannelMember)this.getMembers().get(var1)));
   }

   public List getMembers() {
      return this.members;
   }

   public int[] getSortedMembers() {
      return this.getSortedMembers_base();
   }

   public void onMemberAdded(RSClanChannelMember var1) {
      SpriteMask.client.getCallbacks().post(new ClanMemberJoined(this, var1));
   }

   public net.runelite.api.clan.ClanChannelMember findMember(String var1) {
      var1 = var1.replace(" ", " ");
      List var2 = this.getMembers();
      int[] var3 = this.getSortedMembers();
      int var4 = 0;
      int var5 = var3.length - 1;

      while(var4 <= var5) {
         int var6 = var4 + var5 >>> 1;
         int var7 = var3[var6];
         RSClanChannelMember var8 = (RSClanChannelMember)var2.get(var7);
         int var9 = var8.getName().compareTo(var1);
         if(var9 < 0) {
            var4 = var6 + 1;
         } else {
            if(var9 <= 0) {
               return var8;
            }

            var5 = var6 - 1;
         }
      }

      return null;
   }

   public String getName() {
      return this.name;
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(IB)Z",
      garbageValue = "-18"
   )
   public static boolean method3483_base(int var0) {
      return (var0 >> 22 & 1) != 0;
   }

   @ObfuscatedName("am")
   @ObfuscatedSignature(
      descriptor = "(IIB)I",
      garbageValue = "-73"
   )
   static final int method3482_base(int var0, int var1) {
      if(var0 == -1) {
         return 12345678;
      } else {
         var1 = (var0 & 127) * var1 / 128;
         if(var1 < 2) {
            var1 = 2;
         } else if(var1 > 126) {
            var1 = 126;
         }

         return (var0 & 65408) + var1;
      }
   }

   @ObfuscatedName("bx")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;I)I",
      garbageValue = "1458009071"
   )
   public static int stringCp1252NullTerminatedByteSize_base(String var0) {
      return var0.length() + 1;
   }

   @ObfuscatedName("ml")
   @ObfuscatedSignature(
      descriptor = "(II)V",
      garbageValue = "-1117566535"
   )
   static final void method3471_base(int var0) {
      if(HealthBarDefinition.widgetDefinition.loadInterface_base(var0)) {
         SoundCache.method893(HealthBarDefinition.widgetDefinition.Widget_interfaceComponents[var0], -1);
      }
   }
}
