# shellcheck shell=dash disable=SC2120,SC2034,SC1090
___x_cmd_theme_ls(){
    local X_help_cmd=;  X_help_cmd='x help -m theme ls' help:arg:parse
    [ $# -gt 0 ] || set -- info
    local op="$1";  shift
    case "$op" in
        -a|--all|all)       ___x_cmd_theme_ls___all "$@"    ;;
        -i|--info|info)     ___x_cmd_theme_ls___info        ;;
        *)                  ___x_cmd_theme_ls___info        ;;
    esac
}


___x_cmd_theme_ls___info_printf(){
    local typecode=
    local typeend="${___X_CMD_UNSEENCHAR_033}[0m"
    case "$1" in
        --yellow)   typecode="${___X_CMD_UNSEENCHAR_033}[33;1m"; shift;;
        --green)    typecode="${___X_CMD_UNSEENCHAR_033}[32;1m"; shift;;
    esac

    ___x_cmd_is_stdout2tty || {
        typecode=; typeend=
    }

    local terminal="$1"
    local varname="$2"
    local envshell="${___X_CMD_REAL_CALLER_SHELL:-posix}"
    local vendor=; local name=
    if ___x_cmd_theme_boot configfile_get_ "$envshell" "$terminal"; then
        ___x_cmd_theme_boot current_style_ "$vendor" "$name"
        printf "${typecode}%10s:${typeend} %s\t->\t%s\n" "$terminal" "$vendor" "$name"
    elif [ -n "$varname" ]; then
        printf "${typecode}%10s:${typeend} %s\t->\t%s\n" "$terminal" theme "$varname"
    else
        printf "${typecode}%10s:${typeend}\n" "$terminal"
        return 1
    fi
}

___x_cmd_theme_ls___info(){(
    local rcfile="${___X_CMD_ROOT_CFG}/theme/default" # temp
    if [ -f "$rcfile" ]; then
        local ___X_CMD_THEME_DISABLE=1
        . "$rcfile"
    fi

    local IFS=" "
    local default_activated=
    local fallback_activated=
    local varname=

    local typecode="${___X_CMD_UNSEENCHAR_033}[7;4m"
    local typeend="${___X_CMD_UNSEENCHAR_033}[0m"
    ___x_cmd_is_stdout2tty || {
        typecode=; typeend=
    }

    printf "${typecode}%10s:${typeend} ${typecode}%s${typeend}\t  \t${typecode}%s${typeend}\n" Terminal Vendor Name
    for terminal in vscode gnome iterm apple kde ide windows konsole; do
        varname=
        eval varname=\"\$___X_CMD_THEME_NAME_"${terminal}"\" 2>/dev/null
        if [ "$___X_CMD_THEME_TERM_VENDOR" = "$terminal" ]; then
            ___x_cmd_theme_ls___info_printf --yellow "$terminal" "$varname" || default_activated=1
        else
            ___x_cmd_theme_ls___info_printf "$terminal" "$varname" || true
        fi
    done

    if [ -z "$default_activated" ]; then
        ___x_cmd_theme_ls___info_printf default || true
    else
        ___x_cmd_theme_ls___info_printf --yellow default || true
    fi

    ___x_cmd_theme_ls___info_printf --green fallback || true
)}

___x_cmd_theme_ls___all(){
    case "$1" in
        --no-cache)     ___x_cmd_theme_update || return ;;
    esac

    ___x_cmd_theme_ls___all_pipe
}

___x_cmd_theme_ls___all_pipe(){
    ___x_cmd_theme_ls___all_pipe_main || return
    printf "%s\n" "random"
}

___x_cmd_theme_ls___all_pipe_main(){
    local x_file=; ___x_cmd_theme_which___getfile_ classify.yml || return
    < "$x_file" ___x_cmd_theme_ls___parse_classify
}

___x_cmd_theme_ls___parse_classify(){
    ___x_cmd_cmds_awk '
    ($0 ~ "^  [a-zA-z]") { gsub("(^[ ]*)|(:$)", "", $0); theme = $0; next; }
    ($0 ~ "^    -"){ scheme = $2; print theme "/" scheme; }
    '
}

___x_cmd_theme_update(){
    local X_help_cmd=;  X_help_cmd='x help -m theme update' help:arg:parse
    ___x_cmd_theme___pkg_unzip style --update "dist/$___X_CMD_THEME_PKG_TAR_NAME" "${___X_CMD_THEME_PKG_PATH}/${___X_CMD_THEME_PKG_TAR_NAME}" "$___X_CMD_THEME_DATA_PATH"
}

___x_cmd_theme___get_valid_name_(){
    x_="${1}"
    [ -n "$x_" ] || {
        theme:debug "Default theme set to [name=l]"
        x_=l
    }
    if [ -n "$2" ] && [ "$x_" = "random" ]; then
        x_="random,$2"
    fi
}
