#!/bin/bash

# 函数：获取所有应用列表
# 函数：获取所有应用列表
get_apps_list() {
    local apps_list=()
    local app_dirs=($(find /opt/apps -mindepth 1 -maxdepth 1 -type d -exec test -f {}/files/run.sh \; -print))
    
    if [ ${#app_dirs[@]} -eq 0 ]; then
        zenity --error --text="请至少在应用商店安装一个wine应用后启动" --width 200
        exit 1
    fi
    
    for app_dir in "${app_dirs[@]}"; do
        local package_name=$(basename "$app_dir")
        local run_script="$app_dir/files/run.sh"
        local bottle_name="无法读取"
        local version="无法读取"
        local use_spark="否"
        
        if [ -f "$run_script" ]; then
            bottle_name=$(grep -oP 'BOTTLENAME="\K[^"]+' "$run_script")
            version=$(grep -oP 'APPVER="\K[^"]+' "$run_script")
            
            if [ -z "$version" ]; then
                version="无法读取"
            fi
            
            if grep -q "START_SHELL_PATH=\"/opt/deepinwine/tools/spark_run_v4.sh\"" "$run_script"; then
                use_spark="是"
            fi
        fi
        
        local app_name=$(get_app_name "$package_name")
        
        apps_list+=("$app_name" "$package_name" "$bottle_name" "$version" "$use_spark")
    done
    
    echo "${apps_list[@]}"
}

# 函数：获取应用名称
get_app_name() {
    local app_name_orig=$(grep -m 1 '^Name=' "/usr/share/applications/$1.desktop" | cut -d '=' -f 2)
    local app_name_i18n=$(grep -m 1 "^Name\[$LANGUAGE\]\=" "/usr/share/applications/$1.desktop" | cut -d '=' -f 2)
    local app_name=""
    
    if [ -z "$app_name_i18n" ]; then
        app_name="$app_name_orig"
    else
        app_name="$app_name_i18n"
    fi
    
    echo "$app_name"
}

# 函数：选择应用
select_app() {
    local apps_list=($(get_apps_list))
    local app=$(zenity --print-column=2 --width=800 --height=600 --list --title="选择应用" --text="选择要执行的应用" --column="应用名" --column="包名" --column="容器名" --column="版本号" --column="是否使用星火wine助手" "${apps_list[@]}")
    
    if [ -n "$app" ]; then
        local app_dir="/opt/apps/$app"
        local run_script="$app_dir/files/run.sh"
        local use_spark="否"
            if grep -q "START_SHELL_PATH=\"/opt/deepinwine/tools/spark_run_v4.sh\"" "$run_script"; then
                use_spark="是"
            fi
        if [ "$use_spark" == "是" ]; then
            select_spark_action "$app" "$(get_app_name $app)"
        else
            select_non_spark_action "$app" "$(get_app_name $app)"
        fi
    fi
}

select_non_spark_action(){
    local app="$1"
    local app_name="$2"
    local options=("在终端中打开")
    local choice=$(zenity --width=800 --height=600 --list --title="选择操作" --text="选择要对$app_name执行的操作" --column="操作" "${options[@]}")
        case "$choice" in
        "在终端中打开")
            local app_dir="/opt/apps/$app"
            local run_script="$app_dir/files/run.sh"
            x-terminal-emulator -e "$run_script"
            ;;
         esac
}

# 函数：选择星火wine助手操作
select_spark_action() {
    local app="$1"
    local app_name="$2"
    local options=("在终端中打开" "修改应用缩放" "修改全局缩放")
    local choice=$(zenity --width=800 --height=600 --list --title="选择操作" --text="选择要对$app_name执行的操作" --column="操作" "${options[@]}")
    
    case "$choice" in
        "在终端中打开")
            local app_dir="/opt/apps/$app"
            local run_script="$app_dir/files/run.sh"
            x-terminal-emulator -e "$run_script"
            ;;
        "修改应用缩放")
            select_scale_action "应用" "$app"
            ;;
        "修改全局缩放")
            select_scale_action "全局" "$app"
            ;;
        *)
            ;;
    esac
}

# 函数：选择缩放操作
select_scale_action() {
    local scale_type="$1"
    local app="$2"
    local app_dir="/opt/apps/$app"
    local run_script="$app_dir/files/run.sh"
    local bottle_name=$(grep -oP 'BOTTLENAME="\K[^"]+' "$run_script")
    local scale_factors=("1.0" "1.25" "1.5" "1.75" "2.0" "恢复默认")
    local choice=$(zenity --width=800 --height=600 --list --title="选择缩放比例" --text="选择要设置的缩放比例" --column="比例" "${scale_factors[@]}")
    
    if [ -n "$choice" ]; then
        local scale_factor="$choice"
        
        if [ "$scale_type" == "应用" ]; then
		if [ "$scale_factor" == "恢复默认" ];then
		rm $HOME/.deepinwine/$bottle_name/scale.txt
		else
            /opt/durapps/spark-dwine-helper/scale-set-helper/set-wine-scale.sh -s "$scale_factor" "$HOME/.deepinwine/$bottle_name"
            	fi
        elif [ "$scale_type" == "全局" ]; then
            if [ "$scale_factor" == "恢复默认" ];then
		rm $HOME/.config/spark-wine/scale.txt
		else
            echo "$scale_factor" > "$HOME/.config/spark-wine/scale.txt"
            find "$HOME/.deepinwine/" -name "scale.txt" -type f -print -exec rm -rf {} \;
            fi
        fi
    fi
    zenity --info --width=300 --text="操作已完成，请重启Wine应用查看"
}

select_app

