#!/bin/bash

# Color Variables
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
magenta='\033[0;35m'
cyan='\033[0;36m'
plain='\033[0m'

cur_dir=$(pwd)

clear
echo -e "${cyan}######################################################${plain}"
echo -e "${magenta}    ██████╗  █████╗ ██████╗ ████████╗ ██████╗ ██████╗   ${plain}"
echo -e "${magenta}    ██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗  ${plain}"
echo -e "${magenta}    ██████╔╝███████║██████╔╝   ██║   ██║   ██║██████╔╝  ${plain}"
echo -e "${magenta}    ██╔══██╗██╔══██║██╔═══╝    ██║   ██║   ██║██╔══██╗  ${plain}"
echo -e "${magenta}    ██║  ██║██║  ██║██║        ██║   ╚██████╔╝██║  ██║  ${plain}"
echo -e "${magenta}    ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝        ╚═╝    ╚═════╝ ╚═╝  ╚═╝  ${plain}"                                               
echo -e "${yellow}          3x-ui OFFLINE INSTALLER BY PARSICO.ORG        ${plain}"
echo -e "${cyan}######################################################  ${plain}"
echo ""

# 1. Get Initial Info
echo -e "${blue}Please enter panel credentials:${plain}"
read -p "Enter Panel Port (Default 2053): " admin_port
admin_port=${admin_port:-2053}
read -p "Enter Panel Username (Default admin): " admin_user
admin_user=${admin_user:-admin}
read -p "Enter Panel Password (Default admin): " admin_pass
admin_pass=${admin_pass:-admin}

# 2. Installation Function
do_install() {
    echo -e "${yellow}Extracting and configuring files...${plain}"
    
    if [[ ! -f "./x-ui-linux-amd64.tar.gz" ]]; then
        echo -e "${red}Error: x-ui-linux-amd64.tar.gz not found!${plain}"
        exit 1
    fi

    rm -rf /usr/local/x-ui/
    tar -zxf ./x-ui-linux-amd64.tar.gz -C /usr/local/
    
    cd /usr/local/x-ui/
    chmod +x x-ui bin/xray-linux-amd64
    
    if [[ -f "x-ui.service.debian" ]]; then
        cp -f x-ui.service.debian /etc/systemd/system/x-ui.service
    else
        cp -f x-ui.service /etc/systemd/system/x-ui.service
    fi
    
    systemctl daemon-reload
    systemctl enable x-ui > /dev/null 2>&1
    systemctl restart x-ui > /dev/null 2>&1

    echo -e "${yellow}Applying security settings...${plain}"
    sleep 2
    
    ./x-ui setting -reset > /dev/null 2>&1
    ./x-ui setting -username ${admin_user} -password ${admin_pass} -port ${admin_port} > /dev/null 2>&1
    
    systemctl restart x-ui > /dev/null 2>&1
}

setup_menu() {
    if [[ -f "$cur_dir/x-ui.sh" ]]; then
        cp -f "$cur_dir/x-ui.sh" /usr/bin/x-ui
        chmod +x /usr/bin/x-ui
    fi
}

# Run
do_install
setup_menu

# --- 4. Get Final Info for Summary ---
server_ip=$(curl -s ipv4.icanhazip.com || wget -qO- ipv4.icanhazip.com)
final_web_path=$(/usr/local/x-ui/x-ui setting -show | grep "Web Base Path" | awk '{print $NF}')

if [ -z "$final_web_path" ] || [ "$final_web_path" == "Path" ]; then
    final_web_path="/"
fi

# --- Final Summary Table ---
clear
echo -e "${cyan}######################################################${plain}"
echo -e "${green}         INSTALLATION COMPLETED SUCCESSFULLY!${plain}"
echo -e "${cyan}######################################################${plain}"
echo ""
echo -e "${blue}Server IP:   ${plain} ${server_ip}"
echo -e "${blue}Port:        ${plain} ${admin_port}"
echo -e "${blue}Username:    ${plain} ${admin_user}"
echo -e "${blue}Password:    ${plain} ${admin_pass}"
echo -e "${blue}Web Path:    ${plain} ${final_web_path}"
echo ""
echo -e "${magenta}Access URL:  ${plain} http://${server_ip}:${admin_port}${final_web_path}"
echo ""
echo -e "${cyan}######################################################${plain}"
echo -e "${yellow}Type ${green}x-ui${yellow} to manage the panel anytime.${plain}"
echo -e "${cyan}######################################################${plain}"