package mapped;

import java.util.Map;
import net.runelite.api.ChatMessageType;
import net.runelite.api.events.ChatMessage;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSArchiveLoader;
import net.runelite.rs.api.RSChatChannel;
import net.runelite.rs.api.RSMessage;
import org.slf4j.Logger;

@ObfuscatedName("dh")
public class ArchiveLoader implements RSArchiveLoader {
   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "Loh;"
   )
   final Archive archive;
   @ObfuscatedName("ar")
   @ObfuscatedGetter(
      intValue = -872314017
   )
   final int groupCount;
   @ObfuscatedName("ao")
   @ObfuscatedGetter(
      intValue = 2111746149
   )
   int loadedCount;

   @ObfuscatedSignature(
      descriptor = "(Loh;Ljava/lang/String;)V"
   )
   ArchiveLoader(Archive var1, String var2) {
      this.loadedCount = 0;
      this.archive = var1;
      this.groupCount = var1.getGroupCount_base();
   }

   @ObfuscatedName("at")
   @ObfuscatedSignature(
      descriptor = "(I)Z",
      garbageValue = "2136927523"
   )
   boolean isLoaded_base() {
      this.loadedCount = 0;

      for(int var1 = 0; var1 < this.groupCount; ++var1) {
         if(!this.archive.method6909_base(var1) || this.archive.method6908_base(var1)) {
            ++this.loadedCount;
         }
      }

      return this.loadedCount >= this.groupCount;
   }

   @ObfuscatedName("at")
   public static final int method2328_base(double var0, double var2, double var4) {
      double var6 = var4;
      double var8 = var4;
      double var10 = var4;
      if(0.0D != var2) {
         double var12;
         if(var4 < 0.5D) {
            var12 = (var2 + 1.0D) * var4;
         } else {
            var12 = var2 + var4 - var2 * var4;
         }

         double var14 = 2.0D * var4 - var12;
         double var16 = var0 + 0.3333333333333333D;
         if(var16 > 1.0D) {
            --var16;
         }

         double var20 = var0 - 0.3333333333333333D;
         if(var20 < 0.0D) {
            ++var20;
         }

         if(6.0D * var16 < 1.0D) {
            var6 = (var12 - var14) * 6.0D * var16 + var14;
         } else if(var16 * 2.0D < 1.0D) {
            var6 = var12;
         } else if(3.0D * var16 < 2.0D) {
            var6 = 6.0D * (0.6666666666666666D - var16) * (var12 - var14) + var14;
         } else {
            var6 = var14;
         }

         if(6.0D * var0 < 1.0D) {
            var8 = var14 + 6.0D * (var12 - var14) * var0;
         } else if(var0 * 2.0D < 1.0D) {
            var8 = var12;
         } else if(var0 * 3.0D < 2.0D) {
            var8 = var14 + (var12 - var14) * (0.6666666666666666D - var0) * 6.0D;
         } else {
            var8 = var14;
         }

         if(6.0D * var20 < 1.0D) {
            var10 = var14 + var20 * 6.0D * (var12 - var14);
         } else if(2.0D * var20 < 1.0D) {
            var10 = var12;
         } else if(var20 * 3.0D < 2.0D) {
            var10 = var14 + 6.0D * (0.6666666666666666D - var20) * (var12 - var14);
         } else {
            var10 = var14;
         }
      }

      int var22 = (int)(var6 * 256.0D);
      int var13 = (int)(var8 * 256.0D);
      int var23 = (int)(var10 * 256.0D);
      int var15 = var23 + (var13 << 8) + (var22 << 16);
      return var15;
   }

   @ObfuscatedName("ah")
   @ObfuscatedSignature(
      descriptor = "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V",
      garbageValue = "2035865306"
   )
   static void addChatMessage_base(int var0, String var1, String var2, String var3) {
      Client.copy$addChatMessage(var0, var1, var2, var3, 2035865306);
      Map var4 = SpriteMask.client.getChatLineMap();
      RSChatChannel var5 = (RSChatChannel)var4.get(Integer.valueOf(var0));
      RSMessage var6 = var5.getLines()[0];
      ChatMessageType var7 = ChatMessageType.of(var0);
      if(var7 == ChatMessageType.CLAN_CHAT && var2 != null && var2.startsWith("|")) {
         var7 = ChatMessageType.CLAN_GIM_CHAT;
         var2 = var2.substring(1);
      }

      if(var7 == ChatMessageType.CLAN_MESSAGE && var2 != null && var2.startsWith("|")) {
         var7 = ChatMessageType.CLAN_GIM_MESSAGE;
         var2 = var2.substring(1);
      }

      Logger var8 = SpriteMask.client.getLogger();
      if(var8.isDebugEnabled()) {
         ChatMessageType var9 = ChatMessageType.of(var0);
         var8.debug("Chat message type {}: {}", var9 == ChatMessageType.UNKNOWN?String.valueOf(var0):var7.name(), var2);
      }

      ChatMessage var10 = new ChatMessage(var6, var7, var1, var2, var3, var6.getTimestamp());
      SpriteMask.client.getCallbacks().post(var10);
   }
}
