@echo off
cls
setlocal enableextensions enabledelayedexpansion
title Reset AnyDesk
timeout.exe /t 2 > $null
:: Color codes
set "COL_RED=[31m"
set "COL_GREEN=[32m"
set "COL_YELLOW=[33m"
set "COL_ORANGE=[33m"  :: Orange isn't standard in cmd, so using yellow for messages labeled Orange
set "COL_RESET=[0m"

:: Ensure running as administrator
reg query HKEY_USERS\S-1-5-19 >NUL || (
  echo %COL_RED%Please run this script as Administrator.%COL_RESET%
  pause >NUL
  exit /b
)

:: Start message
echo.
echo %COL_YELLOW%Checking if AnyDesk service is installed...%COL_RESET%
echo.
timeout.exe /t 2 > $null
:: Check for service existence
sc query AnyDesk >NUL 2>&1
if %ERRORLEVEL% EQU 0 (
  echo %COL_YELLOW%AnyDesk service is detected installed.%COL_RESET%
  echo.
  timeout.exe /t 2 > $null
  echo %COL_ORANGE%Stopping AnyDesk service...%COL_RESET%
  timeout.exe /t 2 > $null
  call :stop_any
) else (
  echo %COL_ORANGE%AnyDesk service not found. Assuming portable executable mode.%COL_RESET%
  timeout.exe /t 2 > $null
)
echo.

:: Step: Clean configuration files
echo %COL_ORANGE%Deleting AnyDesk configuration files...%COL_RESET%
echo.
timeout.exe /t 2 > $null
del /f /q "%ALLUSERSPROFILE%\AnyDesk\service.conf" 2>nul
del /f /q "%APPDATA%\AnyDesk\service.conf" 2>nul
echo %COL_GREEN%service.conf files removed.%COL_RESET%
echo.
timeout.exe /t 2 > $null

:: Backup user settings
echo %COL_ORANGE%Backing up user settings and thumbnails...%COL_RESET%
echo.
timeout.exe /t 2 > $null
copy /y "%APPDATA%\AnyDesk\user.conf" "%temp%\" >nul 2>&1
xcopy /c /e /h /r /y /i /k "%APPDATA%\AnyDesk\thumbnails" "%temp%\thumbnails" >nul 2>&1
echo %COL_GREEN%User settings backed up.%COL_RESET%
echo.
timeout.exe /t 2 > $null

:: Remove entire user and system directories
echo %COL_ORANGE%Removing AnyDesk application data...%COL_RESET%
echo.
timeout.exe /t 2 > $null
rd /s /q "%ALLUSERSPROFILE%\AnyDesk" 2>nul
rd /s /q "%APPDATA%\AnyDesk" 2>nul
echo %COL_GREEN%AnyDesk data removed.%COL_RESET%
echo.
timeout.exe /t 2 > $null

:: Restore after license presence
echo %COL_YELLOW%Checking for license configuration...%COL_RESET%
echo.
timeout.exe /t 2 > $null
if exist "%ALLUSERSPROFILE%\AnyDesk\system.conf" (
    findstr /r /c:"ad.anynet.id=" "%ALLUSERSPROFILE%\AnyDesk\system.conf" >nul
    if %ERRORLEVEL% EQU 0 (
        echo %COL_ORANGE%License identifier found. Restoring user settings...%COL_RESET%
        echo.
		timeout.exe /t 2 > $null
        call :stop_any 2>nul
        move /y "%temp%\user.conf" "%APPDATA%\AnyDesk\user.conf" >nul 2>&1
        xcopy /c /e /h /r /y /i /k "%temp%\thumbnails" "%APPDATA%\AnyDesk\thumbnails" >nul 2>&1
        rd /s /q "%temp%\thumbnails" 2>nul
        echo %COL_GREEN%User settings restored successfully.%COL_RESET%
        echo.
		timeout.exe /t 2 > $null
    ) else (
        echo %COL_YELLOW%No license entry found in system.conf.%COL_RESET%
        echo.
		timeout.exe /t 2 > $null
    )
)

:: Restart AnyDesk (if installed or portable)
echo %COL_ORANGE%Starting AnyDesk...%COL_RESET%
echo.
timeout.exe /t 2 > $null
call :start_any
echo %COL_GREEN%AnyDesk start process initiated.%COL_RESET%
echo.
timeout.exe /t 2 > $null

:: Cleanup temp user.conf if still present
if exist "%temp%\user.conf" del /f /q "%temp%\user.conf" 2>nul
echo %COL_GREEN%Temporary files cleaned up.%COL_RESET%
echo.
timeout.exe /t 2 > $null

echo %COL_GREEN%All done!%COL_RESET%
echo.
timeout.exe /t 3 > $null
exit /b

:stop_any
sc stop AnyDesk >nul 2>&1
sc stop AnyDesk >nul 2>&1
if %ERRORLEVEL% neq 1062 (
  goto :stop_any
)
taskkill /f /im "AnyDesk.exe" >nul 2>&1
exit /b

:start_any
sc query AnyDesk >nul 2>&1
if %ERRORLEVEL% EQU 0 (
  sc start AnyDesk >nul 2>&1
)

set "AnyDesk1=%SystemDrive%\Program Files (x86)\AnyDesk\AnyDesk.exe"
set "AnyDesk2=%SystemDrive%\Program Files\AnyDesk\AnyDesk.exe"
if exist "%AnyDesk1%" start "" "%AnyDesk1%"
if exist "%AnyDesk2%" start "" "%AnyDesk2%"
exit /b
