#!/usr/bin/env bash

wineqgui-settings()
{
    local configdir
    local selection
    local file
    local name
    local exe
    local prefix
    local wine_cmd
    local args
    local gamemode
    local setting
    local new_value
    local entries=()

    configdir="${HOME}/.local/share/wineqgui/applications"

    mkdir -p "$configdir"


    # -------------------------------------------------
    # Finn registrerte applikasjonar
    # -------------------------------------------------

    while IFS= read -r -d '' file; do

        name=$(read_config_value "$file" "NAME")

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

        entries+=("$name" "$file")

    done < <(
        find "$configdir" \
            -maxdepth 1 \
            -type f \
            -name '*.conf' \
            -print0
    )


    # -------------------------------------------------
    # Ingen registrerte program
    # -------------------------------------------------

    if (( ${#entries[@]} == 0 )); then
        show_info "There are no WineQGUI applications to configure."
        return
    fi


    # -------------------------------------------------
    # Vel program
    # -------------------------------------------------

    selection=$(
        qarma --list \
            --title="Application settings- choose an application" \
            --width=600 \
            --height=400 \
            --hide-header \
            --column="Application" \
            --column="Config" \
            "${entries[@]}"
    )

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


    # -------------------------------------------------
    # Les config
    # -------------------------------------------------

    name=$(read_config_value "$selection" "NAME")
    exe=$(read_config_value "$selection" "EXE")
    prefix=$(read_config_value "$selection" "PREFIX")
    wine_cmd=$(read_config_value "$selection" "WINE")
    args=$(read_config_value "$selection" "ARGS")
    gamemode=$(read_config_value "$selection" "GAMEMODE")


    # -------------------------------------------------
    # Settings-meny
    # -------------------------------------------------

    while true; do

        setting=$(
            qarma --list \
                --radiolist \
                --hide-header \
                --title="Settings - ${name}" \
                --width=560 \
                --height=420 \
                --column="" \
                --column="Setting" \
                TRUE  "Application name" \
                FALSE "Wine command" \
                FALSE "Launch arguments" \
                FALSE "Gamemode" \
                FALSE "Show current configuration" \
                FALSE "Back" \
                --text="<b>${name}</b>"
        )

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


        case "$setting" in

            "Application name")

                new_value=$(
                    qarma --entry \
                        --title="Application name" \
                        --text="Name:" \
                        --entry-text="$name"
                )

                [[ -z "$new_value" ]] && continue

                name="$new_value"
                write_config_value "$selection" "NAME" "$name"
                ;;


            "Wine command")

                new_value=$(
                    qarma --entry \
                        --title="Wine command" \
                        --text="Wine command or executable:" \
                        --entry-text="$wine_cmd"
                )

                [[ -z "$new_value" ]] && continue

                wine_cmd="$new_value"
                write_config_value "$selection" "WINE" "$wine_cmd"
                ;;


            "Launch arguments")

                new_value=$(
                    qarma --entry \
                        --title="Launch arguments" \
                        --text="Optional launch arguments:" \
                        --entry-text="$args"
                )

                args="$new_value"
                write_config_value "$selection" "ARGS" "$args"
                ;;


            "Gamemode")

                if [[ "$gamemode" == "1" ]]; then
                    new_value=$(
                        qarma --list \
                            --radiolist \
                            --hide-header \
                            --title="Gamemode" \
                            --width=420 \
                            --height=250 \
                            --column="" \
                            --column="Option" \
                            TRUE  "Enabled" \
                            FALSE "Disabled"
                    )
                else
                    new_value=$(
                        qarma --list \
                            --radiolist \
                            --hide-header \
                            --title="Gamemode" \
                            --width=420 \
                            --height=250 \
                            --column="" \
                            --column="Option" \
                            TRUE  "Disabled" \
                            FALSE "Enabled"
                    )
                fi

                case "$new_value" in
                    "Enabled")
                        gamemode="1"
                        ;;
                    "Disabled")
                        gamemode="0"
                        ;;
                    *)
                        continue
                        ;;
                esac

                write_config_value "$selection" "GAMEMODE" "$gamemode"
                ;;


            "Show current configuration")

                show_info \
                    "<b>${name}</b>\n\n"\
                    "<b>Executable:</b>\n${exe}\n\n"\
                    "<b>Wine environment:</b>\n${prefix}\n\n"\
                    "<b>Wine command:</b>\n${wine_cmd}\n\n"\
                    "<b>Arguments:</b>\n${args:-None}\n\n"\
                    "<b>Gamemode:</b> $(
                        [[ "$gamemode" == "1" ]] &&
                        printf 'Enabled' ||
                        printf 'Disabled'
                    )"
                ;;


            "Back")
                return
                ;;
        esac
    done
}
