@echo off
title PlayTab Uninstall
color 0C

echo.
echo  ============================================
echo   PlayTab Uninstall
echo  ============================================
echo.
echo  Before running this script, you MUST first
echo  allow uninstall from within the app:
echo.
echo    1. Open PlayTab on the tablet
echo    2. Go to Admin Panel (PIN: 1234)
echo    3. Scroll to Kiosk Mode section
echo    4. Tap "Enter Maintenance Mode"
echo    5. Tap "Allow Uninstall"
echo.
echo  Also make sure:
echo    - Tablet is connected via USB
echo    - USB debugging is ON
echo.
pause

:: Check if adb is available
set "ADB="
where adb >nul 2>nul
if %errorlevel% equ 0 (
    set "ADB=adb"
    goto :adb_found
)
if exist "%~dp0platform-tools\adb.exe" (
    set "ADB=%~dp0platform-tools\adb.exe"
    goto :adb_found
)

:: ADB not found — download it
echo.
echo  ADB not found. Downloading Android Platform Tools...
echo.

where curl >nul 2>nul
if %errorlevel% neq 0 (
    echo  ERROR: curl not found. Cannot download automatically.
    echo.
    echo  Please download Android Platform Tools manually from:
    echo  https://developer.android.com/tools/releases/platform-tools
    echo.
    echo  Extract the zip next to this script so the folder is:
    echo    playtab-uninstall.bat
    echo    platform-tools\adb.exe
    echo.
    pause
    exit /b 1
)

curl -L -o "%~dp0platform-tools.zip" "https://dl.google.com/android/repository/platform-tools-latest-windows.zip"
if not exist "%~dp0platform-tools.zip" (
    echo  ERROR: Download failed.
    pause
    exit /b 1
)

echo  Extracting...
powershell -Command "Expand-Archive -Force '%~dp0platform-tools.zip' '%~dp0'"
del "%~dp0platform-tools.zip" >nul 2>nul

if exist "%~dp0platform-tools\adb.exe" (
    set "ADB=%~dp0platform-tools\adb.exe"
    echo  ADB installed to %~dp0platform-tools\
    echo.
) else (
    echo  ERROR: ADB still not found after download.
    pause
    exit /b 1
)

:adb_found

echo.
echo  Waiting for device...
%ADB% wait-for-device

for /f "tokens=*" %%i in ('%ADB% shell getprop ro.product.model') do set MODEL=%%i
echo.
echo  Device found! (%MODEL%)

:: Check if PlayTab is installed
%ADB% shell pm list packages | findstr "com.symflo.playtab" >nul 2>nul
if %errorlevel% neq 0 (
    echo.
    echo  PlayTab is not installed on this device. Nothing to do.
    echo.
    pause
    exit /b 0
)

:: Check if device owner is still active
%ADB% shell dumpsys device_policy | findstr /C:"com.symflo.playtab" >nul 2>nul
if %errorlevel% equ 0 (
    echo.
    echo  ERROR: Device owner is still active.
    echo.
    echo  You must tap "Allow Uninstall" in the app first.
    echo  See the steps above, then run this script again.
    echo.
    pause
    exit /b 1
)

echo.
echo  Device owner already cleared. Uninstalling...
%ADB% uninstall com.symflo.playtab >nul 2>nul
if %errorlevel% neq 0 (
    echo.
    echo  ERROR: Uninstall failed. Try uninstalling from Settings ^> Apps.
    echo.
    pause
    exit /b 1
)

echo.
echo  ============================================
echo   PlayTab has been uninstalled.
echo  ============================================
echo.
echo  The tablet is back to normal.
echo.
pause
