#!/usr/bin/env bash

wineqgui-template()
{
    local prefixdir
    local template
    local name
    local safe_name
    local prefix
    local winver
    local arch
    local use_gamemode

    prefixdir="${HOME}/.local/share/wineqgui/prefixes"

    mkdir -p "$prefixdir"


    # -------------------------------------------------
    # Vel template
    # -------------------------------------------------

    template=$(
        qarma --list \
            --radiolist \
            --hide-header \
            --title="Create Wine environment" \
            --width=520 \
            --height=360 \
            --column="" \
            --column="Template" \
            TRUE  "Standard Wine" \
            FALSE "Gaming" \
            FALSE "Windows 10" \
            FALSE "Windows 11" \
            FALSE "32-bit" \
            FALSE "Custom" \
            FALSE "Cancel" \
            --text="<b>Choose a template for the new Wine environment.</b>"
    )

    [[ -z "$template" ]] && return

    [[ "$template" == "Cancel" ]] && return


    # -------------------------------------------------
    # Namn
    # -------------------------------------------------

    name=$(
        qarma --entry \
            --title="Environment name" \
            --text="Name for the new Wine environment:" \
            --entry-text="New application"
    )

    [[ -z "$name" ]] && return


    safe_name=$(
        printf '%s' "$name" |
        tr '[:upper:]' '[:lower:]' |
        tr ' ' '-' |
        tr -cd 'a-z0-9._-'
    )

    [[ -z "$safe_name" ]] && safe_name="wine-environment"

    prefix="${prefixdir}/${safe_name}"


    # -------------------------------------------------
    # Finn innstillingar frå template
    # -------------------------------------------------

    arch="win64"
    winver="win10"
    use_gamemode="0"

    case "$template" in

        "Standard Wine")
            arch="win64"
            winver="win10"
            ;;

        "Gaming")
            arch="win64"
            winver="win10"
            use_gamemode="1"
            ;;

        "Windows 10")
            arch="win64"
            winver="win10"
            ;;

        "Windows 11")
            arch="win64"
            winver="win11"
            ;;

        "32-bit")
            arch="win32"
            winver="win10"
            ;;

        "Custom")
            wineqgui-template-custom
            return
            ;;
    esac


    # -------------------------------------------------
    # Sjekk om miljøet finst
    # -------------------------------------------------

    if [[ -e "$prefix" ]]; then
        show_error "
An environment with this name already exists:

<b>${prefix}</b>
"
        return 1
    fi


    # -------------------------------------------------
    # Stadfest
    # -------------------------------------------------

    if ! qarma --question \
        --title="Create Wine environment" \
        --text="
Create a new Wine environment?

<b>Name:</b> ${name}
<b>Template:</b> ${template}
<b>Architecture:</b> ${arch}
<b>Windows version:</b> ${winver}
"
    then
        return
    fi


    # -------------------------------------------------
    # Opprett prefix
    # -------------------------------------------------

    mkdir -p "$prefix"

    (
        echo "10"
        echo "# Creating Wine environment..."

        WINEPREFIX="$prefix" \
        WINEARCH="$arch" \
        wineboot -u >/dev/null 2>&1

        if [[ ! -f "${prefix}/system.reg" ]]; then
            exit 1
        fi

        echo "70"
        echo "# Applying Windows version..."

        WINEPREFIX="$prefix" \
        winecfg -v "$winver" >/dev/null 2>&1
        actual_winver=$(
    WINEPREFIX="$prefix" \
    wine reg query 'HKCU\Software\Wine' /v Version 2>/dev/null |
    awk '/Version/ {print $NF; exit}'
)

if [[ "$actual_winver" != "$winver" ]]; then
    exit 1
fi

        echo "100"
        echo "# Environment created."

    ) |
    qarma --progress \
        --title="Creating ${name}" \
        --text="Preparing Wine environment..." \
        --percentage=0 \
        --auto-close

    status=${PIPESTATUS[0]}

    if [[ "$status" -ne 0 ]]; then

        rm -rf -- "$prefix"

        show_error "
Wine could not create the environment.
"

        return 1
    fi


    show_info "
Wine environment created successfully.

<b>${name}</b>

Location:

${prefix}
"
}
