#!/bin/bash
clear

# --- WARNA ---
yellow="\033[0;33m"
ungu="\033[0;35m"
Red="\033[91;1m"
Xark="\033[0m"
BlueCyan="\033[5;36m"
WhiteBe="\033[5;37m"
GreenBe="\033[5;32m"
YellowBe="\033[5;33m"
BlueBe="\033[5;34m"

# Ambil nama admin
if [[ -e /etc/xray/username ]]; then
    nama=$(cat /etc/xray/username)
else
    nama="HOKAGE LEGEND"
fi

# --- FUNGSI GARIS ---
function baris_panjang() {
  echo -e "${BlueCyan} ——————————————————————————————————— ${Xark} "
}

# --- BANNER ---
function xdxl_Banner() {
    clear
    baris_panjang
    echo -e "${ungu}           $nama       ${Xark} "
    baris_panjang
}

function Sc_Credit(){
    sleep 1
    baris_panjang
    echo -e "${ungu}  Terimakasih Telah Menggunakan ${Xark}"
    echo -e "${ungu}           Script Credit ${Xark}"
    echo -e "${ungu}           $nama ${Xark}"
    baris_panjang
    echo ""
    read -rp "press any key for back"
    # Cek jika perintah menu ada, jika tidak exit
    if command -v menu &> /dev/null; then
        menu
    else
        exit 0
    fi
}

# --- ANIMASI LOADING ---
duration=3
frames=("██10%" "█████35%" "█████████65%" "█████████████80%" "█████████████████████90%" "█████████████████████████100%")
num_frames=${#frames[@]}
num_iterations=$((duration))

Loading_Animasi() {
    for ((i = 0; i < num_iterations; i++)); do
        clear
        index=$((i % num_frames))
        color_code=$((31 + i % 7))
        echo ""
        echo ""
        echo ""
        echo -e "\e[1;${color_code}m ${frames[$index]}\e[0m"
        sleep 0.5
    done
}

function Loading_Succes() {
    clear
    echo -e  "\033[5;32mSucces\033[0m"
    sleep 2
    clear
}

# --- LIST MEMBER VLESS (FIX TANDA #&) ---
function Daftar_Member_Vless() {
    baris_panjang
    echo ""
    # Cek file config ada atau tidak
    if [[ ! -f "/etc/xray/config.json" ]]; then
        echo -e "${Red} File /etc/xray/config.json tidak ditemukan!${Xark}"
        return
    fi

    # PERBAIKAN UTAMA: Menggunakan #& untuk VLESS sesuai VPS Anda
    if grep -q "^#& " "/etc/xray/config.json"; then
        grep -E "^#& " "/etc/xray/config.json" | cut -d ' ' -f 2-3 | column -t | sort | uniq | lolcat
    else
        echo -e "${Red} Tidak ada user VLESS (tanda #&) ditemukan.${Xark}"
    fi
    echo ""
    baris_panjang
    echo ""
}

# --- CEK CONFIG & GENERATE LINK ---
function Cek_Config_Vless() {
    read -rp "Input Username : " user
    if [ -z "$user" ]; then
        echo -e "User Tidak Ditemukan!!!"
        read -n 1 -s -r -p "Enter Back To Menu"
        if command -v menu &> /dev/null; then menu; else exit 0; fi
    else
        # Cek apakah user ada di config VLESS (Cari #&)
        if ! grep -q "^#& $user" "/etc/xray/config.json"; then
             echo -e "${Red}User '$user' tidak ditemukan di daftar VLESS!${Xark}"
             read -n 1 -s -r -p "Enter Back To Menu"
             if command -v menu &> /dev/null; then menu; else exit 0; fi
             exit 0
        fi

        # Ambil Data
        CITY=$(cat /etc/xray/city 2>/dev/null)
        ISP=$(cat /etc/xray/isp 2>/dev/null)
        domain=$(cat /etc/xray/domain 2>/dev/null)

        # Path Limit & Quota VLESS
        if [[ -e /etc/kyt/limit/vless/ip/$user ]]; then
            iplimit=$(cat /etc/kyt/limit/vless/ip/$user)
        else
            iplimit="Unlimited"
        fi

        if [[ -e /etc/vless/$user ]]; then
            Quota1=$(cat /etc/vless/$user)
            Quota=$((Quota1 / 1024 / 1024 / 1024))
        else
            Quota="Unlimited"
        fi

        # Ambil UUID
        # Logic: Cari baris user (#& user), lalu ambil ID di blok json nya
        uuid=$(cat /etc/xray/config.json | grep -w "$user" -A 1 | grep "id" | cut -d '"' -f 4 | head -1)

        # Fallback jika uuid kosong
        if [[ -z "$uuid" ]]; then
            uuid=$(grep -E "^},{" "/etc/xray/config.json" | grep -i '"'"${user}"'"' | cut -d " " -f 2 | cut -d '"' -f 2 | uniq)
        fi

        # Ambil Expired (Cari #&)
        exp=$(grep -E "^#& " "/etc/xray/config.json" | grep -w "$user" | cut -d ' ' -f 3 | uniq)

        # --- GENERATE LINK VLESS (Standard) ---
        # VLESS WS TLS
        vlesslink1="vless://${uuid}@${domain}:443?path=/vless&security=tls&encryption=none&host=${domain}&type=ws&sni=${domain}#${user}"

        # VLESS WS NON TLS
        vlesslink2="vless://${uuid}@${domain}:80?path=/vless&security=none&encryption=none&host=${domain}&type=ws#${user}"

        # VLESS GRPC
        vlesslink3="vless://${uuid}@${domain}:443?mode=gun&security=tls&encryption=none&type=grpc&serviceName=vless-grpc&sni=${domain}#${user}"
    fi
}

function Details_Account() {
    baris_panjang
    echo -e "\033[37m VLESS XRAY INFO \033[0m"
    baris_panjang
    echo -e " Remarks        : ${user}"
    echo -e " Domain         : ${domain}"
    echo -e " User Quota     : ${Quota} GB"
    echo -e " User Ip        : ${iplimit} IP"
    echo -e " Port TLS       : 443"
    echo -e " Port WS        : 80, 8080"
    echo -e " UUID           : ${uuid}"
    echo -e " Location       : $CITY"
    echo -e " ISP            : $ISP"
    echo -e " Security       : auto"
    echo -e " Network        : ws/grpc"
    echo -e " Path           : /vless"
    echo -e " ServiceName    : vless-grpc"
    echo -e "\033[0m"
}

function Link_Json() {
    baris_panjang
    echo -e " Link TLS       : ${vlesslink1}"
    baris_panjang
    echo -e " Link WS        : ${vlesslink2}"
    baris_panjang
    echo -e " Link GRPC      : ${vlesslink3}"
    baris_panjang
    echo -e " OpenClash      : https://${domain}:81/vless-$user.txt"
    baris_panjang
}

function Details_Expiry() {
    echo -e "\033[33m Expiry         : $exp \033[0m"
    baris_panjang
}

# --- EKSEKUSI UTAMA ---
xdxl_Banner
echo "     "
Daftar_Member_Vless
echo "     "
Cek_Config_Vless
clear
Loading_Animasi
Loading_Succes
Details_Account
Link_Json
Details_Expiry
Sc_Credit
