# shellcheck shell=dash

# 0 - kde
# 1 - mate
# 2 - xfce
# 3 - i3
# 4 - icewm
# 5 - openbox

# 0 - ubuntu
# 1 - debian
# 2 - alpine
# 3 - arch
# 4 - fedora

# 40000
# 41000
# 42000
# 43000
# 44000
# 45000

___x_cmd_webtop_container_portassign(){
    [ $# -gt 0 ] || set -- get

    local op="$1";  shift
    case "$op" in
        get|get_|release_|desktop_)
            ___x_cmd_webtop_container_portassign_"$op"      "$@" ;;
        findport|findport_)
            ___x_cmd_webtop_container___"$op"               "$@" ;;
        *)
            N=webtop M="Unknwon op -> $op" log:ret:64            ;;
    esac
}

___x_cmd_webtop_container_portassign_get_(){
    x_="$(___x_cmd_webtop_container_portassign_get "$@")"
}

___x_cmd_webtop_container_portassign_get(){
    # local release="$1"
    # local desktop="$2"
    local tag="$1"
    local release="${tag%%-*}"
    local desktop="${tag#"$release"-}"
    desktop="${desktop%%-*}"

    local idx="$2"  # TODO: first check this port ...

    local port=4

    local x_=""
    ___x_cmd_webtop_container_portassign_release_ "$release" || return $?
    port="${port}${x_}"
    ___x_cmd_webtop_container_portassign_desktop_ "$desktop" || return $?
    port="${port}${x_}"

    port="${port}0"
    # check if container exists.

    [ -z "$idx" ] || {
        x_="$((port + idx))0"
        printf "%s\n" "$x_"
        return 0
    }

    x_=$port

    (
        ___x_cmd proxy unset 2>/dev/null
        local inc=0
        while [ $inc -le 9 ]; do
            x_="$(( port + inc ))0"  # 0 is kasm-http, 1 kasm-https, 2 ssh, 8 http

            # Detect if there is a container with this name
            if ! ___x_cmd_webtop container id "x-webtop-${x_}" 1>/dev/null 2>/dev/null; then
                # Detect the port is available
                ___x_cmd curl --max-time 0.01 --head "http://localhost:$x_" 1>/dev/null 2>/dev/null || {
                    [ $? -ne 7 ] || {
                        printf "%s\n" "$x_"
                        return 0
                    }
                }
            fi

            inc=$((inc + 1))
        done
        return 1
    )
}

___x_cmd_webtop_container_portassign_release_(){
    case "$1" in
        ubuntu)     x_=0 ;;
        debian)     x_=1 ;;
        alpine)     x_=2 ;;
        arch)       x_=3 ;;
        fedora)     x_=4 ;;
        *)          return 1
    esac
}

___x_cmd_webtop_container_portassign_desktop_(){
    case "$1" in
        kde)        x_=0 ;;
        mate)       x_=1 ;;
        xfce)       x_=2 ;;
        i3)         x_=3 ;;
        icewm)      x_=4 ;;
        openbox)    x_=5 ;;
        *)          return 1
    esac
}

___x_cmd_webtop_container___findport_(){
    x_="$(___x_cmd_webtop_container___findport "$@")"
}

___x_cmd_webtop_container___findport(){(
    ___x_cmd proxy unset 2>/dev/null
    local x_=3000

    while [ "$x_" -le 3199 ]; do
        ___x_cmd curl --max-time 0.01 --head "http://localhost:$x_" 1>/dev/null 2>/dev/null
        [ $? -ne 7 ] || {
            printf "%s\n" "$x_"
            return 0
        }
        x_=$((x_ + 1))
    done
    return 1
)}

