#!/bin/bash

#CURRENT_WINDOW=$(tmux display-message -p '#I')
#WINDOW=$(tmux display-message -p '#W')
TARGET_NAME='spotify'
TARGET_COMMAND='spotify_player'
LIMIT=1000
VOLUME=80
SPOTIFY_PID=0
CHANGE_VOL=0

for ARGUMENT in "$@"; do
    KEY=$(echo "$ARGUMENT" | cut -f1 -d=)
    KEY_LENGTH=${#KEY}
    VALUE="${ARGUMENT:$KEY_LENGTH+1}"
    if [[ "$KEY" == "-v" ]]; then
        VOLUME=$VALUE
        CHANGE_VOL=1
        shift #remove the used switch
    fi
done

POSITION=$(tmux list-windows | awk -v name="$TARGET_NAME" '$0 ~ name {print $1}' | awk -F: '{print $1}')

check_deps() {
    if command -v "$TARGET_COMMAND" >/dev/null 2>&1; then
        if $(command -v tmux >/dev/null 2>&1); then
            echo 1
        else
            echo 0
        fi
    fi
}
display() {
    # echo "$1"
    tmux display-message "$1"
}
check_running() {
    local output
    output=$("$TARGET_COMMAND" get key playback)
    if [ "$output" = "null" ]; then
        echo 0
    else
        echo 1
        SPOTIFY_PID=$(pgrep spotify_player)
    fi
}

get_volume() {
    local volume
    volume=$("$TARGET_COMMAND" get key playback 2>/dev/null | jq -r '.device.volume_percent')
    if test -n "$volume"; then
        echo $volume
    else
        display "Spotify Not Running"
    fi
}

set_volume() {
    local volume
    volume=$(get_volume)
    if [[ "$volume" != "$VOLUME" ]]; then
        $TARGET_COMMAND playback volume $VOLUME
    fi
}

start_spotify() {
    display "Starting Spotify"
    tmux new-window -n $TARGET_NAME $TARGET_COMMAND &&
        tmux swap-window -s $TARGET_NAME -t 1
    display "Spotify Window Created"
    until [ "$(check_running)" = "1" ]; do
        display "Waiting for Spotify"
        sleep 1
    done
    display "Starting Liked Playlist"
    $TARGET_COMMAND playback start liked -r --limit=$LIMIT
    until [ "$(get_volume)" != "Spotify Not Running" ]; do
        display "Checking Initial Volume"
        sleep 1
    done
    display "Checking Volume"
    current_volume="$(get_volume)"
    if [ "$current_volume" != "$VOLUME" ]; then
        display $current_volume
        $($TARGET_COMMAND playback volume $VOLUME)
    fi
}

restart_spotify() {
    pid=$(pgrep $TARGET_COMMAND)
    if [[ -n "$pid" ]]; then
        until [[ -z $(pgrep $TARGET_COMMAND) ]]; do
            display "Spotify: Killing $pid"
            $(kill "$pid")
        done
        start_spotify
        until [[ $(check_running) = "1" ]]; do
            display "Spotify: Restarting"
            sleep 1
        done
        display "Spotify: Restarted"

    fi

}

check_deps >/dev/null
check_running >/dev/null

if [ "$#" -lt 1 ]; then
    if [ $SPOTIFY_PID != 0 ]; then
        if [[ "$CHANGE_VOL" == 1 ]]; then
            set_volume
            exit 0
        fi
        display "Spotify Already Running"
        exit 12
    fi
    if [ -n "$POSITION" ] && [ "$POSITION" -ne 1 ]; then
        tmux swap-window -s $TARGET_NAME -t 1
    elif [ -n "$POSITION" ] && [ "$POSITION" -eq 1 ]; then
        tmux select-window -t $TARGET_NAME
    else
        start_spotify
    fi
else
    if [ "$(check_running)" = "0" ]; then
        display "Spotify Is Not Running - Restarting"
        restart_spotify
    fi
    echo "$1"
    case "$1" in
    "n")
        $TARGET_COMMAND playback next && set_volume
        ;;
    "p")
        $TARGET_COMMAND playback previous && set_volume
        ;;
    "s")
        $TARGET_COMMAND playback play-pause && set_volume
        ;;
    "r")
        restart_spotify
        ;;

    *)
        echo "Usage: $0 [-n|-p|-s|-v]"
        exit 1
        ;;
    esac
fi
