- name: Create limited GameUser
  ansible.windows.win_user:
    name: "{{ game_user }}"
    password: "{{ game_password }}"
    groups: "{{ game_default_groups }}"
    account_disabled: false
    password_never_expires: true
    user_cannot_change_password: true
    state: present

- name: Configure auto-login for GameUser
  ansible.windows.win_regedit:
    path: HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
    name: "{{ item.name }}"
    data: "{{ item.data }}"
    type: string
  loop:
    - { name: "AutoAdminLogon", data: "1" }
    - { name: "DefaultUserName", data: "{{ game_user }}" }
    - { name: "DefaultPassword", data: "{{ game_password }}" }
    - { name: "DefaultDomainName", data: "{{ ansible_hostname }}" }

- name: Create launcher script directory
  ansible.windows.win_file:
    path: C:\GameShell
    state: directory

- name: Deploy launcher.bat
  ansible.windows.win_copy:
    dest: C:\GameShell\launcher.bat
    content: "{{ lookup('file', 'scripts/launcher.bat') }}"

- name: Set High Performance Power Plan
  ansible.windows.win_command: powercfg -setactive SCHEME_MIN

- name: Disable auto suspend (sleep) for plugged in
  ansible.windows.win_command: powercfg -change -standby-timeout-ac 0

- name: Disable auto suspend (sleep) for battery
  ansible.windows.win_command: powercfg -change -standby-timeout-dc 0

- name: Disable Windows Defender Real-Time Protection
  ansible.windows.win_shell: Set-MpPreference -DisableRealtimeMonitoring $true

- name: Remove unnecessary startup items (HKLM)
  ansible.windows.win_regedit:
    path: HKLM:\Software\Microsoft\Windows\CurrentVersion\Run
    name: SecurityHealth
    state: absent

- name: Remove startup folder shortcuts (user)
  ansible.windows.win_file:
    path: "{{ item }}"
    state: absent
  with_fileglob:
    - "{{ ansible_env.APPDATA }}\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\*.lnk"

- name: Remove startup folder shortcuts (all users)
  ansible.windows.win_file:
    path: "{{ item }}"
    state: absent
  with_fileglob:
    - "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\*.lnk"

- name: Disable Xbox Game Bar and DVR
  ansible.windows.win_regedit:
    path: HKCU:\Software\Microsoft\Windows\CurrentVersion\GameDVR
    name: AppCaptureEnabled
    data: 0
    type: dword
    state: present

- name: Disable GameDVR_Enabled
  ansible.windows.win_regedit:
    path: HKCU:\System\GameConfigStore
    name: GameDVR_Enabled
    data: 0
    type: dword
    state: present

- name: Disable GameDVR_FSEBehaviorMode
  ansible.windows.win_regedit:
    path: HKCU:\System\GameConfigStore
    name: GameDVR_FSEBehaviorMode
    data: 2
    type: dword
    state: present

- name: Disable GameDVR_HonorUserFSEBehaviorMode
  ansible.windows.win_regedit:
    path: HKCU:\System\GameConfigStore
    name: GameDVR_HonorUserFSEBehaviorMode
    data: 1
    type: dword
    state: present

- name: Disable Windows notifications
  ansible.windows.win_regedit:
    path: HKCU:\Software\Microsoft\Windows\CurrentVersion\PushNotifications
    name: ToastEnabled
    data: 0
    type: dword
    state: present

- name: Disable background services
  vars:
    services_to_disable:
      - WSearch
      - SysMain
      - DiagTrack
      - XblAuthManager
      - XblGameSave
      - XboxGipSvc
      - Fax
      - MapsBroker
      - RetailDemo
      - RemoteRegistry
      - WMPNetworkSvc
      - edgeupdate
      - WpnService
      - BITS
      - WerSvc
      - Spooler
      - SCardSvr
  ansible.windows.win_service:
    name: "{{ item }}"
    start_mode: disabled
    state: stopped
  loop: "{{ services_to_disable }}"

- name: Get all services
  ansible.windows.win_service_info:
  register: win_services

- name: Disable scheduled telemetry tasks
  vars:
    scheduled_tasks_to_disable:
      - "\\Microsoft\\Windows\\Application Experience\\ProgramDataUpdater"
      - "\\Microsoft\\Windows\\Customer Experience Improvement Program\\Consolidator"
      - "\\Microsoft\\Windows\\Customer Experience Improvement Program\\KernelCeipTask"
      - "\\Microsoft\\Windows\\Feedback\\Siuf\\DmClient"
      - "\\Microsoft\\Windows\\Windows Error Reporting\\QueueReporting"
  ansible.windows.win_shell: |
    schtasks /Change /TN "{{ item }}" /Disable
  loop: "{{ scheduled_tasks_to_disable }}"
  register: telemetry_task_result
  failed_when: "{{ telemetry_task_result.rc != 0 and ('ERROR: The system cannot find the file specified.' not in telemetry_task_result.stderr) }}"

- name: Disable Windows Search feature
  ansible.windows.win_shell: DISM /Online /Disable-Feature /FeatureName:SearchEngine-Client-Package /NoRestart
  register: disable_search_result
  failed_when: >
    disable_search_result.rc != 0 and
    ('The specified package is not valid Windows package' not in disable_search_result.stderr) and
    ('is already disabled' not in disable_search_result.stdout)

- name: Disable Windows Media Player feature
  ansible.windows.win_shell: DISM /Online /Disable-Feature /FeatureName:WindowsMediaPlayer /NoRestart
  register: disable_wmp_result
  failed_when: >
    disable_wmp_result.rc != 0 and
    ('The specified package is not valid Windows package' not in disable_wmp_result.stderr) and
    ('is already disabled' not in disable_wmp_result.stdout)

- name: Disable Print to PDF
  ansible.windows.win_shell: DISM /Online /Disable-Feature /FeatureName:Printing-PrintToPDFServices-Features /NoRestart
  register: disable_print_pdf_result
  failed_when: >
    disable_print_pdf_result.rc != 0 and
    ('The specified package is not valid Windows package' not in disable_print_pdf_result.stderr) and
    ('is already disabled' not in disable_print_pdf_result.stdout)

- name: Reminder - NVIDIA Control Panel Tweaks (manual)
  ansible.builtin.debug:
    msg: |
      Please manually set the following in NVIDIA Control Panel:
      - Power Management: Prefer Maximum Performance
      - Low Latency Mode: Ultra
      - Vertical Sync: Off or Fast Sync (depending on monitor)

# - name: Set custom shell to launcher.bat
#   ansible.windows.win_regedit:
#     path: HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
#     name: Shell
#     data: C:\GameShell\launcher.bat
#     type: string
