@echo off
setlocal enabledelayedexpansion
chcp 65001 >nul
cls

:: ---- 芯片参数配置（仅此处不同） ----
set CHIP=esp32
set BAUD=460800
set BOOT_ADDR=0x1000
:: ------------------------------------------

set ESPTOOL=%~dp0..\..\esptool.exe
set FW=firmware

echo ============================================
echo    AScript-ESP 固件烧录工具
echo    芯片: %CHIP%
echo ============================================
echo.

:: ---- 检查固件文件 ----
if not exist "%FW%\bootloader.bin" ( echo  [错误] 未找到 bootloader.bin！ & pause & exit /b )
if not exist "%FW%\partitions.bin" ( echo  [错误] 未找到 partitions.bin！ & pause & exit /b )
if not exist "%FW%\firmware.bin"   ( echo  [错误] 未找到 firmware.bin！   & pause & exit /b )
echo  [OK] 固件文件检查通过
echo.

:: ---- 扫描COM端口 ----
:refresh_ports
set PORT=
set PORT_COUNT=0
echo  正在扫描端口...
echo.

set IDX=0
for /f "delims=" %%a in ('powershell.exe -Command "[Console]::OutputEncoding=[System.Text.Encoding]::UTF8; Get-WmiObject Win32_PnPEntity | Where-Object { $_.Name -match 'COM\d+' } | ForEach-Object { $_.Name }" 2^>nul') do (
    set /a IDX+=1
    set /a PORT_COUNT+=1
    set "DEV_!IDX!=%%a"
    for /f "tokens=2 delims=()" %%c in ("%%a") do ( set "COM_!IDX!=%%c" )
    echo    [!IDX!] %%a
)

echo.

if !PORT_COUNT! equ 0 (
    echo  未找到COM端口，请连接设备。
    echo  按任意键重新扫描...
    pause >nul
    cls
    goto refresh_ports
)

if !PORT_COUNT! equ 1 (
    set PORT=!COM_1!
    echo  自动选择: !COM_1!
    goto port_selected
)

:ask_choice
set /p CHOICE=  请输入序号 [1-!PORT_COUNT!]（输入R刷新）:
if /i "!CHOICE!"=="R" (
    cls
    goto refresh_ports
)
if "!CHOICE!"=="" goto ask_choice
call set PORT=%%COM_!CHOICE!%%

:port_selected
if "!PORT!"=="" ( echo  [错误] 未选择端口！ & pause & exit /b )

echo.
echo ============================================
echo    端口: !PORT!   波特率: %BAUD%
echo ============================================
echo.

:: ---- 第一次尝试：自动下载模式 ----
echo  [1/4] 连接设备...
echo  [2/4] 自动进入下载模式...
echo  [3/4] 正在烧录（esptool 原生进度）...
echo  --------------------------------------------

"%ESPTOOL%" --chip %CHIP% -p !PORT! -b %BAUD% --before=default_reset --after=watchdog-reset write_flash ^
%BOOT_ADDR% "%FW%\bootloader.bin" 0x8000 "%FW%\partitions.bin" 0x10000 "%FW%\firmware.bin"

echo  --------------------------------------------
if !errorlevel! equ 0 goto flash_ok

:: ---- 第二次尝试：手动下载模式 ----
echo.
echo  自动模式失败，请手动操作：
echo.
echo    1. 按住 BOOT 键
echo    2. 点击 EN/RST 键
echo    3. 松开 BOOT 键
echo.
echo  准备好后按任意键继续...
pause >nul
echo.
echo  [3/4] 手动模式烧录中...
echo  --------------------------------------------

"%ESPTOOL%" --chip %CHIP% -p !PORT! -b %BAUD% --before=no_reset --after=watchdog-reset write_flash ^
%BOOT_ADDR% "%FW%\bootloader.bin" 0x8000 "%FW%\partitions.bin" 0x10000 "%FW%\firmware.bin"

echo  --------------------------------------------
if !errorlevel! equ 0 goto flash_ok

echo.
echo  ============================================
echo    烧录失败！请检查接线后重试。
echo  ============================================
echo.
pause
exit /b

:flash_ok
echo.
echo  [4/4] 重启设备...
"%ESPTOOL%" -p !PORT! run >nul 2>&1
echo.
echo  ============================================
echo    烧录成功！
echo  ============================================
echo  如果未检测到AS开头的蓝牙设备，请按复位按钮。
echo.
set /p AGAIN=  按 Y 继续烧录下一台, 任意其它键退出:
if /i "!AGAIN!"=="Y" (
    cls
    goto refresh_ports
)
