#!/bin/bash

# ===== ACE环境配置 =====
declare -a ace_commands_order=(
    "bookworm-run:amber-ce-bookworm"
    "trixie-run:amber-ce-trixie"
    "deepin23-run:amber-ce-deepin23"
)

# ===== 日志和函数 =====
[ -f /opt/durapps/spark-store/bin/bashimport/log.amber ] && \
    source /opt/durapps/spark-store/bin/bashimport/log.amber || {
    log.info() { echo "INFO: $*"; }
    log.warn() { echo "WARN: $*"; }
    log.error() { echo "ERROR: $*"; }
    log.debug() { echo "DEBUG: $*"; }
}

# ===== 功能函数 =====
function scan_desktop_file_log() {
    unset desktop_file_path
    local package_name=$1
    # 标准desktop文件检测
    while IFS= read -r path; do
        [ -z "$(grep 'NoDisplay=true' "$path")" ] && {
            log.info "Found valid desktop file: $path"
            desktop_file_path="$path"
            return 0
        }
    done < <(dpkg -L "$package_name" 2>/dev/null | grep -E '/usr/share/applications/.*\.desktop$|/opt/apps/.*/entries/applications/.*\.desktop$')

    # 深度环境特殊处理
    while IFS= read -r path; do
        [ -z "$(grep 'NoDisplay=true' "$path")" ] && {
            log.info "Found deepin desktop file: $path"
            desktop_file_path="$path"
            return 0
        }
    done < <(find /opt/apps/$package_name -path '*/entries/applications/*.desktop' 2>/dev/null)
    return 1
}

function scan_desktop_file() {
    local package_name=$1 result=""
    # 标准结果收集
    while IFS= read -r path; do
        [ -z "$(grep 'NoDisplay=true' "$path")" ] && result+="$path,"
    done < <(dpkg -L "$package_name" 2>/dev/null | grep -E '/usr/share/applications/.*\.desktop$|/opt/apps/.*/entries/applications/.*\.desktop$')
    
    # 深度环境补充扫描
    while IFS= read -r path; do
        [ -z "$(grep 'NoDisplay=true' "$path")" ] && result+="$path,"
    done < <(find /opt/apps/$package_name -path '*/entries/applications/*.desktop' 2>/dev/null)
    
    echo "${result%,}"
}

function launch_app() {
    local DESKTOP_FILE_PATH="${1#file://}"
    # 提取并净化Exec命令
    exec_command=$(grep -m1 '^Exec=' "$DESKTOP_FILE_PATH" | cut -d= -f2- | sed 's/%.//g')
    [ -z "$exec_command" ] && return 1
    log.info "Launching: $exec_command"
    # 图形环境启动优化
    if [ -n "$DISPLAY" ]; then
        nohup env DISPLAY=$DISPLAY XAUTHORITY=${XAUTHORITY:-~/.Xauthority} ${SHELL:-bash} -c "$exec_command" >/dev/null 2>&1 &
    else
        nohup ${SHELL:-bash} -c "$exec_command" >/dev/null 2>&1 &
    fi
}

# ===== ACE环境执行器 =====
function ace_runner() {
    local command_type=$1 package_name=$2
    for ace_entry in "${ace_commands_order[@]}"; do
        local ace_cmd=${ace_entry%%:*}
        command -v "$ace_cmd" >/dev/null || continue
        
        log.info "Checking in $ace_cmd environment..."
        if output=$($ace_cmd "$0" "$command_type" "$package_name" 2>/dev/null); then
            [ "$command_type" = "list" ] && echo "$output"
            exit 0
        fi
    done
    return 1
}

# ===== 主逻辑 =====
[ $# -lt 2 ] && {
    log.error "Usage: $0 {check|launch|list|start} package_name/desktop_file"
    exit 1
}

case $1 in
check)
    # 当前环境检查
    if scan_desktop_file_log "$2"; then
        exit 0
    else
        # 非ACE环境下执行ACE环境扫描
        [ -z "$IS_ACE_ENV" ] && ace_runner check "$2"
        exit 1
    fi
    ;;

list)
    # 当前环境列表
    if result=$(scan_desktop_file "$2"); then
        echo "$result"
        exit 0
    else
        # 非ACE环境下执行ACE环境扫描
        [ -z "$IS_ACE_ENV" ] && ace_runner list "$2"
        exit 1
    fi
    ;;

launch|start)
    # 当前环境启动
    if scan_desktop_file_log "$2" && launch_app "$desktop_file_path"; then
        exit 0
    else
        # 非ACE环境下通过ACE环境启动
        [ -z "$IS_ACE_ENV" ] && ace_runner launch "$2"
        exit 1
    fi
    ;;
*)
    log.error "Invalid command: $1"
    exit 2
    ;;
esac
