#!/bin/bash

# Colors
green="\e[38;5;82m"
red="\e[38;5;196m"
neutral="\e[0m"
orange="\e[38;5;130m"
blue="\e[38;5;39m"
yellow="\e[38;5;226m"
purple="\e[38;5;141m"
bold_white="\e[1;37m"
reset="\e[0m"
pink="\e[38;5;205m"
print_rainbow() {
    local text="$1"
    local length=${#text}
    local start_color=(0 5 0)
    local mid_color=(0 200 0)
    local end_color=(0 5 0)

    for ((i = 0; i < length; i++)); do
        local progress=$(echo "scale=2; $i / ($length - 1)" | bc)

        if (($(echo "$progress < 0.5" | bc -l))); then
            local factor=$(echo "scale=2; $progress * 2" | bc)
            r=$(echo "scale=0; (${start_color[0]} * (1-$factor) + ${mid_color[0]} * $factor)/1" | bc)
            g=$(echo "scale=0; (${start_color[1]} * (1-$factor) + ${mid_color[1]} * $factor)/1" | bc)
            b=$(echo "scale=0; (${start_color[2]} * (1-$factor) + ${mid_color[2]} * $factor)/1" | bc)
        else
            local factor=$(echo "scale=2; ($progress - 0.5) * 2" | bc)
            r=$(echo "scale=0; (${mid_color[0]} * (1-$factor) + ${end_color[0]} * $factor)/1" | bc)
            g=$(echo "scale=0; (${mid_color[1]} * (1-$factor) + ${end_color[1]} * $factor)/1" | bc)
            b=$(echo "scale=0; (${mid_color[2]} * (1-$factor) + ${end_color[2]} * $factor)/1" | bc)
        fi

        printf "\e[38;2;%d;%d;%dm%s" "$r" "$g" "$b" "${text:$i:1}"
    done
    echo -e "$reset"
}
cek_status() {
    status=$(systemctl is-active --quiet $1 && echo "aktif" || echo "nonaktif")
    if [ "$status" = "aktif" ]; then
        echo -e "${green}GOOD${neutral}"
    else
        echo -e "${red}BAD${neutral}"
    fi
}

setup_bot() {
    if ! dpkg -s nodejs >/dev/null 2>&1; then
        curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - || echo -e "${red}Failed to download Node.js setup${neutral}"
        apt-get install -y nodejs || echo -e "${red}Failed to install Node.js${neutral}"
        npm install -g npm@latest
    else
        echo -e "${green}Node.js is already installed, skipping...${neutral}"
    fi

    if [ ! -f /root/BotVPN/app.js ]; then
        git clone https://github.com/Paper890/Sanstore.git /root/BotVPN
    fi

    if ! npm list --prefix /root/BotVPN express telegraf axios moment sqlite3 >/dev/null 2>&1; then
        npm install --prefix /root/BotVPN sqlite3 express crypto telegraf axios dotenv
    fi

    if [ -n "$(ls -A /root/BotVPN)" ]; then
        chmod +x /root/BotVPN/*
    fi
}

server_app() {
    clear
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo -e "   ${green}.:::. BOT SELLVPN TELEGRAM .:::.   ${neutral}"
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo -e "       ${green}•${neutral} server create user"
    echo -e "       ${green}•${neutral} server delete user"
    echo -e "       ${green}•${neutral} server addsaldo user"
    echo -e "       ${green}•${neutral} server ceksaldo user"
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    read -p "Masukkan token bot: " token
    while [ -z "$token" ]; do
        read -p "Masukkan token bot: " token
        if [ -z "$token" ]; then
            echo -e "${red}Token bot tidak boleh kosong. Silakan coba lagi.${neutral}"
        fi
    done
    while [ -z "$adminid" ]; do
        read -p "Masukkan admin ID: " adminid
        if [ -z "$adminid" ]; then
            echo -e "${red}Admin ID tidak boleh kosong. Silakan coba lagi.${neutral}"
        fi
    done
        while [ -z "$namastore" ]; do
        read -p "Masukkan nama store: " namastore
        if [ -z "$namastore" ]; then
            echo -e "${red}Nama store tidak boleh kosong. Silakan coba lagi.${neutral}"
        fi
    done
    rm -f /root/BotVPN/.vars.json
    echo "{
  \"BOT_TOKEN\": \"$token\",
  \"USER_ID\": \"$adminid\",
  \"NAMA_STORE\": \"$namastore\",
  \"PORT\": \"50123\"
}" >/root/BotVPN/.vars.json
    cat >/etc/systemd/system/sellvpn.service <<EOF
[Unit]
Description=App Bot sellvpn Service
After=network.target

[Service]
ExecStart=/bin/bash /usr/bin/sellvpn
Restart=always
User=root
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/usr/bin

[Install]
WantedBy=multi-user.target
EOF
echo '#!/bin/bash' >/usr/bin/server_sellvpn
echo "USER_ID=$adminid" >>/usr/bin/server_sellvpn
echo "BOT_TOKEN=$token" >>/usr/bin/server_sellvpn
echo 'curl -s -F chat_id="$USER_ID" -F document=@"/root/BotVPN/sellvpn.db" "https://api.telegram.org/bot$BOT_TOKEN/sendDocument" >/dev/null 2>&1' >>/usr/bin/server_sellvpn
echo '#!/bin/bash' >/usr/bin/sellvpn
echo "cd /root/BotVPN" >>/usr/bin/sellvpn
echo "node app.js" >>/usr/bin/sellvpn
chmod +x /usr/bin/server_sellvpn
chmod +x /usr/bin/sellvpn
cat >/etc/cron.d/server_sellvpn <<EOF
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
0 */6 * * * root /usr/bin/server_sellvpn
EOF

    systemctl daemon-reload >/dev/null 2>&1
    systemctl enable sellvpn.service >/dev/null 2>&1
    systemctl start sellvpn.service >/dev/null 2>&1
    systemctl restart sellvpn.service >/dev/null 2>&1
    service cron restart
    printf "\033[5A\033[0J"
    echo -e "       Status Server is "$(cek_status sellvpn.service)""
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo -e "${green}Bot has been installed and running.${neutral}"
    echo -e "${green}Type ${bold_white}/start${neutral} or ${bold_white}menu${neutral} in the telegram bot${neutral}"
}

if [[ ${1} == "sellvpn" ]]; then
    setup_bot
    server_app
else
    echo -e "${red}Invalid command. Use: ${yellow}start sellvpn${neutral}"
    exit 1
fi 
