#!/bin/bash

# Mouse toggle script for Sway
# Toggles mouse position 60 pixels left/right on each execution
# Requires ydotool and ydotoold daemon running

# Configuration

### 1T KARAMBWANS
MOVE_DISTANCE=120
#RANDOM_NUM=$((RANDOM % 101))  # Generate 0-100
#RANDOM_DELAY=$(echo "scale=3; 0.5 + $RANDOM_NUM / 1000" | bc)
RANDOM_DELAY=0.575
RANDOM_DELAY_LEFT=0.05

### ANTIVENOM POTIONS 
#MOVE_DISTANCE=50
#RANDOM_DELAY=0.6
#RANDOM_DELAY_LEFT=0.6

### DRAGON JAVELINS
#MOVE_DISTANCE=50
#RANDOM_DELAY=0.1
#RANDOM_DELAY_LEFT=0.3

### PRAYER BONES
MOVE_DISTANCE=180
#RANDOM_NUM=$((RANDOM % 101))  # Generate 0-100
#RANDOM_DELAY=$(echo "scale=3; 0.5 + $RANDOM_NUM / 1000" | bc)
RANDOM_DELAY=0.100
RANDOM_DELAY_LEFT=0.05

STATE_FILE="/tmp/mouse_toggle_state"
TIMESTAMP_FILE="/tmp/mouse_toggle_timestamp"
ENABLED_FILE="/tmp/mouse_toggle_enabled"
LOCK_FILE="/tmp/mouse_toggle_lock"
RESET_TIMEOUT=1  # Reset to right after 1 second

# Check if ydotool is available
if ! command -v ydotool &> /dev/null; then
    echo "Error: ydotool is not installed. Install it with: sudo pacman -S ydotool"
    exit 1
fi

# Check if bc is available (needed for random delay calculation)
if ! command -v bc &> /dev/null; then
    echo "Error: bc is not installed. Install it with: sudo pacman -S bc"
    exit 1
fi

# Check if script is already running (prevent multiple instances)
if [ -f "$LOCK_FILE" ]; then
    echo "Script is already running, skipping..."
    exit 0
fi

# Create lock file
echo $$ > "$LOCK_FILE"

# Clean up lock file on exit
trap 'rm -f "$LOCK_FILE"' EXIT

# Check if script should toggle on/off or execute
if [ "$1" = "toggle" ]; then
    if [ -f "$ENABLED_FILE" ]; then
        rm "$ENABLED_FILE"
        echo "Mouse toggle script DISABLED"
    else
        echo "enabled" > "$ENABLED_FILE"
        echo "Mouse toggle script ENABLED"
    fi
    exit 0
fi

# Check if script is enabled
if [ ! -f "$ENABLED_FILE" ]; then
    echo "Mouse toggle script is DISABLED. Use '$0 toggle' to enable."
    exit 0
fi

# Check if ydotoold daemon is running
if ! pgrep -x "ydotoold" > /dev/null; then
    echo "Starting ydotoold daemon..."
    sudo ydotoold &
    sleep 1
fi

# Get current timestamp
CURRENT_TIME=$(date +%s)

# Read current state and timestamp
if [ -f "$STATE_FILE" ] && [ -f "$TIMESTAMP_FILE" ]; then
    LAST_ACTION=$(cat "$STATE_FILE")
    LAST_TIME=$(cat "$TIMESTAMP_FILE")
    TIME_DIFF=$((CURRENT_TIME - LAST_TIME))
    
    # If more than RESET_TIMEOUT seconds have passed, reset to "right"
    if [ $TIME_DIFF -gt $RESET_TIMEOUT ]; then
        echo "More than ${RESET_TIMEOUT}s since last action, resetting to move right first"
        LAST_ACTION="left"  # Set to left so next action will be right
    fi
else
    LAST_ACTION="left"  # Default to left so first action is right
fi

# Toggle the action and move mouse
if [ "$LAST_ACTION" = "right" ]; then
    # Last action was right, so move left
    echo "Moving mouse left ($MOVE_DISTANCE pixels) and clicking (delay: ${RANDOM_DELAY}s)"
    ydotool mousemove -- -$MOVE_DISTANCE 0
    sleep $RANDOM_DELAY  # Random delay between 0.3-0.5s
    ydotool click 0xC0  # Left click
    echo "left" > "$STATE_FILE"
    echo "$CURRENT_TIME" > "$TIMESTAMP_FILE"
else
    # Last action was left (or unknown), so move right
    echo "Moving mouse right ($MOVE_DISTANCE pixels) and clicking (delay: ${RANDOM_DELAY}s)"
    ydotool mousemove -- $MOVE_DISTANCE 0
    sleep $RANDOM_DELAY_LEFT  # Random delay between 0.1-0.2s
    ydotool click 0xC0  # Left click
    echo "right" > "$STATE_FILE"
    echo "$CURRENT_TIME" > "$TIMESTAMP_FILE"
fi
