# shellcheck disable=all

# Section: init
# zsh https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html#Visual-effects

autoload -U colors && colors
zmodload zsh/datetime

______x_cmd_theme_setup_zsh_basecolor() {
    typeset -AHg fg_dim bg_dim fg_light bg_light
    for k in ${(k)color[(I)fg-*]}; do
        fg_dim[${k#fg-}]="%{${___X_CMD_UNSEENCHAR_033}[2;${color[$k]}m%}"
        fg_light[${k#fg-}]="%{${___X_CMD_UNSEENCHAR_033}[9${color[$k]#3}m%}"
    done
    for k in ${(k)color[(I)bg-*]}; do
        bg_dim[${k#bg-}]="%{${___X_CMD_UNSEENCHAR_033}[2;${color[$k]}m%}"
        bg_light[${k#bg-}]="%{${___X_CMD_UNSEENCHAR_033}[10${color[$k]#4}m%}"
    done
    unset k
}

______x_cmd_theme_setup_zsh_basecolor


###
  # @description: set up style color
  # @example: ___x_cmd_theme_style fg-red bold hello world
###
___x_cmd_theme_style() {
    local x_=""
    ___x_cmd_theme_style_ "$@" || return
    printf "%s" "$x_"
}

___x_cmd_theme_style_(){
    ___x_cmd_theme_style_zsh_ "$@"
}

___x_cmd_theme_style_zsh_() {
    x_=""

    local IFS=" "
    case "$1" in
        %*%)
                local op="${1#\%}";  shift
                local msg="$*"
                op="${op//\#/\\#}"
                eval set -- "${op%\%}" "--" "\"\$msg\""
        ;;
    esac

    local color_code=""
    while [ $# -gt 0 ]; do
        case "$1" in
            bold)                   x_="${x_}%B"                                            ;;
            dim)                    x_="${x_}%{${___X_CMD_UNSEENCHAR_033}[2m%}"             ;;
            reverse)                x_="${x_}%{${___X_CMD_UNSEENCHAR_033}[7m%}"             ;;
            underline)              x_="${x_}%U"                                            ;;
            black|red|green|yellow|blue|magenta|cyan|white)
                                    x_="${x_}%{$fg[$1]%}"                                   ;;
            fg-[0-9]*)              x_="${x_}%{${___X_CMD_UNSEENCHAR_033}[38;5;${1#fg-}m%}" ;;
            fg-*)                   x_="${x_}%{$fg[${1#fg-}]%}"                             ;;
            bfg-*)                  x_="${x_}%{$fg_bold[${1#bfg-}]%}"                       ;;
            hfg-*)                  x_="${x_}%{$fg_light[${1#hfg-}]%}"                      ;;
            dfg-*)                  x_="${x_}%{$fg_dim[${1#dfg-}]%}"                        ;;
            colourfg-*)             x_="${x_}%{${___X_CMD_UNSEENCHAR_033}[38;5;${1#colourfg-}m%}"   ;;
            rgbfg-*)                ___x_cmd_theme_set___rgb_color_code "${1#rgbfg-}"
                                    x_="${x_}%{%{${___X_CMD_UNSEENCHAR_033}[38;2;${color_code}m%}%}" ;;
            bg-[0-9]*)              x_="${x_}%{${___X_CMD_UNSEENCHAR_033}[48;5;${1#bg-}m%}" ;;
            bg-*)                   x_="${x_}%{$bg[${1#bg-}]%}"                             ;;
            bbg-*)                  x_="${x_}%{$bg_bold[${1#bbg-}]%}"                       ;;
            hbg-*)                  x_="${x_}%{$bg_light[${1#hbg-}]%}"                      ;;
            dbg-*)                  x_="${x_}%{$bg_dim[${1#dbg-}]%}"                        ;;
            colourbg-*)             x_="${x_}%{${___X_CMD_UNSEENCHAR_033}[48;5;${1#colourbg-}m%}"   ;;
            rgbbg-*)                ___x_cmd_theme_set___rgb_color_code "${1#rgbbg-}"
                                    x_="${x_}%{%{${___X_CMD_UNSEENCHAR_033}[48;2;${color_code}m%}%}" ;;
            reset)                  x_="${x_}%{$reset_color%}"                              ;;
            --)                     shift; ___x_cmd_theme_style_zsh_end_color "$@"; return       ;;
            *)                      ___x_cmd_theme_style_zsh_end_color "$@"; return              ;;
        esac
        shift
    done
}

