#!/usr/bin/env bash

# shellcheck shell=bash

# ``````````````````````````````````````````````````````````````````````````````
# Function name: show_config()
#
# Description:
#   Show config.
#
# Usage:
#   show_config
#
# Examples:
#   show_config
#

function show_config() {

  # shellcheck disable=SC2034
  local _FUNCTION_ID="show_config"
  local _STATE=0

  # shellcheck disable=SC2154
  if [[ "$session_type" == "main" ]] ; then

    local _session_arg="$2"

  elif [[ "$session_type" == "module" ]] ; then

    local _session_arg="$3"

  fi

  _break=0

  echo

  # shellcheck disable=SC2154
  for _svar in "${_variables_stack[@]}" ; do

    # shellcheck disable=SC2034
    _key_description=$(echo "$_svar" | awk -v FS="(;|;)" '{print $1}')
    # shellcheck disable=SC2034
    _key_values=$(echo "$_svar" | awk -v FS="(;|;)" '{print $2}')
    # shellcheck disable=SC2034
    _key_id=$(echo "$_svar" | awk -v FS="(;|;)" '{print $3}')
    # shellcheck disable=SC2034
    _key_var=$(echo "$_svar" | awk -v FS="(;|;)" '{print $4}')

    # For debug:
    # printf "%s , %s , %s , %s\n" \
    # "_session_key: $_session_key" \
    # "_key_description: $_key_description" \
    # "_key_values: $_key_values" \
    # "_key_id: $_key_id" \
    # "_key_var: $_key_var"

    if [[ -z "$_key_id" ]] ; then

      printf "stdout: %s\\n" "invalid session param"

    fi

    # Show specific key.
    if [[ "$_session_arg" == "$_key_id" ]] ; then

      printf "  Description: \\e[0;37m%s\\e[m\\n" \
      "$_key_description"
      printf "    Examples: \\e[0;37m%s\\e[m\\n" \
      "$_key_values"
      printf "    { \"\\e[0;38m%s\\e[m\":\"\\e[1;36m%s\\e[m\" }\\n\\n" \
      "$_key_id" \
      "$_key_var"

      _break=1

    elif [[ "$_break" -eq 0 ]] ; then

      # Show all keys.
      if [[ -z "$_session_arg" ]] ; then

        printf "  Description: \\e[0;37m%s\\e[m\\n" \
        "$_key_description"
        printf "    Examples: \\e[0;37m%s\\e[m\\n" \
        "$_key_values"
        printf "    { \"\\e[0;38m%s\\e[m\":\"\\e[1;36m%s\\e[m\" }\\n\\n" \
        "$_key_id" \
        "$_key_var"

      fi

    fi

  done

  return $_STATE

}
