package mapped;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import net.runelite.mapping.ObfuscatedGetter;
import net.runelite.mapping.ObfuscatedName;
import net.runelite.mapping.ObfuscatedSignature;
import net.runelite.rs.api.RSMouseHandler;

@ObfuscatedName("bp")
public class MouseHandler implements MouseListener, MouseMotionListener, FocusListener, RSMouseHandler {
   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "Lbp;"
   )
   public static MouseHandler MouseHandler_instance;
   @ObfuscatedName("ab")
   @ObfuscatedGetter(
      intValue = -1953718231
   )
   static volatile int MouseHandler_idleCycles;
   @ObfuscatedName("au")
   @ObfuscatedGetter(
      intValue = 2084494689
   )
   public static volatile int MouseHandler_currentButtonVolatile;
   @ObfuscatedName("aa")
   @ObfuscatedGetter(
      intValue = -1039777345
   )
   public static volatile int MouseHandler_xVolatile;
   @ObfuscatedName("ac")
   @ObfuscatedGetter(
      intValue = -2049825775
   )
   public static volatile int MouseHandler_yVolatile;
   @ObfuscatedName("al")
   @ObfuscatedGetter(
      longValue = -1900724714987571041L
   )
   public static volatile long MouseHandler_lastMovedVolatile;
   @ObfuscatedName("az")
   @ObfuscatedGetter(
      intValue = -2023273643
   )
   public static int MouseHandler_currentButton;
   @ObfuscatedName("ap")
   @ObfuscatedGetter(
      intValue = -948387119
   )
   public static int MouseHandler_x;
   @ObfuscatedName("av")
   @ObfuscatedGetter(
      intValue = -357791911
   )
   public static int MouseHandler_y;
   @ObfuscatedName("ax")
   @ObfuscatedGetter(
      longValue = 3969876424504661119L
   )
   public static long MouseHandler_millis;
   @ObfuscatedName("as")
   @ObfuscatedGetter(
      intValue = 1660798155
   )
   public static volatile int MouseHandler_lastButtonVolatile;
   @ObfuscatedName("ay")
   @ObfuscatedGetter(
      intValue = 720496543
   )
   public static volatile int MouseHandler_lastPressedXVolatile;
   @ObfuscatedName("ak")
   @ObfuscatedGetter(
      intValue = 1036059975
   )
   public static volatile int MouseHandler_lastPressedYVolatile;
   @ObfuscatedName("aj")
   @ObfuscatedGetter(
      longValue = -1184323502833097981L
   )
   public static volatile long MouseHandler_lastPressedTimeMillisVolatile;
   @ObfuscatedName("am")
   @ObfuscatedGetter(
      intValue = 1454500269
   )
   public static int MouseHandler_lastButton;
   @ObfuscatedName("aq")
   @ObfuscatedGetter(
      intValue = -1672841831
   )
   public static int MouseHandler_lastPressedX;
   @ObfuscatedName("ai")
   @ObfuscatedGetter(
      intValue = 1663390927
   )
   public static int MouseHandler_lastPressedY;
   @ObfuscatedName("aw")
   @ObfuscatedGetter(
      longValue = 5746421217152333253L
   )
   public static long MouseHandler_lastPressedTimeMillis;
   public int isInEvent;

   static {
      MouseHandler_instance = new MouseHandler();
      MouseHandler_idleCycles = 0;
      MouseHandler_currentButtonVolatile = 0;
      MouseHandler_xVolatile = -1;
      MouseHandler_yVolatile = -1;
      MouseHandler_lastMovedVolatile = -1L;
      MouseHandler_currentButton = 0;
      MouseHandler_x = 0;
      MouseHandler_y = 0;
      MouseHandler_millis = 0L;
      MouseHandler_lastButtonVolatile = 0;
      MouseHandler_lastPressedXVolatile = 0;
      MouseHandler_lastPressedYVolatile = 0;
      MouseHandler_lastPressedTimeMillisVolatile = 0L;
      MouseHandler_lastButton = 0;
      MouseHandler_lastPressedX = 0;
      MouseHandler_lastPressedY = 0;
      MouseHandler_lastPressedTimeMillis = 0L;
   }

   @ObfuscatedName("ao")
   @ObfuscatedSignature(
      descriptor = "(Ljava/awt/event/MouseEvent;B)I",
      garbageValue = "115"
   )
   final int getButton_base(MouseEvent var1) {
      int var2 = var1.getButton();
      return !var1.isAltDown() && var2 != 2?(!var1.isMetaDown() && var2 != 3?1:2):4;
   }

   public final synchronized void mouseMoved(MouseEvent var1) {
      if(this.isInEvent == 0) {
         var1 = SpriteMask.client.getCallbacks().mouseMoved(var1);
      }

      if(!var1.isConsumed()) {
         ++this.isInEvent;

         try {
            this.copy$mouseMoved(var1);
         } finally {
            --this.isInEvent;
         }
      }

   }

   public final void copy$mouseClicked(MouseEvent var1) {
      if(var1.isPopupTrigger()) {
         var1.consume();
      }

   }

   public final synchronized void copy$mousePressed(MouseEvent var1) {
      if(MouseHandler_instance != null) {
         MouseHandler_idleCycles = 0;
         Client.onIdleCyclesChanged(-1);
         MouseHandler_lastPressedXVolatile = var1.getX();
         MouseHandler_lastPressedYVolatile = var1.getY();
         MouseHandler_lastPressedTimeMillisVolatile = SpotAnimationDefinition.method3775_base();
         MouseHandler_lastButtonVolatile = this.getButton_base(var1);
         if(MouseHandler_lastButtonVolatile != 0) {
            MouseHandler_currentButtonVolatile = MouseHandler_lastButtonVolatile;
         }
      }

      if(var1.isPopupTrigger()) {
         var1.consume();
      }

   }

   public final synchronized void copy$mouseReleased(MouseEvent var1) {
      if(MouseHandler_instance != null) {
         MouseHandler_idleCycles = 0;
         Client.onIdleCyclesChanged(-1);
         MouseHandler_currentButtonVolatile = 0;
      }

      if(var1.isPopupTrigger()) {
         var1.consume();
      }

   }

   public final synchronized void copy$mouseEntered(MouseEvent var1) {
      this.mouseMoved(var1);
   }

   public final synchronized void copy$mouseExited(MouseEvent var1) {
      if(MouseHandler_instance != null) {
         MouseHandler_idleCycles = 0;
         Client.onIdleCyclesChanged(-1);
         MouseHandler_xVolatile = -1;
         MouseHandler_yVolatile = -1;
         MouseHandler_lastMovedVolatile = var1.getWhen();
      }

   }

   public final synchronized void copy$mouseDragged(MouseEvent var1) {
      this.mouseMoved(var1);
   }

   public final synchronized void copy$mouseMoved(MouseEvent var1) {
      if(MouseHandler_instance != null) {
         MouseHandler_idleCycles = 0;
         Client.onIdleCyclesChanged(-1);
         MouseHandler_xVolatile = var1.getX();
         MouseHandler_yVolatile = var1.getY();
         MouseHandler_lastMovedVolatile = var1.getWhen();
      }

   }

   public final synchronized void mousePressed(MouseEvent var1) {
      if(this.isInEvent == 0) {
         var1 = SpriteMask.client.getCallbacks().mousePressed(var1);
      }

      if(!var1.isConsumed()) {
         ++this.isInEvent;

         try {
            this.copy$mousePressed(var1);
         } finally {
            --this.isInEvent;
         }
      }

   }

   public final void mouseClicked(MouseEvent var1) {
      var1 = SpriteMask.client.getCallbacks().mouseClicked(var1);
      if(!var1.isConsumed()) {
         this.copy$mouseClicked(var1);
      }

   }

   public final synchronized void mouseDragged(MouseEvent var1) {
      if(this.isInEvent == 0) {
         var1 = SpriteMask.client.getCallbacks().mouseDragged(var1);
      }

      if(!var1.isConsumed()) {
         ++this.isInEvent;

         try {
            this.copy$mouseDragged(var1);
         } finally {
            --this.isInEvent;
         }
      }

   }

   public final synchronized void mouseReleased(MouseEvent var1) {
      if(this.isInEvent == 0) {
         var1 = SpriteMask.client.getCallbacks().mouseReleased(var1);
      }

      if(!var1.isConsumed()) {
         ++this.isInEvent;

         try {
            this.copy$mouseReleased(var1);
         } finally {
            --this.isInEvent;
         }
      }

   }

   public final synchronized void focusLost(FocusEvent var1) {
      if(MouseHandler_instance != null) {
         MouseHandler_currentButtonVolatile = 0;
      }

   }

   public final void focusGained(FocusEvent var1) {
   }

   public final synchronized void mouseEntered(MouseEvent var1) {
      if(this.isInEvent == 0) {
         var1 = SpriteMask.client.getCallbacks().mouseEntered(var1);
      }

      if(!var1.isConsumed()) {
         ++this.isInEvent;

         try {
            this.copy$mouseEntered(var1);
         } finally {
            --this.isInEvent;
         }
      }

   }

   public final synchronized void mouseExited(MouseEvent var1) {
      if(this.isInEvent == 0) {
         var1 = SpriteMask.client.getCallbacks().mouseExited(var1);
      }

      if(!var1.isConsumed()) {
         ++this.isInEvent;

         try {
            this.copy$mouseExited(var1);
         } finally {
            --this.isInEvent;
         }
      }

   }

   @ObfuscatedName("ar")
   @ObfuscatedSignature(
      descriptor = "(IIIII)V",
      garbageValue = "-427551211"
   )
   static final void method670_base(int var0, int var1, int var2, int var3) {
      for(int var4 = var1; var4 <= var3 + var1; ++var4) {
         for(int var5 = var0; var5 <= var0 + var2; ++var5) {
            if(var5 >= 0 && var5 < 104 && var4 >= 0 && var4 < 104) {
               class93.Tiles_underlays2[0][var5][var4] = 127;
               if(var0 == var5 && var5 > 0) {
                  Tiles.Tiles_heights[0][var5][var4] = Tiles.Tiles_heights[0][var5 - 1][var4];
               }

               if(var0 + var2 == var5 && var5 < 103) {
                  Tiles.Tiles_heights[0][var5][var4] = Tiles.Tiles_heights[0][var5 + 1][var4];
               }

               if(var4 == var1 && var4 > 0) {
                  Tiles.Tiles_heights[0][var5][var4] = Tiles.Tiles_heights[0][var5][var4 - 1];
               }

               if(var3 + var1 == var4 && var4 < 103) {
                  Tiles.Tiles_heights[0][var5][var4] = Tiles.Tiles_heights[0][var5][var4 + 1];
               }
            }
         }
      }

   }
}
