package mapped;

import java.util.Iterator;
import net.runelite.api.FriendContainer;
import net.runelite.api.events.FriendAdded;
import net.runelite.api.events.RemovedFriend;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSFriendSystem;
import net.runelite.rs.api.RSIgnoreList;
import net.runelite.rs.api.RSUsername;

@ObfuscatedName("cj")
public class FriendSystem implements RSFriendSystem {
   @ObfuscatedName("oi")
   @ObfuscatedGetter(
      intValue = 271780873
   )
   static int menuX;
   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "Ltt;"
   )
   final LoginType loginType;
   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "Lqh;"
   )
   public final FriendsList friendsList;
   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "Lqs;"
   )
   public final IgnoreList ignoreList;
   @ObfuscatedName("aa")
   @ObfuscatedGetter(
      intValue = -1161414997
   )
   int field816;

   @ObfuscatedSignature(
      descriptor = "(Ltt;)V"
   )
   FriendSystem(LoginType var1) {
      this.field816 = 0;
      this.loginType = var1;
      this.friendsList = new FriendsList(var1);
      this.ignoreList = new IgnoreList(var1);
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(B)Z",
      garbageValue = "-69"
   )
   boolean method1902_base() {
      return this.field816 == 2;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-207858450"
   )
   final void method1929_base() {
      this.field816 = 1;
   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(Luj;II)V",
      garbageValue = "-1145524939"
   )
   final void method1904(Buffer var1, int var2) {
      this.friendsList.method8079(var1, var2);
      this.field816 = 2;

      for(int var3 = 0; var3 < Players.Players_count; ++var3) {
         Player var4 = Client.players[Players.Players_indices[var3]];
         var4.clearIsFriend_base();
      }

      Iterator var5 = Messages.Messages_hashTable.iterator();

      while(var5.hasNext()) {
         Message var6 = (Message)var5.next();
         var6.clearIsFromFriend_base();
      }

      if(class20.friendsChat != null) {
         class20.friendsChat.clearFriends_base();
      }

   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "1225915073"
   )
   final void processFriendUpdates_base() {
      for(FriendLoginUpdate var1 = (FriendLoginUpdate)this.friendsList.friendLoginUpdates.method7364(); var1 != null; var1 = (FriendLoginUpdate)this.friendsList.friendLoginUpdates.method7362()) {
         if((long)var1.field4683 < SpotAnimationDefinition.method3775_base() / 1000L - 5L) {
            if(var1.world > 0) {
               GrandExchangeOfferUnitPriceComparator.addGameMessage_base(5, "", var1.username + " has logged in.");
            }

            if(var1.world == 0) {
               GrandExchangeOfferUnitPriceComparator.addGameMessage_base(5, "", var1.username + " has logged out.");
            }

            var1.remove_base();
         }
      }

   }

   @ObfuscatedName("ab")
   @ObfuscatedSignature(
      descriptor = "(I)V",
      garbageValue = "-2013654454"
   )
   final void clear_base() {
      this.field816 = 0;
      this.friendsList.clear_base();
      this.ignoreList.clear_base();
   }

   @ObfuscatedName("au")
   @ObfuscatedSignature(
      descriptor = "(Lvj;ZI)Z",
      garbageValue = "1878561272"
   )
   final boolean method1946(Username var1, boolean var2) {
      return var1 == null?false:(var1.equals(class229.localPlayer.username)?true:this.friendsList.method8075(var1, var2));
   }

   @ObfuscatedName("aa")
   @ObfuscatedSignature(
      descriptor = "(Lvj;I)Z",
      garbageValue = "150326624"
   )
   final boolean method1908(Username var1) {
      return var1 == null?false:this.ignoreList.method8182(var1);
   }

   @ObfuscatedName("ac")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;I)V",
      garbageValue = "1173246505"
   )
   final void addFriend_base(String var1) {
      this.rl$addFriend(var1);
      if(var1 != null) {
         Username var2 = new Username(var1, this.loginType);
         if(var2.hasCleanName_base()) {
            if(this.friendsListIsFull_base()) {
               AttackOption.method2748_base();
            } else if(class229.localPlayer.username.equals(var2)) {
               class163.method3461_base();
            } else if(this.method1946(var2, false)) {
               class222.method4306_base(var1);
            } else if(this.method1908(var2)) {
               AbstractUserComparator.method8173_base(var1);
            } else {
               PacketBufferNode var3 = class113.method2919(ClientPacket.FRIENDLIST_ADD, Client.packetWriter.isaacCipher);
               var3.packetBuffer.writeByte_base(ClanChannel.stringCp1252NullTerminatedByteSize_base(var1));
               var3.packetBuffer.writeStringCp1252NullTerminated_base(var1);
               Client.packetWriter.method2867(var3);
            }
         }
      }
   }

   @ObfuscatedName("as")
   @ObfuscatedSignature(
      descriptor = "(I)Z",
      garbageValue = "-1130501973"
   )
   final boolean friendsListIsFull_base() {
      return this.friendsList.isFull_base() || this.friendsList.getSize_base() >= 200 && Client.field754 != 1;
   }

   @ObfuscatedName("ay")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;I)V",
      garbageValue = "2025283192"
   )
   final void addIgnore_base(String var1) {
      if(var1 != null) {
         Username var2 = new Username(var1, this.loginType);
         if(var2.hasCleanName_base()) {
            if(this.canAddIgnore_base()) {
               WorldMapArea.method4622_base("Your ignore list is full. Max of 100 for free users, and 400 for members");
            } else if(class229.localPlayer.username.equals(var2)) {
               class158.method3415_base();
            } else if(this.method1908(var2)) {
               WorldMapArea.method4622_base(var1 + " is already on your ignore list");
            } else if(this.method1946(var2, false)) {
               WorldMapArea.method4622_base("Please remove " + var1 + " from your friend list first");
            } else {
               ScriptFrame.method1213_base(var1);
            }
         }
      }
   }

   @ObfuscatedName("am")
   @ObfuscatedSignature(
      descriptor = "(I)Z",
      garbageValue = "431528192"
   )
   final boolean canAddIgnore_base() {
      return this.ignoreList.isFull_base() || this.ignoreList.getSize_base() >= 100 && Client.field754 != 1;
   }

   @ObfuscatedName("aq")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;B)V",
      garbageValue = "-125"
   )
   final void removeFriend_base(String var1) {
      this.rl$removeFriend(var1);
      if(var1 != null) {
         Username var2 = new Username(var1, this.loginType);
         if(var2.hasCleanName_base()) {
            if(this.friendsList.method8188(var2)) {
               JagexCache.method4214_base();
               PacketBufferNode var3 = class113.method2919(ClientPacket.FRIENDLIST_DEL, Client.packetWriter.isaacCipher);
               var3.packetBuffer.writeByte_base(ClanChannel.stringCp1252NullTerminatedByteSize_base(var1));
               var3.packetBuffer.writeStringCp1252NullTerminated_base(var1);
               Client.packetWriter.method2867(var3);
            }

            for(int var5 = 0; var5 < Players.Players_count; ++var5) {
               Player var4 = Client.players[Players.Players_indices[var5]];
               var4.clearIsFriend_base();
            }

            Iterator var6 = Messages.Messages_hashTable.iterator();

            while(var6.hasNext()) {
               Message var7 = (Message)var6.next();
               var7.clearIsFromFriend_base();
            }

            if(class20.friendsChat != null) {
               class20.friendsChat.clearFriends_base();
            }

         }
      }
   }

   @ObfuscatedName("ai")
   @ObfuscatedSignature(
      descriptor = "(Ljava/lang/String;ZI)V",
      garbageValue = "-462327739"
   )
   public final void removeIgnore_base(String var1, boolean var2) {
      if(var1 != null) {
         Username var3 = new Username(var1, this.loginType);
         if(var3.hasCleanName_base()) {
            if(this.ignoreList.method8188(var3)) {
               JagexCache.method4214_base();
               if(var2) {
                  PacketBufferNode var4 = class113.method2919(ClientPacket.IGNORELIST_DEL, Client.packetWriter.isaacCipher);
                  var4.packetBuffer.writeByte_base(ClanChannel.stringCp1252NullTerminatedByteSize_base(var1));
                  var4.packetBuffer.writeStringCp1252NullTerminated_base(var1);
                  Client.packetWriter.method2867(var4);
               }
            }

            DelayFadeTask.FriendSystem_invalidateIgnoreds_base();
         }
      }
   }

   @ObfuscatedName("ae")
   @ObfuscatedSignature(
      descriptor = "(Lvj;I)Z",
      garbageValue = "-94470483"
   )
   final boolean method1915(Username var1) {
      Friend var2 = (Friend)this.friendsList.method8185(var1);
      return var2 != null && var2.hasWorld_base();
   }

   public void rl$removeFriend(String var1) {
      RemovedFriend var2 = new RemovedFriend(SpriteMask.client.getFriendManager().getFriendContainer().findByName(var1));
      SpriteMask.client.getCallbacks().post(var2);
   }

   public void rl$addFriend(String var1) {
      FriendAdded var2 = new FriendAdded(var1);
      SpriteMask.client.getCallbacks().post(var2);
   }

   public FriendContainer getFriendContainer() {
      return this.friendsList;
   }

   public RSIgnoreList getIgnoreContainer() {
      return this.ignoreList;
   }

   public boolean isFriended(RSUsername var1, boolean var2) {
      return this.method1946((Username)var1, var2);
   }

   public boolean isIgnored(RSUsername var1) {
      return this.method1908((Username)var1);
   }

   public void addFriend(String var1) {
      this.addFriend_base(var1);
   }

   public void addIgnore(String var1) {
      this.addIgnore_base(var1);
   }

   public void removeFriend(String var1) {
      this.removeFriend_base(var1);
   }

   public void removeIgnore(String var1, boolean var2) {
      this.removeIgnore_base(var1, var2);
   }
}