___x_cmd_theme_style_zsh_end_color(){
    if [ -z "$___X_CMD_THEME_PS1_OUTSIDE" ]; then
        x_="${x_}$*%{$reset_color%}%b%u"
    else
        local style=
        print -v x_ -P "$x_"
        print -v style -P "%{$reset_color%}%b%u"
        x_="${x_}$*${style}"
    fi
}

_______x_cmd_theme_set_complete_color() {
    local color_code="${1:?Provide Color Code e.g 32}"
    local x_cmd_theme_ls_color
    # add base hightlight-block and description complete hightlight
    x_cmd_theme_ls_color="ma=7;$color_code:$LS_COLORS:"
    x_cmd_theme_ls_color="=(#b) #(*) #(-- *)=0=$color_code=0:$x_cmd_theme_ls_color:"

    # set default (cmd option desc) complete hightlight
    zstyle ':completion:*:default' list-colors "${(s.:.)x_cmd_theme_ls_color}"

    # set x command complete hightlight
    zstyle ':completion:*:*:x:*' list-colors "=^(*.*)=$color_code:"
}

# ___x_cmd_theme_style() {
#     while [ $# -gt 0 ]; do
#         case "$1" in
#             bold)                                                   printf "%s" "%B"                                           ;;
#             underline)                                              printf "%s" "%U"                                           ;;
#             black|red|green|yellow|blue|magenta|cyan|white)         printf "%s" "%{$fg[$1]%}"                                  ;;
#             fg-*)                                                   printf "%s" "%{$fg[${1#fg-}]%}"                            ;;
#             bfg-*)                                                  printf "%s" "%{$fg_bold[${1#bfg-}]%}"                      ;;
#             hfg-*)                                                  printf "%s" "%{$fg_light[${1#hfg-}]%}"                     ;;
#             dfg-*)                                                  printf "%s" "%{$fg_dim[${1#dfg-}]%}"                       ;;
#             colourfg-*)                                             printf "%s" "%{$FG[${1#colourfg-}]%}"                      ;;
#             rgbfg-*)                                                local color_code=""; ___x_cmd_theme_set___rgb_color_code "${1#rgbfg-}"; printf "%s" "%{%{${___X_CMD_UNSEENCHAR_033}[38;2;${color_code}m%}%}" ;;
#             bg-*)                                                   printf "%s" "%{$bg[${1#bg-}]%}"                            ;;
#             bbg-*)                                                  printf "%s" "%{$bg_bold[${1#bbg-}]%}"                      ;;
#             hbg-*)                                                  printf "%s" "%{$bg_light[${1#hbg-}]%}"                     ;;
#             dbg-*)                                                  printf "%s" "%{$bg_dim[${1#dbg-}]%}"                       ;;
#             colourbg-*)                                             printf "%s" "%{$BG[${1#colourbg-}]%}"                      ;;
#             rgbbg-*)                                                local color_code=""; ___x_cmd_theme_set___rgb_color_code "${1#rgbbg-}"; printf "%s" "%{%{${___X_CMD_UNSEENCHAR_033}[48;2;${color_code}m%}%}" ;;
#             reset)                                                  printf "%s" "%{$reset_color%}"                             ;;
#             --)                                                     shift; printf "%s" "$*%{$reset_color%}%b%u"; return        ;;
#             *)                                                      printf "%s" "$*%{$reset_color%}%b%u"; return
#         esac
#         shift
#     done
# }
