# shellcheck shell=dash

___x_cmd_theme_control_app(){
    local x_file=

    theme:info "Welcome to gallery for theme previews online -> https://x-cmd.com/theme"

    ___x_cmd_theme_which___getfile_ classify.yml || {
        theme:error "Fail to download theme resource -> classfiy.yml"
        return 1
    }

    local x_=; ___x_cmd storeread_ ___x_cmd_theme_app "$x_file" || return
    [ -n "$x_" ] || return

    local id
    ___x_cmd ui select id "Next for theme -> $x_"                       \
        "x theme use $x_     # Apply globally"                   \
        "x theme try $x_     # Apply only in current shell env"  \
        "x open https://x-cmd.com/theme/${x_%/*}"                \
        "EXIT"

    case "$id" in
        1)
            # TODO: In the future, we might provide use theme app to choose the specific scenario
            theme:info "cmd -> x theme use $x_"
            ___x_cmd theme use "$x_"
            ;;
        2)
            theme:info "cmd -> x theme try $x_"
            ___x_cmd theme try "$x_"
            ;;
        3)
            theme:info "Open browser for more info about ${x_%/*}"
            ___x_cmd open "https://x-cmd.com/theme/${x_%/*}"
            ;;
        4)
            theme:info "exit the theme app"
            return 0
            ;;
    esac

}

___x_cmd_theme_app(){
    ___X_CMD_SHBIN_CODE='
        xrc theme ;
        xrc:mod:lib theme app/_index ;
        "$@"
    ' ___x_cmd antizshbin ___x_cmd_theme_app___ "$@"
}

___x_cmd_theme_app___(){
    ___x_cmd __ttyupdate
    theme:debug --col "$COLUMNS" --row "$LINES" "windows size updated"
    { [ "$COLUMNS" -ge 100 ] && [ "$LINES" -ge 24 ]; } || theme:warn "The current terminal screen size may be too small"

    local classify_fp="$1"
    local navi_row="${___X_CMD_THEME_NAVI_ROW:-8}"
    [ -f "$classify_fp" ] || {
        local x_file=; ___x_cmd_theme_which___getfile_ classify.yml || return
        classify_fp="$x_file"
    }

    local ___X_CMD_TUI_CURRENT_THEME_POSITION=; local _exitcode=; local ___X_CMD_THEME_SCHEME=
    local x_=; ___x_cmd tui position get_ theme

    ___X_CMD_THEME_NAVI_POSITION="$x_" \
    navi_row="$navi_row" \
    classify_fp="$classify_fp" \
    style_preview="${___X_CMD_THEME_DATA_PATH}/style-preview" \
    ___x_cmd tui bigapp --clear-on-exit \
        -f "$___X_CMD_ROOT_MOD/awk/lib/re.awk"              \
        -f "$___X_CMD_ROOT_MOD/yml/lib/yutil.awk"           \
        -f "$___X_CMD_ROOT_MOD/yml/lib/ystr.awk"            \
        -f "$___X_CMD_ROOT_MOD/yml/lib/yparse.value.awk"    \
        -f "$___X_CMD_ROOT_MOD/yml/lib/yparse.json.awk"     \
        -f "$___X_CMD_ROOT_MOD/yml/lib/yparse.awk"          \
        -f "$___X_CMD_ROOT_MOD/tui/lib/eg.comp/navi.comp.awk" \
        -f "$___X_CMD_ROOT_MOD/theme/lib/app/app.awk"
    _exitcode="$?"

    [ -z "$___X_CMD_THEME_SCHEME" ] || printf "%s\n" "$___X_CMD_THEME_SCHEME"
    ___x_cmd tui position store theme "$___X_CMD_TUI_CURRENT_THEME_POSITION"
    return "$_exitcode"
}

