#!/bin/bash

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # 无颜色

# 定义脚本 URL 数组
script_urls=(
  "https://www.mgrsc.top/http/linux/debian-misc-script/setup_init.sh"
  "https://www.mgrsc.top/http/linux/debian-misc-script/setup_bashrc.sh"
  "https://www.mgrsc.top/http/linux/debian-misc-script/setup_cache_clean.sh"
  "https://www.mgrsc.top/http/linux/debian-misc-script/setup_vimrc.sh"
)

# 定义脚本描述
script_descriptions=(
  "系统初始化配置脚本"
  "Bash配置优化脚本"
  "系统缓存清理脚本"
  "Vim配置优化脚本"
)

# 打印带颜色的消息
print_message() {
  local color=$1
  local message=$2
  echo -e "${color}${message}${NC}"
}

# 显示进度条
show_progress() {
  local width=50
  local percent=$1
  local filled=$((width * percent / 100))
  local empty=$((width - filled))
  
  printf "["
  printf "%${filled}s" | tr ' ' '#'
  printf "%${empty}s" | tr ' ' ' '
  printf "] %d%%\r" $percent
}

# 下载脚本函数
download_script() {
  script_url="$1"
  script_name=$(basename "$script_url")
  print_message "$BLUE" "\n📥 正在下载 ${script_name}..."
  
  # 使用curl下载并显示进度
  if curl -s -o "${script_name}" "${script_url}"; then
    print_message "$GREEN" "✅ 下载 ${script_name} 完成。"
    chmod +x "${script_name}"  # 赋予执行权限
    return 0
  else
    print_message "$RED" "❌ 下载 ${script_name} 失败！"
    return 1
  fi
}

# 执行脚本函数
execute_script() {
  script_name="$1"
  print_message "$YELLOW" "\n🚀 正在执行 ${script_name}..."
  print_message "$PURPLE" "----------------------------------------------"
  
  ./"${script_name}"
  local result=$?
  
  print_message "$PURPLE" "----------------------------------------------"
  if [ $result -eq 0 ]; then
    print_message "$GREEN" "✅ 执行 ${script_name} 完成。"
    return 0
  else
    print_message "$RED" "❌ 执行 ${script_name} 失败！"
    return 1
  fi
}

# 显示菜单
show_menu() {
  print_message "$CYAN" "\n📋 可用脚本列表:"
  print_message "$PURPLE" "----------------------------------------------"
  for i in "${!script_urls[@]}"; do
    local index=$((i + 1))
    local script_name=$(basename "${script_urls[$i]}")
    print_message "$CYAN" "  ${index}. ${script_name} - ${script_descriptions[$i]}"
  done
  print_message "$PURPLE" "----------------------------------------------"
}

# 用户自定义执行
custom_execution() {
  show_menu
  
  print_message "$YELLOW" "\n选择操作方式:"
  print_message "$CYAN" "  • 输入脚本编号，用空格分隔 (例如: 1 3 5)"
  print_message "$CYAN" "  • 输入 'A' 执行所有脚本"
  print_message "$CYAN" "  • 输入 'Q' 退出程序"
  
  read -p "$(echo -e ${YELLOW}"请输入您的选择: "${NC})" choices
  
  if [[ "$choices" == "Q" || "$choices" == "q" ]]; then
    print_message "$GREEN" "👋 退出程序。"
    exit 0
  elif [[ "$choices" == "A" || "$choices" == "a" ]]; then
    choices="1 2 3 4 5"
    print_message "$GREEN" "✅ 将按默认顺序执行所有脚本。"
  fi
  
  # 检查输入是否有效
  valid_choices=()
  for choice in $choices; do
    if [[ "$choice" =~ ^[1-5]$ ]]; then
      valid_choices+=("$choice")
    else
      print_message "$RED" "❌ 无效的输入: $choice。请输入 1-5 之间的数字。"
      return 1
    fi
  done
  
  if [ ${#valid_choices[@]} -eq 0 ]; then
    print_message "$RED" "❌ 没有有效的脚本选择。"
    return 1
  fi
  
  # 确认执行
  print_message "$YELLOW" "\n将执行以下脚本:"
  for choice in "${valid_choices[@]}"; do
    local index=$((choice - 1))
    print_message "$CYAN" "  ${choice}. $(basename "${script_urls[$index]}") - ${script_descriptions[$index]}"
  done
  
  read -p "$(echo -e ${YELLOW}"确认执行? (y/n): "${NC})" confirm
  if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
    print_message "$GREEN" "已取消执行。"
    return 0
  fi
  
  # 执行选择的脚本
  local total=${#valid_choices[@]}
  local current=0
  
  for choice in "${valid_choices[@]}"; do
    ((current++))
    local progress=$((current * 100 / total))
    
    print_message "$CYAN" "\n🔄 总进度: $current/$total"
    show_progress $progress
    
    index=$((choice - 1))
    script_url="${script_urls[$index]}"
    
    if download_script "${script_url}"; then
      script_name=$(basename "${script_url}")
      execute_script "${script_name}"
      rm "${script_name}" # 执行完成后删除脚本
    fi
  done
  
  print_message "$GREEN" "\n✨ 所有选择的脚本执行完毕！"
}

# 检查必要的命令
check_dependencies() {
  local deps=("curl" "basename")
  local missing=()
  
  for dep in "${deps[@]}"; do
    if ! command -v "$dep" &> /dev/null; then
      missing+=("$dep")
    fi
  done
  
  if [ ${#missing[@]} -ne 0 ]; then
    print_message "$RED" "❌ 缺少必要的命令: ${missing[*]}"
    print_message "$YELLOW" "请使用包管理器安装它们后再运行此脚本。"
    exit 1
  fi
}

# 主程序
main() {
  print_message "$GREEN" "🚀 Debian 系统初始化脚本 🚀"
  check_dependencies
  custom_execution
}

# 运行主程序
main
