# shellcheck shell=dash disable=2016,2034

___X_CMD_OHMYPOSH_CONFIG_DIR="$___X_CMD_ROOT_DATA/ohmyposh/config"
___X_CMD_OHMYPOSH_PREVIEW_DIR="$___X_CMD_ROOT_DATA/ohmyposh/preview"
___x_cmd_ohmyposh_ls(){
    ___x_cmd_ohmyposh___check_config || return $?
    local i
    for i in "$___X_CMD_OHMYPOSH_CONFIG_DIR"/*; do
        i="${i%".omp.json"}"
        i="${i%".omp.yaml"}"
        printf "%s\n" "${i#"$___X_CMD_OHMYPOSH_CONFIG_DIR/"}"
    done
}

# Section: fz
___x_cmd_ohmyposh_fz(){
    if ! { ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_chatty; }; then
        ___x_cmd_ohmyposh_ls
    else
        local x_=; ___x_cmd_ohmyposh_fz_ || return $?
        local id=;
        ___x_cmd ui select id "Next for ohmyposh theme -> $x_"  \
            "x ohmyposh use $x_     # Apply globally"           \
            "x ohmyposh try $x_     # Apply only in current shell env" \
            "Return"

        case "$id" in
            1)
                ohmyposh:info "cmd -> x ohmyposh use $x_"
                ___x_cmd ohmyposh use "$x_"
                ;;
            2)
                ohmyposh:info "cmd -> x ohmyposh try $x_"
                ___x_cmd ohmyposh try "$x_"
                ;;
            *)  return 0
                ;;
        esac
    fi
}

___x_cmd_ohmyposh_fz_data(){
    ___x_cmd fsiter "$___X_CMD_OHMYPOSH_PREVIEW_DIR" | ___x_cmd fzf --ansi --reverse --cycle --exit-0 \
        --height 50% --preview-window right:70% \
        --preview='___x_cmdexe ohmyposh --fzfinfo {1}'
}

___x_cmd_ohmyposh_fz_(){
    x_=""
    ___x_cmd_ohmyposh_fz___check_preview || return $?
    x_="$(
        ___X_CMD_RUNMODE="$___X_CMD_RUNMODE" \
        ___x_cmdexe ohmyposh --fzdata
    )"
    [ -n "$x_" ]
}

___x_cmd_ohmyposh_fz_info(){
    local name="$1";
    [ -n "$name" ]  || N=ohmyposh M="Please provide ohmyposh theme name" log:ret:64
    ___x_cmd_ohmyposh_fz___check_preview || return $?
    local fp; fp="$___X_CMD_OHMYPOSH_PREVIEW_DIR/$name"
    [ -f "$fp" ]    || N=ohmyposh M="Not found ohmyposh theme [name=$name] preview file" log:ret:1
    ___x_cmd_cmds cat "$fp"
}

___x_cmd_ohmyposh_fz___check_preview(){
    local dir="$___X_CMD_OHMYPOSH_PREVIEW_DIR"
    [ "$X_OHMYPOSH_UPDATE_CONFIG" = 1 ] || [ ! -d "$dir" ] || return 0

    local url; local tarname="ohmyposh.preview.tar.xz"
    if [ -n "$___X_CMD_THEME_DEV_MOD" ]; then
        ohmyposh:warn "Download form github x-cmd-theme/release, ___X_CMD_THEME_DEV_MOD=$___X_CMD_THEME_DEV_MOD"
        url="https://raw.githubusercontent.com/x-cmd-theme/release/main/dist/${tarname}"
    elif ! ___x_cmd websrc is cn; then
        url="https://raw.githubusercontent.com/x-cmd/theme/main/dist/${tarname}"
    else
        url="https://oss.resource.x-cmd.com/x-cmd/theme/dist/${tarname}"
    fi

    local cache="$___X_CMD_ROOT_DATA/ohmyposh/$tarname"
    ___x_cmd ensurefp "$cache"
    ohmyposh:info --url "$url" --unpack_dir "$dir" "Download $tarname"
    ___x_cmd curl -L -sS --speed-time 5 --speed-limit 100 "$url" > "${cache}.tmp" || {
        ohmyposh:error "Fail to download ${cache}"
        return 1
    }

    ___x_cmd mv -f "${cache}.tmp" "${cache}" || return $?
    ___x_cmd rmrf "$dir" || return $?
    ___X_CMD_ZUZ_QUIET=1 ___x_cmd uz "$cache" "$dir" || {
        ohmyposh:error "Fail to extract $cache"
        ___x_cmd rmrf "$cache" "$dir"
        return 1
    }
}

# EndSection
___x_cmd_ohmyposh___check_valid_name(){
    local name="$1"
    ___x_cmd_ohmyposh___check_config || return $?
    local fp="$___X_CMD_OHMYPOSH_CONFIG_DIR/${name}.omp.json"
    [ -f "$fp" ] || fp="$___X_CMD_OHMYPOSH_CONFIG_DIR/${name}.omp.yaml"
    [ -f "$fp" ]
}

___x_cmd_ohmyposh___get_valid_name_(){
    local name="$1"
    if ___x_cmd_ohmyposh___check_valid_name "$name"; then
        x_="$name"
    elif [ -f "$name" ]; then
        ___x_cmd abspath_ "$name" || return $?
        ohmyposh:debug "Custom config file -> $x_"
    else
        ohmyposh:error "Invaild value for prompt theme [name=$name]"
        return 1
    fi
}

___x_cmd_ohmyposh_which_(){
    local name="$1"
    ___x_cmd_ohmyposh___check_config || return $?
    local fp="$___X_CMD_OHMYPOSH_CONFIG_DIR/${name}.omp.json"
    [ -f "$fp" ] || fp="$___X_CMD_OHMYPOSH_CONFIG_DIR/${name}.omp.yaml"

    if [ -f "$fp" ]; then
        x_="$fp"
    elif [ -f "$name" ]; then
        ___x_cmd abspath_ "$name" || return $?
        ohmyposh:debug "Custom config file -> $x_"
    else
        ohmyposh:error "Invaild value for prompt theme [name=$name]"
        return 1
    fi
}

___x_cmd_ohmyposh_which(){
    local x_=; ___x_cmd_ohmyposh_which_ "$@" || return $?
    printf "%s\n" "$x_"
}

___x_cmd_ohmyposh___check_config(){
    [ "$X_OHMYPOSH_UPDATE_CONFIG" = 1 ] || [ ! -d "$___X_CMD_OHMYPOSH_CONFIG_DIR" ] || return 0
    local url=
    if ___x_cmd websrc is cn; then
        url="https://codeberg.org/x-cmd-sourcecode/oh-my-posh/releases/download/latest/themes.zip"
    else
        url='https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip'
    fi

    local tmpdir="$___X_CMD_ROOT_TMP/ohmyposh"
    ___x_cmd mkdirp "$tmpdir"
    ohmyposh:info --url "$url" --unpack_dir "$___X_CMD_OHMYPOSH_CONFIG_DIR" "Download themes.zip"
    ___x_cmd curl -L -sS --speed-time 5 --speed-limit 100 "$url" > "$tmpdir/themes.zip" || {
        ohmyposh:error "Download themes.zip failed"
        ___x_cmd rmrf "$tmpdir"
        return 1
    }

    ___X_CMD_ZUZ_QUIET=1 ___x_cmd uz "$tmpdir/themes.zip" "$___X_CMD_OHMYPOSH_CONFIG_DIR" || return $?
    ___x_cmd rmrf "$tmpdir"
}

___x_cmd_ohmyposh_update(){
    local X_OHMYPOSH_UPDATE_CONFIG=1
    ___x_cmd_ohmyposh___check_config || return $?
    ___x_cmd_ohmyposh_fz___check_preview
}

