@echo off
chcp 437 >nul
title BIOS Programming Tool
setlocal enabledelayedexpansion
mode con: cols=80 lines=40

REM 检查管理员权限
>nul 2>&1 net session
if %errorlevel% neq 0 (
    echo Requesting administrator privileges...
    powershell -Command "Start-Process cmd -ArgumentList '/c','%~f0' -Verb RunAs"
    exit /b
)

cd /d "%~dp0"

echo ============================================
echo     BIOS Programming Tool
echo ============================================
echo.
echo Programming will start in 3 seconds...
echo Press Ctrl+C to cancel, or wait to continue...
echo.

REM 3秒倒计时
set "countdown=3"
:COUNTDOWN_START
echo Starting in %countdown%...
timeout /t 1 /nobreak >nul
if errorlevel 1 (
    REM 用户按了Ctrl+C，询问是否取消
    echo.
    set "cancel_choice="
    set /p "cancel_choice=Do you want to cancel programming? (Y/N): "
    
    if /i "!cancel_choice!"=="Y" (
        echo.
        echo Programming cancelled by user.
        timeout /t 2 >nul
        exit /b
    ) else (
        REM 用户选择不取消，继续倒计时
        echo.
        echo Continuing with programming...
        echo.
        set /a "countdown-=1"
        if !countdown! gtr 0 goto :COUNTDOWN_START
    )
) else (
    set /a "countdown-=1"
    if !countdown! gtr 0 goto :COUNTDOWN_START
)

cls
echo ============================================
echo     BIOS Programming in Progress
echo ============================================
echo.

REM 使用Powershell显示红色文字
powershell -Command "Write-Host '         ****************************************' -ForegroundColor Red"
powershell -Command "Write-Host '         *    WARNING: DO NOT POWER OFF!       *' -ForegroundColor Red"
powershell -Command "Write-Host '         *    DO NOT DISCONNECT ANY CABLES!    *' -ForegroundColor Red"
powershell -Command "Write-Host '         *    DO NOT INTERRUPT THIS PROCESS!   *' -ForegroundColor Red"
powershell -Command "Write-Host '         ****************************************' -ForegroundColor Red"
echo.

REM 开始刷写
echo         Starting programming...
echo.

REM 执行刷写命令，让刷写工具自带提示显示
AFUWINx64.exe SERAL302.bin /p /b /n /x /l /r 
echo.
echo         ********************************************
echo         *         Programming Ended               *
echo         ********************************************
echo.

REM 无论刷写结果如何，都显示菜单
echo ============================================
echo             SELECT ACTION
echo ============================================
echo.
echo Press 1: Restart the system (5 second delay)
echo Press 2: Shutdown the system (5 second delay)
echo Press any other key: Exit immediately
echo.
echo ============================================
echo.

REM 使用PowerShell读取按键
echo Please press a key: 

REM 调用PowerShell读取按键
for /f "delims=" %%k in ('powershell -Command "$key = [System.Console]::ReadKey($true).KeyChar; Write-Host $key"') do (
    set "pressed_key=%%k"
)

REM 检查按键
if "!pressed_key!"=="1" (
    echo.
    echo You selected: Restart the system
    echo.
    echo System will restart in 5 seconds...
    echo.
    
    REM 设置5秒后重启
    shutdown /r /f /t 5
    
    REM 显示5秒倒计时
    for /l %%i in (5,-1,1) do (
        echo Restarting in %%i...
        timeout /t 1 >nul
    )
    
    echo Restarting now...
    echo.
) else if "!pressed_key!"=="2" (
    echo.
    echo You selected: Shutdown the system
    echo.
    echo System will shutdown in 5 seconds...
    echo.
    
    REM 设置5秒后关机
    shutdown /s /f /t 5
    
    REM 显示5秒倒计时
    for /l %%i in (5,-1,1) do (
        echo Shutting down in %%i...
        timeout /t 1 >nul
    )
    
    echo Shutting down now...
    echo.
) else (
    REM 按了其他键，直接退出
    echo.
    echo Exiting...
    timeout /t 1 >nul
    exit
)