@echo off
REM === Configuração Arduino + VSCode + Wokwi (Windows) ===
REM === Usa PowerShell para automação no run.bat ===

mkdir .vscode 2>nul
mkdir bin 2>nul
mkdir libs 2>nul

REM --- Baixa e instala arduino-cli ---
cd bin
curl -L -o arduino-cli.zip https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Windows_64bit.zip
tar -xf arduino-cli.zip
del arduino-cli.zip
del LICENSE.txt
cd ..

REM --- Cria run.bat com automação via PowerShell ---
echo @echo off > .vscode\run.bat
echo echo Compiling... >> .vscode\run.bat
echo .\bin\arduino-cli.exe compile --fqbn arduino:avr:uno code --libraries .\libraries\ --build-path .\build --clean >> .vscode\run.bat
echo. >> .vscode\run.bat
echo powershell -Command "Add-Type -AssemblyName System.Windows.Forms; Start-Sleep -Milliseconds 300; [Windows.Forms.SendKeys]::SendWait('^+p'); Start-Sleep -Milliseconds 500; [Windows.Forms.SendKeys]::SendWait('Wokwi: Start Simulation{ENTER}')" >> .vscode\run.bat

REM --- tasks.json ---
echo { > .vscode\tasks.json
echo     "version": "2.0.0", >> .vscode\tasks.json
echo     "tasks": [ >> .vscode\tasks.json
echo         { >> .vscode\tasks.json
echo             "label": "Run", >> .vscode\tasks.json
echo             "type": "shell", >> .vscode\tasks.json
echo             "command": "${workspaceFolder}\\.vscode\\run.bat", >> .vscode\tasks.json
echo             "group": { >> .vscode\tasks.json
echo                 "kind": "build", >> .vscode\tasks.json
echo                 "isDefault": true >> .vscode\tasks.json
echo             } >> .vscode\tasks.json
echo         } >> .vscode\tasks.json
echo     ] >> .vscode\tasks.json
echo } >> .vscode\tasks.json

REM --- Outros arquivos ---
echo build/ > .gitignore

echo { > diagram.json
echo     "version": 1, >> diagram.json
echo     "author": "Anonymous maker", >> diagram.json
echo     "editor": "wokwi", >> diagram.json
echo     "parts": [ { "type": "wokwi-arduino-uno", "id": "uno", "top": -28.2, "left": -10.2, "attrs": {} } ], >> diagram.json
echo     "connections": [], >> diagram.json
echo     "dependencies": {} >> diagram.json
echo } >> diagram.json

echo [wokwi] > wokwi.toml
echo version = 1 >> wokwi.toml
echo firmware = "build/code.ino.hex" >> wokwi.toml
echo elf = "build/code.ino.elf" >> wokwi.toml

REM --- Instalação do core AVR ---
bin\arduino-cli.exe core install arduino:avr
bin\arduino-cli.exe sketch new code

REM --- Limpeza ---
del README.md 2>nul
rmdir /s /q .git 2>nul

echo.
del install-linux.sh 2>nul
del install-windows.bat 2>nul
pause