#!/bin/bash

# Setup Global Keybindings for GNOME Desktop
# This script configures custom keybindings for dictation control

TOGGLE_SCRIPT="/mnt/storage/Development/dictation-service/scripts/toggle-dictation.sh"
KEYBINDING_NAME="Toggle Dictation"
DESIRED_BINDING="<Alt>d"

echo "Setting up dictation service keybindings..."

# --- Find or Create Custom Keybinding ---
KEYBASE="/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings"
FOUND_PATH=""
CURRENT_LIST_STR=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
CURRENT_LIST_ARRAY=()

# Parse CURRENT_LIST_STR into an array
# This handles both empty and non-empty lists from gsettings
if [[ "$CURRENT_LIST_STR" != "@as []" ]]; then
    # Remove leading "@as [" and trailing "]" and split by "', '"
    # Then add each path to the array
    TEMP_STR=$(echo "$CURRENT_LIST_STR" | sed -e "s/^@as \[//g" -e "s/\]$//g" -e "s/'//g")
    IFS=',' read -ra CURRENT_LIST_ARRAY <<< "$TEMP_STR"
fi

for path in "${CURRENT_LIST_ARRAY[@]}"; do
    path=$(echo "$path" | xargs) # Trim whitespace
    if [ -n "$path" ]; then
        name=$(gsettings get org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"$path"/ name 2>/dev/null)
        if [[ "$name" == "'$KEYBINDING_NAME'" ]]; then
            FOUND_PATH="$path"
            break
        fi
    fi
done

if [ -n "$FOUND_PATH" ]; then
    echo "Updating existing keybinding: $FOUND_PATH"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"$FOUND_PATH"/ command "'$TOGGLE_SCRIPT'"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"$FOUND_PATH"/ binding "'$DESIRED_BINDING'"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:"$FOUND_PATH"/ name "'$KEYBINDING_NAME'"
else
    # Create a new custom keybinding slot
    NEXT_NUM=0
    for path in "${CURRENT_LIST_ARRAY[@]}"; do
        path_num=$(echo "$path" | sed -n 's/.*custom\([0-9]\+\)$/\1/p')
        if [ -n "$path_num" ] && [ "$path_num" -ge "$NEXT_NUM" ]; then
            NEXT_NUM=$((path_num + 1))
        fi
    done
    
    NEW_KEY_ID="custom$NEXT_NUM"
    FULL_KEYPATH="$KEYBASE/$NEW_KEY_ID/"

    echo "Creating new keybinding at: $FULL_KEYPATH"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybindings:"$FULL_KEYPATH" name "'$KEYBINDING_NAME'"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybindings:"$FULL_KEYPATH" command "'$TOGGLE_SCRIPT'"
    gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybindings:"$FULL_KEYPATH" binding "'$DESIRED_BINDING'"
    
    # Add the new keybinding to the list if it's not already there
    if ! echo "$CURRENT_LIST_STR" | grep -q "$FULL_KEYPATH"; then
        if [[ "$CURRENT_LIST_STR" == "@as []" ]]; then
            NEW_LIST="['$FULL_KEYPATH']"
        else
            # Ensure proper comma separation
            NEW_LIST="${CURRENT_LIST_STR::-1}, '$FULL_KEYPATH']"
            NEW_LIST=$(echo "$NEW_LIST" | sed "s/@as //g") # Remove @as if present
        fi
        gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "$NEW_LIST"
    fi
fi

echo "Keybinding setup complete!"
echo "Press $DESIRED_BINDING to toggle dictation service"
echo ""
echo "Note: The keybinding will only function if the 'dictation.service' is running."
echo "To remove this specific keybinding (if it was created), you might need to manually check"
echo "your GNOME Keyboard Shortcuts settings or use dconf-editor to remove '$KEYBINDING_NAME'."