#!/usr/bin/env bash

SOUND_DIR="$HOME/.config/hypr/sound"
PLAYER="play"
VOLUME="0.1"

declare -A SOUND_MAP=(
    ["workspace"]="futuristic-click.wav"
    ["activewindow"]="popup.wav"
    ["activelayout"]="popup.wav"
    ["fullscreen"]="fullscreen.wav"
    ["openwindow"]="open.mp3"
    ["closewindow"]="close_2.wav"
    ["movewindow"]="popup.wav"
    ["changefloatingmode"]="float.wav"
)

declare -A EVENT_ENABLED=(
    ["workspace"]=1
    ["activewindow"]=1
    ["activelayout"]=0
    ["fullscreen"]=1
    ["openwindow"]=1
    ["closewindow"]=1
    ["movewindow"]=1
    ["changefloatingmode"]=1
)

declare -A EVENT_COOLDOWN=(
    ["activewindow"]=0.1
    ["movewindow"]=0.2
    ["changefloatingmode"]=0.3
)

DEBUG=1
declare -a SOUND_QUEUE=()
declare -A COOLDOWN_TIMERS=()

mkdir -p "$SOUND_DIR"

cp -r $SOUND_DIR "/tmp/hypr-sound"
SOUND_DIR="/tmp/hypr-sound"

log() {
    if [ $DEBUG -eq 1 ]; then
        echo "[$(date '+%H:%M:%S')] $1" >&2
    fi
}

play_sound() {
    local event="$1"
    local sound_file="$2"
    local full_path="$SOUND_DIR/$sound_file"
    
    if [ ! -f "$full_path" ]; then
        log "Sound file not found: $full_path"
        return 1
    fi
    
    log "Playing: $event -> $sound_file"
    
    play -v $VOLUME "$full_path" &
    
    return 0
}

handle_event() {
    local raw_event="$1"
    local event_type=""
    local event_data=""
    
    if [[ "$raw_event" == *">>"* ]]; then
        event_type="${raw_event%%>>*}"
        event_data="${raw_event#*>>}"
    else
        event_type="$raw_event"
    fi
    
    log "Received event: $event_type (data: $event_data)"
    
    case "$event_type" in
        "workspace"*)
            if [[ "$event_data" == *,* ]]; then
                trigger_event "workspace" "$event_data"
            fi
            ;;
        "activelayout"*)
            trigger_event "activelayout" "$event_data"
            ;;
        "fullscreen"*)
            trigger_event "fullscreen" "$event_data"
            ;;
        "openwindow"*)
            trigger_event "openwindow" "$event_data"
            ;;
        "closewindow"*)
            trigger_event "closewindow" "$event_data"
            ;;
        "movewindow"*)
            if [[ "$event_data" == *,*,* ]]; then
                trigger_event "movewindow" "$event_data"
            fi
            ;;
        "changefloating"*)
            trigger_event "changefloatingmode" "$event_data"
            ;;
        *)
            log "Unknown event: $event_type"
            ;;
    esac
}

trigger_event() {
    local event="$1"
    local data="$2"
    
    if [ "${EVENT_ENABLED[$event]:-0}" -ne 1 ]; then
        log "Event $event is disabled"
        return
    fi
    
    local sound_file="${SOUND_MAP[$event]}"
    
    if [ -z "$sound_file" ]; then
        log "No sound mapped for event: $event"
        return
    fi
    
    if [ "$event" = "changefloatingmode" ]; then
        local state="${data#*,}"
        if [ "$state" -eq 1 ]; then
            sound_file="float.wav"
        else
            sound_file="tile.wav"
        fi
    fi
    
    play_sound "$event" "$sound_file"
}

echo "Sound directory: $SOUND_DIR"
echo "Enabled events:"
for event in "${!EVENT_ENABLED[@]}"; do
    if [ "${EVENT_ENABLED[$event]}" -eq 1 ]; then
        echo "  - $event: ${SOUND_MAP[$event]}"
    fi
done

socat - "UNIX-CONNECT:$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock" | \
while read -r line; do
    handle_event "$line"
done
