#!/bin/bash
set -euo pipefail

# MPRIS2 服务名
SERVICE="org.mpris.MediaPlayer2.KylinMusic"
# MPRIS2 对象路径
OBJECT_PATH="/org/mpris/MediaPlayer2"
# MPRIS2 接口
INTERFACE="org.mpris.MediaPlayer2.Player"

# 检查 dbus-send 是否可用
if ! command -v dbus-send &> /dev/null; then
    echo "错误：dbus-send 命令不可用"
    exit 1
fi

# 显示帮助信息
show_help() {
    echo "使用方法: $0 <命令> [参数]"
    echo ""
    echo "命令列表:"
    echo "  open <文件路径> - 打开并播放音频文件"
    echo "  play          - 开始播放"
    echo "  pause         - 暂停播放"
    echo "  toggle        - 切换播放/暂停状态"
    echo "  next          - 下一首"
    echo "  previous      - 上一首"
    echo "  stop          - 停止播放"
    echo "  volume <值>   - 设置音量 (0.0-1.0)"
    echo "  volume-up     - 音量增加 5%"
    echo "  volume-down   - 音量减少 5%"
    echo "  seek <秒数>   - 快进/快退 (正数快进，负数快退)"
    echo "  get-volume    - 获取当前音量"
    echo "  get-position  - 获取当前播放位置（秒）"
    echo "  get-status    - 获取当前播放状态"
    echo ""
    echo "示例:"
    echo "  $0 open ~/Music/song.mp3    # 打开并播放音频文件"
    echo "  $0 play                # 开始播放"
    echo "  $0 volume 0.7          # 设置音量为 70%"
    echo "  $0 seek 10             # 快进 10 秒"
    echo "  $0 volume-up           # 音量增加 5%"
}

# 执行 MPRIS2 命令
execute_mpris_command() {
    method="$1"
    params="${2:-}"

    dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" "${INTERFACE}.${method}" $params
}

# 获取当前音量
get_volume() {
    result=$(dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Properties.Get string:"$INTERFACE" string:"Volume")
    echo "$result" | grep -oP 'double "\K[0-9.]+'
}

# 主逻辑
case "${1:-}" in
    open)
        if [ $# -lt 2 ]; then
            echo "错误：请指定文件路径"
            exit 1
        fi
        file_path="$2"
        if [ ! -f "$file_path" ]; then
            echo "错误：文件不存在: $file_path"
            exit 1
        fi
        echo "正在打开文件: $file_path"
        DISPLAY=:0 QT_QPA_PLATFORMTHEME=ukui XDG_CURRENT_DESKTOP=UKUI nohup kylin-music "$file_path" > /dev/null 2>&1 &
        echo "文件已在后台打开播放"
        ;;
    play)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        execute_mpris_command "Play"
        echo "开始播放"
        ;;
    pause)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        execute_mpris_command "Pause"
        echo "暂停播放"
        ;;
    toggle)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        execute_mpris_command "PlayPause"
        echo "切换播放/暂停状态"
        ;;
    next)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        execute_mpris_command "Next"
        echo "下一首"
        ;;
    previous)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        execute_mpris_command "Previous"
        echo "上一首"
        ;;
    stop)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        execute_mpris_command "Stop"
        echo "停止播放"
        ;;
    volume)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        if [ $# -lt 2 ]; then
            echo "错误：请指定音量值 (0.0-1.0)"
            exit 1
        fi
        volume="$2"
        if (( $(echo "$volume < 0.0 || $volume > 1.0" | bc -l) )); then
            echo "错误：音量值必须在 0.0-1.0 之间"
            exit 1
        fi
        dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Properties.Set string:"$INTERFACE" string:"Volume" variant:double:"$volume"
        echo "音量已设置为 $(echo "$volume * 100" | bc -l | cut -d. -f1)%"
        ;;
    volume-up)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        current_volume=$(get_volume)
        new_volume=$(echo "$current_volume + 0.05" | bc -l)
        if (( $(echo "$new_volume > 1.0" | bc -l) )); then
            new_volume="1.0"
        fi
        dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Properties.Set string:"$INTERFACE" string:"Volume" variant:double:"$new_volume"
        echo "音量已增加到 $(echo "$new_volume * 100" | bc -l | cut -d. -f1)%"
        ;;
    volume-down)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        current_volume=$(get_volume)
        new_volume=$(echo "$current_volume - 0.05" | bc -l)
        if (( $(echo "$new_volume < 0.0" | bc -l) )); then
            new_volume="0.0"
        fi
        dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Properties.Set string:"$INTERFACE" string:"Volume" variant:double:"$new_volume"
        echo "音量已减少到 $(echo "$new_volume * 100" | bc -l | cut -d. -f1)%"
        ;;
    seek)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        if [ $# -lt 2 ]; then
            echo "错误：请指定快进/快退秒数"
            exit 1
        fi
        seconds="$2"
        execute_mpris_command "Seek" int64:"$((seconds * 1000000))"
        echo "已${seconds#-}秒"
        ;;
    get-volume)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        volume=$(get_volume)
        echo "当前音量: $(echo "$volume * 100" | bc -l | cut -d. -f1)%"
        ;;
    get-position)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        result=$(dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Properties.Get string:"$INTERFACE" string:"Position")
        position=$(echo "$result" | grep -oP 'int64 "\K[0-9]+')
        seconds=$((position / 1000000))
        echo "当前播放位置: ${seconds}秒"
        ;;
    get-status)
        # 检查 kylin-music 是否在运行
        if ! dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Introspectable.Introspect > /dev/null 2>&1; then
            echo "错误：kylin-music 未运行"
            exit 1
        fi
        result=$(dbus-send --print-reply --dest="$SERVICE" "$OBJECT_PATH" org.freedesktop.DBus.Properties.Get string:"$INTERFACE" string:"PlaybackStatus")
        status=$(echo "$result" | grep -oP 'string "\K[^"]+')
        echo "当前状态: $status"
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        echo "错误：未知命令"
        show_help
        exit 1
        ;;
esac
