---
# Android Studio (and SDK?). Copied and adjusted from Flutter playbook.
# https://wiki.debian.org/AndroidStudio
# https://developer.android.com/studio/install

## Facts ##

# Studio download URLs:
#   https://developer.android.com/studio/archive
# Current version:
#   https://redirector.gvt1.com/edgedl/android/studio/ide-zips/2024.2.2.14/android-studio-2024.2.2.14-linux.tar.gz
# Command-line tools is all Flutter actually needs:
#   https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip

- name: Workstation | Linux | Software | Android | Facts [1/3]
  set_fact:
    android_url: "https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip"
    android_download_file: "{{ user_user.home }}/Downloads/android-cmdline-tools.tar.xz"
    android_sdk_location: "{{ user_user.home }}/SDKs/Android/Sdk"

- name: Workstation | Linux | Software | Android | Facts [2/3]
  set_fact:
    android_bin_location: "{{ android_sdk_location }}/platform-tools"
    android_sdk_cmdline_temp: "{{ android_sdk_location }}/ansible"
    android_sdk_cmdline_final: "{{ android_sdk_location }}/cmdline-tools/latest/bin"

- name: Workstation | Linux | Software | Android | Facts [3/3]
  set_fact:
    android_sdkmanager_temp: "{{ android_sdk_cmdline_temp }}/cmdline-tools/bin/sdkmanager"
    android_sdkmanager_final: "{{ android_sdk_cmdline_final }}/sdkmanager"

## Checks ##

- name: Workstation | Linux | Software | Android | Check SDK Manager Exists [1/2]
  stat:
    path: "{{ android_sdkmanager_temp }}"
  register: android_sdkmanager_temp_stat
  when: coding == true

- name: Workstation | Linux | Software | Android | Check SDK Manager Exists [2/2]
  stat:
    path: "{{ android_sdkmanager_final }}"
  register: android_sdkmanager_final_stat
  when: coding == true

- name: Workstation | Linux | Software | Android | Check Download Exists
  stat:
    path: "{{ android_download_file }}"
  register: android_download_stat
  when: coding == true

## Packages ##

# https://docs.flutter.dev/get-started/install/linux/android
- name: Workstation | Linux | Software | Android | Dependencies [Install]
  package:
    name:
      - default-jdk
      - libc6
      - libncurses5
      - libstdc++6
      - lib32z1
      - libbz2-1.0
    state: present
  when: coding == true

- name: Workstation | Linux | Software | Android | Dependencies [Remove]
  package:
    name:
      - sdkmanager
    state: absent
  when: coding == true

## Install SDK ##

- name: Workstation | Linux | Software | Android | SDK
  block:

  - name: Workstation | Linux | Software | Android | Download Commandline Tools
    get_url:
      url: "{{ android_url }}"
      dest: "{{ android_download_file }}"
      owner: "{{ user }}"
      group: "{{ user }}"
      mode: '0664'
    when: not android_download_stat.stat.exists

  - name: Workstation | Linux | Software | Android | Create Folder
    file:
      path: "{{ android_sdk_cmdline_temp }}"
      state: directory
      owner: "{{ user }}"
      group: "{{ user }}"
      mode: '0755'

  - name: Workstation | Linux | Software | Android | Extract Tools
    unarchive:
      src: "{{ android_download_file }}"
      dest: "{{ android_sdk_cmdline_temp }}"
      owner: "{{ user }}"
      group: "{{ user }}"

  become_user: "{{ user }}"
  when: coding == true and not android_sdkmanager_temp_stat.stat.exists

- name: Workstation | Linux | Software | Android | Delete Archive
  file:
    path: "{{ android_download_file }}"
    state: absent

## Configure Modules ##

# This can only be run once, otherwise cmdline-tools creates latest-* folders.
- name: Workstation | Linux | Software | Android | Install Consistent Modules
  shell: "yes | {{ android_sdkmanager_temp }} --install '{{ item }}' --sdk_root={{ android_sdk_location }}"
  loop:
    # Current
    - cmdline-tools;latest
    - platform-tools
    - emulator
  become_user: "{{ user }}"
  when: coding == true and not android_sdkmanager_final_stat.stat.exists

# These are safe to run multiple times, and uses the new `latest` version.
- name: Workstation | Linux | Software | Android | Install Modules
  shell: "yes | {{ android_sdkmanager_final }} --install '{{ item }}' --sdk_root={{ android_sdk_location }}"
  loop:
    # 34
    - build-tools;34.0.0
    - platforms;android-34
    - sources;android-34
    # 35
    - build-tools;35.0.0
    - build-tools;35.0.1
    - platforms;android-35
    - sources;android-35
    # Images
    #- system-images;android-35;default;x86_64
    #- system-images;android-35;aosp_atd;x86_64
    - system-images;android-35;google_apis_playstore;x86_64
  become_user: "{{ user }}"
  when: coding == true

- name: Workstation | Linux | Software | Android | Inform Flutter
  shell: "{{ flutter }} config --android-sdk={{ android_sdk_location }}"
  become_user: "{{ user }}"
  when: coding == true

- name: Workstation | Linux | Software | Android | Licenses Agreements [1/2]
  shell: "yes | {{ android_sdkmanager_final }} --licenses --sdk_root={{ android_sdk_location }}"
  become_user: "{{ user }}"
  when: coding == true

- name: Workstation | Linux | Software | Android | License Agreements [2/2]
  shell: "yes | {{ flutter }} doctor --android-licenses"
  become_user: "{{ user }}"
  when: coding == true

- name: Workstation | Linux | Software | Android | Refresh Flutter Doctor Report
  shell: "{{ item }}"
  loop: "{{ flutter_report_commands }}"
  become_user: "{{ user }}"
  when: coding == true

## Configure Environment ##

- name: Workstation | Linux | Software | Android | Modify PATH (.bashrc)
  blockinfile:
    path: "{{ item }}/.bashrc"
    block: |
      export PATH="$PATH:{{ android_bin_location }}:{{ android_sdk_cmdline_final }}"
    marker: '# {mark} MANAGED BY ANSIBLE | Android'
    state: present
    create: yes
    backup: yes
  loop:
    - "{{ user_root.home }}"
    - "{{ user_user.home }}"
  ignore_errors: yes
  when: coding == true and user_root.home != "" and user_user.home != ""

- name: Workstation | Linux | Software | Android | Modify PATH (.zshrc)
  blockinfile:
    path: "{{ item }}/.zshrc"
    block: |
      export PATH="$PATH:{{ android_bin_location }}:{{ android_sdk_cmdline_final }}"
    marker: '# {mark} MANAGED BY ANSIBLE | Android'
    state: present
    create: yes
    backup: yes
  loop:
    - "{{ user_root.home }}"
    - "{{ user_user.home }}"
  ignore_errors: yes
  when: coding == true and user_root.home != "" and user_user.home != ""

## Uninstall SDK ##

- name: Workstation | Linux | Software | Android | Remove SDK
  file:
    path: "{{ android_sdk_location }}"
    state: absent
  when: not coding == true

## User Tools ##
#   Only needed from repo if not a development device,
#   otherwise better versions are in the SDK.

- name: Workstation | Linux | Software | Android | System Tools [Install]
  package:
    name:
      - fastboot
      - adb
    state: present
  when: not coding == true

- name: Workstation | Linux | Software | Android | System Tools [Remove]
  package:
    name:
      - fastboot
      - adb
    state: absent
  when: coding == true