# shellcheck shell=dash

___X_CMD_STARSHIP_PREVIEW_DIR="$___X_CMD_ROOT_DATA/starship/preview"
___X_CMD_STARSHIP_CONFIG_DIR="$___X_CMD_ROOT_DATA/starship/config"
___x_cmd_starship_ls(){
    ___x_cmd_starship_run preset --list
}


# using fz to search and find the theme
# Section: fz
___x_cmd_starship_fz(){
    if ! { ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_chatty; }; then
        ___x_cmd_starship_ls
    else
        local x_=; ___x_cmd_starship_fz_ || return $?
        local id=;
        ___x_cmd ui select id "Next for starship theme -> $x_"  \
            "x starship use $x_     # Apply globally"           \
            "x starship try $x_     # Apply only in current shell env" \
            "Return"

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

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

___x_cmd_starship_fz_(){
    x_=""
    ___x_cmd_starship_fz___check_preview || return $?
    x_="$(
        ___X_CMD_RUNMODE="$___X_CMD_RUNMODE" \
        ___x_cmdexe starship --fzdata
    )"
    [ -n "$x_" ]
}

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

___x_cmd_starship_fz___check_preview(){
    local dir="$___X_CMD_STARSHIP_PREVIEW_DIR"
    [ "$X_STARSHIP_UPDATE_CONFIG" = 1 ] || [ ! -d "$dir" ] || return 0

    local url; local tarname="starship.preview.tar.xz"
    if [ -n "$___X_CMD_THEME_DEV_MOD" ]; then
        starship: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/starship/$tarname"
    ___x_cmd ensurefp "$cache"
    starship:info --url "$url" --unpack_dir "$dir" "Download $tarname"
    ___x_cmd curl -L -sS --speed-time 5 --speed-limit 100 "$url" > "${cache}.tmp" || {
        starship: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" || {
        starship:error "Fail to extract $cache"
        ___x_cmd rmrf "$cache" "$dir"
        return 1
    }
}

# EndSection
___x_cmd_starship___check_valid_name(){
    local name="$1"
    local l=""
    ___x_cmd_starship_ls | {
        while read -r l; do
            [ -n "$l" ] || continue
            [ "$l" != "$name" ] || return 0
        done
        return 1
    }
}

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

___x_cmd_starship_which_(){
    local name="$1"
    local fp="$___X_CMD_STARSHIP_CONFIG_DIR/${name}.toml"
    if [ -f "$fp" ]; then
        x_="$fp"
    elif ___x_cmd_starship___check_valid_name "$name"; then
        ___x_cmd_starship___update_config "$name" || return $?
        [ -f "$fp" ] || return 1
        x_="$fp"
    elif [ -f "$name" ]; then
        ___x_cmd abspath_ "$name" || return $?
        starship:debug "Custom config file -> $x_"
    else
        starship:error "Invaild value for prompt theme [name=$name]"
        return 1
    fi
}

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

___x_cmd_starship_update(){
    local X_STARSHIP_UPDATE_CONFIG=1

    starship:info --config_dir "$___X_CMD_STARSHIP_CONFIG_DIR" "Update the starship preset theme file"
    local name=
    ___x_cmd_starship_ls | while read -r name; do
        [ -n "$name" ] || continue
        ___x_cmd_starship___update_config "$name" || return $?
    done

    ___x_cmd_starship_fz___check_preview
}

___x_cmd_starship___update_config(){
    local name="$1"
    local fp="$___X_CMD_STARSHIP_CONFIG_DIR/${name}.toml"
    local tmpfp="${fp}.tmp"
    ___x_cmd ensurefp "$tmpfp" || return 1
    ___x_cmd_starship_run preset "$name" > "$tmpfp" || {
        starship:error "Failed to output [name=$name] configuration"
        ___x_cmd rmrf "$tmpfp"
        return 1
    }
    ___x_cmd mv -f "$tmpfp" "$fp" || return 1
}
