package mapped;

import net.runelite.api.FriendsChatMember;
import net.runelite.api.FriendsChatRank;
import net.runelite.api.events.FriendsChatMemberJoined;
import net.runelite.api.events.FriendsChatMemberLeft;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSFriendsChat;
import net.runelite.rs.api.RSUser;
import net.runelite.rs.api.RSUsername;

@ObfuscatedName("qi")
public class FriendsChat extends UserList<FriendsChatMember> implements RSFriendsChat {
   @ObfuscatedName("hh")
   static String field4677;
   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "Ltt;"
   )
   final LoginType loginType;
   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "Lqz;"
   )
   final Usernamed localUser;
   @ObfuscatedName("ao")
   public String name;
   @ObfuscatedName("ab")
   public String owner;
   @ObfuscatedName("au")
   public byte minKick;
   @ObfuscatedName("aa")
   @ObfuscatedGetter(
      intValue = 1177118877
   )
   public int rank;
   @ObfuscatedName("ac")
   @ObfuscatedGetter(
      intValue = -1705938395
   )
   int field4676;

   @ObfuscatedSignature(
      descriptor = "(Ltt;Lqz;)V"
   )
   public FriendsChat(LoginType var1, Usernamed var2) {
      super(500);
      this.name = null;
      this.owner = null;
      this.field4676 = 1;
      this.loginType = var1;
      this.localUser = var2;
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(I)Lrf;",
      garbageValue = "794695059"
   )
   User vmethod8212() {
      return new ClanMate();
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(II)[Lrf;",
      garbageValue = "-190310061"
   )
   User[] vmethod8221(int var1) {
      return new ClanMate[var1];
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;I)V",
      garbageValue = "-2021517341"
   )
   final void method8085_base(String var1) {
      this.name = class128.method3069_base(var1);
   }

   @ObfuscatedName("al")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;B)V",
      garbageValue = "111"
   )
   final void method8086_base(String var1) {
      this.owner = class128.method3069_base(var1);
   }

   @ObfuscatedName("az")
   @ObfuscatedSignature(
      descriptor = "(Luj;IB)V",
      garbageValue = "103"
   )
   public final void method8089(Buffer var1, int var2) {
      this.method8086_base(var1.readStringCp1252NullTerminated_base());
      long var3 = var1.readLong_base();
      long var6 = var3;
      String var5;
      int var8;
      if(var3 > 0L && var3 < 6582952005840035281L) {
         if(var3 % 37L == 0L) {
            var5 = null;
         } else {
            var8 = 0;

            for(long var14 = var3; 0L != var14; var14 /= 37L) {
               ++var8;
            }

            StringBuilder var16 = new StringBuilder(var8);

            while(var6 != 0L) {
               long var17 = var6;
               var6 /= 37L;
               var16.append(class400.base37Table[(int)(var17 - 37L * var6)]);
            }

            var5 = var16.reverse().toString();
         }
      } else {
         var5 = null;
      }

      this.method8085_base(var5);
      this.minKick = var1.readByte_base();
      short var9;
      if(var2 == 1) {
         var8 = var1.readUnsignedByte_base();
         var9 = 255;
      } else {
         if(var2 != 2) {
            throw new IllegalArgumentException("Invalid friend chat full update version: " + var2);
         }

         var8 = var1.readShortSmartSub_base();
         var9 = -1;
      }

      if(var8 != var9) {
         int var10 = var8;
         this.clear_base();

         for(int var11 = 0; var11 < var10; ++var11) {
            ClanMate var12 = (ClanMate)this.method8190(new Username(var1.readStringCp1252NullTerminated_base(), this.loginType));
            int var13 = var1.readUnsignedShort_base();
            var12.set_base(var13, ++this.field4676 - 1);
            var12.rank = var1.readByte_base();
            var1.readStringCp1252NullTerminated_base();
            this.method8102(var12);
         }

      }
   }

   @ObfuscatedName("ap")
   @ObfuscatedSignature(
      descriptor = "(Luj;I)V",
      garbageValue = "-623451894"
   )
   public final void method8090(Buffer var1) {
      Username var2 = new Username(var1.readStringCp1252NullTerminated_base(), this.loginType);
      int var3 = var1.readUnsignedShort_base();
      byte var4 = var1.readByte_base();
      boolean var5 = false;
      if(var4 == -128) {
         var5 = true;
      }

      ClanMate var6;
      if(var5) {
         if(this.getSize_base() == 0) {
            return;
         }

         var6 = (ClanMate)this.method8222(var2);
         if(var6 != null && var6.getWorld_base() == var3) {
            this.method8189(var6);
         }
      } else {
         var1.readStringCp1252NullTerminated_base();
         var6 = (ClanMate)this.method8222(var2);
         if(var6 == null) {
            if(this.getSize_base() > super.capacity) {
               return;
            }

            var6 = (ClanMate)this.method8190(var2);
         }

         var6.set_base(var3, ++this.field4676 - 1);
         var6.rank = var4;
         this.method8102(var6);
      }

   }

   @ObfuscatedName("av")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-2120772646"
   )
   public final void clearFriends_base() {
      for(int var1 = 0; var1 < this.getSize_base(); ++var1) {
         ((ClanMate)this.method8192(var1)).clearIsFriend_base();
      }

   }

   @ObfuscatedName("ax")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "511267202"
   )
   public final void invalidateIgnoreds_base() {
      for(int var1 = 0; var1 < this.getSize_base(); ++var1) {
         ((ClanMate)this.method8192(var1)).clearIsIgnored_base();
      }

   }

   @ObfuscatedName("as")
   @ObfuscatedSignature(
      descriptor = "(Lrp;S)V",
      garbageValue = "-25332"
   )
   final void method8102(ClanMate var1) {
      if(var1.method8171().equals(this.localUser.vmethod8071())) {
         this.rank = var1.rank;
      }

   }

   public byte getMinKickRank() {
      return this.minKick;
   }

   public int getRank() {
      return this.rank;
   }

   public void rl$add(RSUsername var1, RSUsername var2) {
      FriendsChatMember var3 = (FriendsChatMember)this.findByName(var1);
      if(var3 != null) {
         FriendsChatMemberJoined var4 = new FriendsChatMemberJoined(var3);
         SpriteMask.client.getCallbacks().postDeferred(var4);
      }
   }

   public void rl$remove(RSUser var1) {
      FriendsChatMember var2 = (FriendsChatMember)this.findByName(var1.getRsName());
      if(var2 != null) {
         FriendsChatMemberLeft var3 = new FriendsChatMemberLeft(var2);
         SpriteMask.client.getCallbacks().postDeferred(var3);
      }
   }

   public FriendsChatRank getMyRank() {
      return FriendsChatRank.valueOf(this.getRank());
   }

   public FriendsChatRank getKickRank() {
      return FriendsChatRank.valueOf(this.getMinKickRank());
   }

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

   public String getOwner() {
      return this.owner;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(CI)C",
      garbageValue = "-350707745"
   )
   public static char method8113_base(char var0) {
      switch(var0) {
      case ' ':
      case '-':
      case '_':
      case ' ':
         return '_';
      case '#':
      case '[':
      case ']':
         return var0;
      case 'À':
      case 'Á':
      case 'Â':
      case 'Ã':
      case 'Ä':
      case 'à':
      case 'á':
      case 'â':
      case 'ã':
      case 'ä':
         return 'a';
      case 'Ç':
      case 'ç':
         return 'c';
      case 'È':
      case 'É':
      case 'Ê':
      case 'Ë':
      case 'è':
      case 'é':
      case 'ê':
      case 'ë':
         return 'e';
      case 'Í':
      case 'Î':
      case 'Ï':
      case 'í':
      case 'î':
      case 'ï':
         return 'i';
      case 'Ñ':
      case 'ñ':
         return 'n';
      case 'Ò':
      case 'Ó':
      case 'Ô':
      case 'Õ':
      case 'Ö':
      case 'ò':
      case 'ó':
      case 'ô':
      case 'õ':
      case 'ö':
         return 'o';
      case 'Ù':
      case 'Ú':
      case 'Û':
      case 'Ü':
      case 'ù':
      case 'ú':
      case 'û':
      case 'ü':
         return 'u';
      case 'ß':
         return 'b';
      case 'ÿ':
      case 'Ÿ':
         return 'y';
      default:
         return Character.toLowerCase(var0);
      }
   }

   @ObfuscatedName("cp")
   @ObfuscatedSignature(
      descriptor = "(ILmh;ZI)V",
      garbageValue = "-1919128859"
   )
   static void method8116(int var0, Coord var1, boolean var2) {
      WorldMapArea var3 = WorldMapArchiveLoader.method8711().method8799(var0);
      int var4 = class229.localPlayer.plane;
      int var5 = (class229.localPlayer.x >> 7) + NpcOverrides.baseX;
      int var6 = (class229.localPlayer.y >> 7) + class101.baseY;
      Coord var7 = new Coord(var4, var5, var6);
      WorldMapArchiveLoader.method8711().method8796(var3, var7, var1, var2);
   }
}
