#!/bin/bash
set -u
color_text() {
    local color=$1
    local text=$2
    declare -A colors=(
        ["bright_white"]="\033[97m" ["bg_black"]="\033[40m" ["bg_red"]="\033[41m"
        ["bg_green"]="\033[42m" ["bg_yellow"]="\033[43m" ["bg_blue"]="\033[44m"
        ["bg_magenta"]="\033[45m" ["bg_cyan"]="\033[46m" ["bg_white"]="\033[47m"
        ["bg_bright_black"]="\033[100m" ["bg_bright_red"]="\033[101m"
        ["bg_bright_green"]="\033[102m" ["bg_bright_yellow"]="\033[103m"
        ["bg_bright_blue"]="\033[104m" ["bg_bright_magenta"]="\033[105m"
        ["bg_bright_cyan"]="\033[106m" ["bg_bright_white"]="\033[107m"
    )

    if [[ -n "${colors[$color]}" ]]; then
        echo -e "${colors[$color]}${text}\033[0m"
    else
        echo "Color '$color' not recognized."
    fi
}

# Function to center text based on terminal width, handling multi-line input
center_text() {
    local text="$1"
    local cols=$(tput cols)
    while IFS= read -r line; do
        local text_length=${#line}
        if [ $text_length -lt $cols ]; then
            local padding=$(( (cols - text_length) / 2 ))
            printf "%${padding}s%s\n" " " "$line"
        else
            echo "$line"
        fi
    done <<< "$text"
}

# Function to calculate percentage
get_percentage() {
    used=$1
    total=$2
    awk -v used="$used" -v total="$total" 'BEGIN { printf("%.2f%%", (used / total) * 100) }'
}

# Get memory and swap usage details
memory_usage=$(free -h --si | awk '/^Mem/ {print $3 " / " $2}')
swap_usage=$(free -h --si | awk '/^Swap/ {print $3 " / " $2}')

# Calculate percentage for memory
memory_used=$(free --si | awk '/^Mem/ {print $3}')
memory_total=$(free --si | awk '/^Mem/ {print $2}')
memory_percentage=$(get_percentage "$memory_used" "$memory_total")

# Calculate percentage for swap
swap_used=$(free --si | awk '/^Swap/ {print $3}')
swap_total=$(free --si | awk '/^Swap/ {print $2}')
swap_percentage=$(get_percentage "$swap_used" "$swap_total")

# Function to display ASCII art of a computer screen with SYSI
display_logo() {
    center_text "$(color_text "bg_red" "    _________  ")"
    center_text "$(color_text "bg_green" "     /         |")"
    center_text "$(color_text "bg_yellow" "    / _______   |")"
    center_text "$(color_text "bg_blue" "   | |       |  |")"
    center_text "$(color_text "bg_magenta" "   | |  SYSI |  |")"
    center_text "$(color_text "bg_cyan" "   | |_______|  |")"
    center_text "$(color_text "bg_white" "   |           |")"
    center_text "$(color_text "bg_bright_red" "   |  _______  |")"
    center_text "$(color_text "bg_bright_yellow" "   | /       \\ |")"
    center_text "$(color_text "bg_bright_blue" "   |/         \\|")"
    center_text "$(color_text "bg_green" "    \\___________/")"
    echo""
}

# Function to display system informations
display_info() {
    os=$(awk -F= '/^PRETTY_NAME=/{print $2}' /etc/os-release | tr -d '"')
    center_text "$(color_text "bg_green" "  Operating System")"
    center_text "$(color_text "bright_white"  "$os")"
    echo""
    center_text "$(color_text "bg_green" "  Host")"
    center_text "$(color_text "bright_white" "$(cat /proc/sys/kernel/hostname)")"
    echo""
    center_text "$(color_text "bg_green" "  Model")"
    center_text "$(color_text "bright_white" "$(cat /sys/devices/virtual/dmi/id/board_{name,vendor} | awk '!(NR%2){print$1,p}{p=$0}')")"
    echo""
    center_text "$(color_text "bg_yellow" "  Logged-in Users")"
    center_text "$(w)"
    echo""
    center_text "$(color_text "bg_yellow" "  Kernel Name")"
    center_text "$(color_text "bright_white" "$(uname -s)")"
    echo""
    center_text "$(color_text "bg_yellow" "  Kernel Version")"
    center_text "$(color_text "bright_white" "$(uname -r)")"
    echo""
    center_text "$(color_text "bg_yellow" "  Kernel Release \e[0m")"
    center_text "$(uname -v)"
    echo""
    center_text "$(color_text "bg_yellow" "  Kernel Architecture")"
    center_text "$(color_text "bright_white" "$(uname -m)")"
    echo""
    center_text "$(color_text "bg_white" "  Uptime")"
    center_text "$(color_text "bg_blue" "\e[0m $(uptime -p)")"
    echo""
    center_text "$(color_text "bg_white" " Clock/Time")"
    center_text "$(color_text "bright_white" "\e[0m $(date '+%Y-%m-%d %H:%M:%S')")"
    echo""
    center_text "$(color_text "bg_bright_black" "  Packages: $(get_package_count)")"
    echo""
    center_text "$(color_text "bg_bright_black" "  Resolution")"
    center_text "$(color_text "bright_white" "\e[0m $(xrandr | grep -i  "*" )")"
    echo""
    center_text "$(color_text "bg_bright_black" "  Machine Hardware Platform")"
    center_text "$(color_text "bg_white" "\e[0m $(uname -i)")"
    echo""
    center_text "$(color_text "bg_bright_black" "  Processor Type")"
    center_text "$(color_text "bg_bright_black" "\e[0m $(uname -p)")"
    echo""
    center_text "$(color_text "bg_blue" "  CPU")"
    center_text "$(color_text "bg_blue" "\e[0m $(grep "model name" /proc/cpuinfo | cut -d ' ' -f 3- | uniq)")"
    echo""
    center_text "$(color_text "bg_blue" "  CPU Cores")"
    center_text "$(color_text "bg_blue" "\e[0m $(awk '/^cpu cores/ {print $4; exit}' /proc/cpuinfo)")"
    echo""
    center_text "$(color_text "bg_blue" "  CPU Threads")"
    center_text "$(color_text "bg_blue" "\e[0m $(awk '/^processor/ {count++} END {print count}' /proc/cpuinfo)")"
    echo""
    center_text "$(color_text "bg_blue" "  Fan Speeds")"
    center_text "$(color_text "bg_blue" "\e[0m$(sensors | grep -i 'fan' | awk '{print "      ", $1, $2, $3, $4}')")"
    echo""
if command -v sensors &> /dev/null; then
     center_text "$(color_text "bg_bright_blue" "  CPU Temperature")"
    # Collect all lines containing "Core" from sensors output
    echo""
    core_lines=$(sensors | grep "Core")
    # Iterate over each line and center it
    while IFS= read -r line; do
        center_text "$(color_text "bg_red" "$line")"
    done <<< "$core_lines"
else
    center_text "$(color_text "bg_red" "  CPU Temperature: Not available (lm_sensors not installed)")"
fi
echo""
    center_text "$(color_text "bg_bright_black" "  GPU")"
    center_text "$(color_text "bg_bright_black" "\e[0m $(lspci | grep VGA | cut -d ':' -f 3 | cut -d '[' -f 1,2 | sed 's/^ *//')")"
    echo""
    if ! command -v sensors &> /dev/null; then
    center_text "$(color_text "red" "lmsensors not installed")"
else
    gpu_temp=$(sensors | grep -i 'gpu')
    if [ -z "$gpu_temp" ]; then
        center_text "$(color_text "bg_bright_red" "  No GPU temperature data available")"
    else
        center_text "$(color_text "bg_bright_black" "  GPU Temperatures:" "$gpu_temp")"
       fi
    fi
    echo""
    center_text "$(color_text "bg_bright_magenta" "  Memory")"
    center_text "$(color_text "bg_black" "($memory_usage) ($memory_percentage)")" 
    echo""
    center_text "$(color_text "bg_bright_magenta" "Swap")"
    center_text "$(color_text "bg_black" "($swap_usage) ($swap_percentage)")"
    echo""
        local disk_usage=$(df -h / | awk 'NR==2 {print "(" $5 " used) " $3 "/" $2 }')
    center_text "$(color_text "bg_bright_black" "  Disk Usage: ${disk_usage}")"
    echo""
        local battery_info=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state|to full|percentage")
    center_text "$(color_text "bg_bright_black" "  Battery:")"
    while IFS= read -r line; do
        center_text "$line"
    done <<< "$battery_info"
    echo""
        center_text "$(color_text "bg_cyan" "  Top Processes by CPU Usage")"
    ps -eo pid,%cpu,%mem,cmd --sort=-%cpu | head -n 11 | while IFS= read -r line; do
        center_text "$line"
    done
    echo""
    center_text "$(color_text "bg_cyan" "  Top Processes by Memory Usage")"
    ps -eo pid,%cpu,%mem,cmd --sort=-%mem | head -n 11 | while IFS= read -r line; do
        center_text "$line"
    done
    echo""
  center_text "$(color_text "bg_yellow" " Disk I/O Statistics")"
    local io_stats=$(iostat)
    while IFS= read -r line; do
        center_text "$line"
    done <<< "$io_stats"
    echo""
    center_text "$(color_text "bg_bright_black" "  Mounted Drives")"
    echo ""
    local header="$(color_text "bg_blue" "      Filesystem                        Size       Used      Use%")"
    center_text "$header"
    local drives=$(df -h | awk 'NR>1 {printf "      %-30s %-10s %-10s %-10s\n", $1, $2, $3, $5}')
    while IFS= read -r line; do
        center_text "$line"
    done <<< "$drives"
echo""

# [Additional functionalities enable manually]

# [#Display Network Interfaces]
#    center_text "$(color_text "bg_yellow" "  Network Interfaces:")"
#    ip addr show | sed 's/^/      /'
#echo""

# [# Display GPU temperatures for Nvidia, AMD, and Intel GPUs if available]
#    center_text "$(color_text "bg_yellow" "  GPU Temperatures:")"
    
    # Check and display Nvidia GPU temperature
#    if command -v nvidia-smi &> /dev/null; then
#        center_text "$(color_text "bg_yellow" "   Nvidia:")"
#        nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader | awk '{print "      ", $1, "°C"}'
#    fi

    # Check and display AMD GPU temperature
#    if command -v radeontop &> /dev/null; then
#        center_text "$(color_text "bg_yellow" "   AMD:")"
#        radeontop -l1 | grep "Temperature" | sed 's/^/      /'
#    fi

    # Check and display Intel GPU temperature
#    if command -v intel_gpu_top &> /dev/null; then
#        center_text "$(color_text "bg_yellow" "   Intel:")"
#        intel_gpu_top -s 1 | awk '/Rendering/ {print "      ", $7, "°C"}'
#    fi

# [Display Uptime]
#    center_text "$(color_text "bg_yellow" "  Uptime:") $(uptime)"
#echo ""

# [Display Shell]
 # center_text "$(color_text "bg_green" " Shell:") $SHELL"

# [Display Firewall Information requires password]
    #center_text "$(color_text "bg_yellow" "  Firewall Information:")"
    #center_text "$(color_text "bg_yellow" "   UFW Status:")"
    #ufw status verbose | sed 's/^/      /'  # Adjust format as needed for your system's output
    #center_text "$(color_text "bg_yellow" "   IPTables Rules:")"
    #iptables -L -v -n | sed 's/^/      /'  # Adjust format as needed for your system's output
    #center_text "$(color_text "bg_yellow" "   Firewalld Zones and Services:")"
    #firewall-cmd --list-all-zones | sed 's/^/      /'  # Adjust format as needed for your system's output
    #center_text "$(color_text "bg_yellow" "   Nftables Rules:")"
    #nft list ruleset | sed 's/^/      /'  # Adjust format as needed for your system's output

# [resolution]
# Display Resolution
#    center_text "$(color_text "bg_yellow" " Resolution")"
#    center_text "$(color_text "bg_yellow"  "\e[0m $(hwinfo --monitor)")"
#    echo""
}
# Function to display a random quote
display_quote() {
    quotes=(
         "Talk is cheap. Show me the code. - Linus Torvalds"
        "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program. - Linus Torvalds"
        "Free software is software that respects your freedom and the social solidarity of your community. So it's free as in freedom. - Richard Stallman"
        "Control over the use of one's ideas really constitutes control over other people's lives; and it is usually used to make their lives more difficult. - Richard Stallman"
        "Given enough eyeballs, all bugs are shallow. - Eric S. Raymond"
        "The next best thing to having good ideas is recognizing good ideas from your users. Sometimes the latter is better. - Eric S. Raymond"
        "The Web as I envisaged it, we have not seen it yet. The future is still so much bigger than the past. - Tim Berners-Lee"
        "The only way to make software secure, reliable, and fast is to put it out in the public domain and let people attack it, play with it, and break it. - Brian Behlendorf"
        "I believe that openness is the key to creativity and innovation. - Mitchell Baker"
        "May the Force be with you. -Star Wars (many characters)"
        "Knowledge is power. -Sir Francis Bacon"
        "Information flow is what the Internet is about. Information sharing is power. If you dont share your ideas smart people cant do anything about them and your all remain anonymous and powerless. -Vint cerf"
        "When we have welcoming communities of contributors, open source software gets better and more useful to everyone. -Limor Fried"
        "Open source is about collaborating; not competing. -Kelesey Hightower"
    )
    random_index=$((RANDOM % ${#quotes[@]}))
  center_text "$(color_text "bg_cyan" "Random Quote: ${quotes[$random_index]}")"
  echo""
}

# Function to display a random programming joke
display_joke() {
    jokes=(
        "Why do programmers prefer dark mode? Because light attracts bugs!"
        "There are 10 types of people in the world: those who understand binary and those who don't."
        "A SQL statement walks into a bar and sees two tables. It approaches, and asks, 'May I join you?'"
        "How many programmers does it take to change a light bulb? None. It's a hardware problem."
        "your mama so fat 32 she cant store file over 4 GB"
        "your mama so ugly she turn sdd into flopy"
        "Knock, knock. Who’s There? Very long pause… Java."
        "Things aren’t always #000000 and #FFFFFF"
        "Why do Java programmers have to wear glasses? Because they don’t C#"
        "There’s no place like 127.0.0.1"
        "My love for you has no bugs"
        "Real programmers count from 0"
        "while (alive) { eat(); sleep (); code ();}"
        "Binary: It’s as easy as 01, 10, 11"
    )
    random_index=$((RANDOM % ${#jokes[@]}))
    center_text "$(color_text "bg_magenta" "Joke of the Day: ${jokes[$random_index]}")"
    echo""
}

# Function to display a random fun fact
display_fun_fact() {
    facts=(
        "The first computer bug was an actual bug: a moth stuck in a Harvard Mark II in 1947."
        "The first 1GB hard drive, announced in 1980, weighed over 500 pounds and cost 40000."
        "The first computer virus was created in 1983 by Fred Cohen as part of his PhD thesis."
        "The first programming language was Ada Lovelace's algorithm for Charles Babbage's Analytical Engine."
        "The first webcam was used to monitor a coffee pot at the University of Cambridge."
        "The first computer mouse was made of wood."
        "The Firefox logo isnt a fox. is Red panda."
        "Nintendo made playing cards."
        "As of 2017, 2.1 millions people still use dial up."
        "Google's First Tweet was in binary"
        "Motorola produced the first handheld mobile phone."
        "YouTube uploads 72 hours of video every single minute. "
        "The First Computer Weighed More Than 24,493.988 KG/27 Tons."
        "The C Language was not called C at the beginning"
        
    )
    random_index=$((RANDOM % ${#facts[@]}))
    center_text "$(color_text "bg_yellow" "Fun Fact: ${facts[$random_index]}")"
    echo""
}

get_package_count() {
    local package_count="N/A"
if command -v dpkg >/dev/null 2>&1; then
    pkgs=$(dpkg -l | grep '^ii' | wc -l)
    source="(dpkg)"
elif command -v pacman >/dev/null 2>&1; then
    pkgs=$(pacman -Q | wc -l)
    source="(pacman)"
elif command -v equo >/dev/null 2>&1; then
    pkgs=$(equo query list installed -q | wc -l)
    source="(equo)"
elif command -v eopkg >/dev/null 2>&1; then
    pkgs=$(eopkg list-installed | wc -l)
    source="(eopkg)"
elif command -v tce-status >/dev/null 2>&1; then
    pkgs=$(tce-status -i | wc -l)
    source="(tce-status)"
elif command -v apk >/dev/null 2>&1; then
    pkgs=$(apk info | wc -l)
    source="(apk)"
elif command -v xbps-query >/dev/null 2>&1; then
    pkgs=$(xbps-query -l | wc -l)
    source="(xbps-query)"
elif command -v emerge >/dev/null 2>&1; then
    pkgs=$(qlist -I | wc -l)
    source="(emerge)"
elif command -v rpm >/dev/null 2>&1; then
    pkgs=$(rpm -qa | wc -l)
    source="(rpm)"
fi

    echo "$pkgs" "$source"
}

# Function to get information about various themes
# !doesnt work on debian! 
get_theme_info() {
    local gtk_theme icon_theme cursor_theme shell_theme window_manager_theme qt_theme

    # Initialize a flag to check if any themes are found
    local any_themes_found=0

    # Get GTK theme
    if gtk_theme=$(gsettings get org.gnome.desktop.interface gtk-theme 2>/dev/null); then
        gtk_theme="${gtk_theme//\'/}"  # Remove single quotes from the output
        center_text "$(color_text "bg_red" "GTK Theme: $gtk_theme")"
        any_themes_found=1
    fi

    # Get icon theme
    if icon_theme=$(gsettings get org.gnome.desktop.interface icon-theme 2>/dev/null); then
        icon_theme="${icon_theme//\'/}"  # Remove single quotes from the output
        center_text "$(color_text "bg_green" "Icon Theme: $icon_theme")"
        any_themes_found=1
    fi

    # Get cursor theme
    if cursor_theme=$(gsettings get org.gnome.desktop.interface cursor-theme 2>/dev/null); then
        cursor_theme="${cursor_theme//\'/}"  # Remove single quotes from the output
        center_text "$(color_text "bg_blue" "Cursor Theme: $cursor_theme")"
        any_themes_found=1
    fi

    # Get GNOME Shell theme
    if shell_theme=$(gsettings get org.gnome.shell.extensions.user-theme name 2>/dev/null); then
        shell_theme="${shell_theme//\'/}"  # Remove single quotes from the output
        center_text "$(color_text "bg_yellow" "Shell Theme: $shell_theme")"
        any_themes_found=1
    fi

    # Get Window Manager theme (example for Openbox)
    if [ -f ~/.config/openbox/rc.xml ]; then
        window_manager_theme=$(grep -E '^theme=' ~/.config/openbox/rc.xml | sed 's/theme=//')
        if [ -n "$window_manager_theme" ]; then
            center_text "$(color_text "bg_cyan" "Window Manager Theme: $window_manager_theme")"
            any_themes_found=1
        fi
    fi

    # Get Qt theme (example for Qt5 applications)
    if [ -f ~/.config/qt5ct/qt5ct.conf ]; then
        qt_theme=$(grep -E '^theme=' ~/.config/qt5ct/qt5ct.conf | sed 's/theme=//')
        if [ -n "$qt_theme" ]; then
            center_text "$(color_text "bg_magenta" "Qt Theme: $qt_theme")"
            any_themes_found=1
        fi
    fi

    # If no themes were found, provide a message
    if [ $any_themes_found -eq 0 ]; then
        center_text "$(color_text "bg_gray" "No themes or settings found.")"
    fi
}
    # Easter egg for ASUSTeK model
    get_model() {
    model=$(cat /sys/devices/virtual/dmi/id/board_{name,vendor} | awk '!(NR%2){print$1,p}{p=$0}')
    if [[ "$model" == *"ASUSTeK"* ]]; then
        echo ""
        center_text "$(color_text "bg_bright_black" "Asus more like ASSUS")"
    fi
    echo""
}

#stuff about the program
sysi-info() {
center_text "$(color_text "bg_bright_black" "language")"
echo""
center_text "$(color_text "bg_bright_blue" "english")"
echo""
center_text "$(color_text "bg_bright_black" "sysi version")"
echo""
center_text "$(color_text "bg_bright_blue" "1.0.9")"
echo""
}

# Main function to display output
main() {
    clear
    display_logo
    display_info
    get_theme_info
    get_model
    display_quote
    display_joke
    display_fun_fact
    sysi-info
}
# Execute main function
main
