@echo off
echo ========================================
echo FORCE GRADLE TO USE JAVA 11
echo ========================================
echo.

echo Current JAVA_HOME: %JAVA_HOME%
echo.

echo Setting JAVA_HOME to Java 11 for this session...
set "JAVA_HOME=C:\Program Files\Eclipse Adoptium\jdk-11.0.21.9-hotspot"
echo ✓ JAVA_HOME set to: %JAVA_HOME%

echo.
echo Setting PATH to prioritize Java 11...
set "PATH=%JAVA_HOME%\bin;%PATH%"
echo ✓ PATH updated

echo.
echo Verifying Java version...
java -version
echo.

echo ========================================
echo UPDATING GRADLE WRAPPER CONFIGURATION
echo ========================================
echo.

echo Updating gradle-wrapper.properties to use compatible Gradle version...
(
echo distributionBase=GRADLE_USER_HOME
echo distributionPath=wrapper/dists
echo zipStoreBase=GRADLE_USER_HOME
echo zipStorePath=wrapper/dists
echo distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
) > android\gradle\wrapper\gradle-wrapper.properties
echo ✓ gradle-wrapper.properties updated

echo.
echo Updating gradle.properties with Java 11 settings...
(
echo org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
echo android.useAndroidX=true
echo android.enableJetifier=true
echo org.gradle.parallel=true
echo org.gradle.daemon=true
echo org.gradle.configureondemand=true
echo org.gradle.java.home=%JAVA_HOME%
) > android\gradle.properties
echo ✓ gradle.properties updated

echo.
echo ========================================
echo CLEARING GRADLE CACHES
echo ========================================
echo.

echo Stopping Gradle daemons...
cd android
gradlew --stop
cd ..
echo ✓ Gradle daemons stopped

echo.
echo Clearing Gradle caches...
if exist "android\.gradle" (
    rmdir /s /q "android\.gradle"
    echo ✓ android\.gradle removed
)

if exist "%USERPROFILE%\.gradle" (
    rmdir /s /q "%USERPROFILE%\.gradle"
    echo ✓ Global Gradle cache removed
)

echo.
echo ========================================
echo TESTING GRADLE WITH JAVA 11
echo ========================================
echo.

echo Testing Gradle version with Java 11...
cd android
gradlew --version
cd ..

echo.
echo ========================================
echo READY TO BUILD
echo ========================================
echo.
echo Now try building your Flutter app:
echo flutter build apk --debug
echo.
echo If you still get Java version errors, restart your IDE and try again.
echo.
pause 