---
- name: "Ensure {{ hostsfile_dest_dir }} exists"
  ansible.windows.win_file:
    path: "{{ hostsfile_dest_dir }}"
    state: directory

- name: Ensure hosts files are downloaded
  ansible.windows.win_get_url:
    url: "{{ hostsfile.url }}"
    dest: "{{ hostsfile_dest_dir }}/{{ hostsfile.filename }}"
  loop:
    - url: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
      filename: base
    - url: https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews/hosts
      filename: fakenews
    - url: https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling/hosts
      filename: gambling
    - url: https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/porn/hosts
      filename: porn
    - url: https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/social/hosts
      filename: social
    - url: https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts
      filename: all
  loop_control:
    loop_var: hostsfile
  register: download_result

- name: Ensure a copy of hosts files are created
  ansible.windows.win_copy:
    src: "{{ hostsfile_dest_dir }}/{{ filename }}"
    dest: "{{ hostsfile_dest_dir }}/{{ filename }}-clean"
    remote_src: true
  loop:
    - base
    - fakenews
    - gambling
    - porn
    - social
    - all
  loop_control:
    loop_var: filename
  when: "download_result.results | map(attribute='changed') | join(',') | regex_search('True')"

- name: Ensure the hosts file are sanitized
  community.windows.win_lineinfile:
    path: "{{ hostsfile_dest_dir }}/{{ filename }}-clean"
    regex: '(^ *#.*$)|^$'
    state: absent
  loop:
    - base
    - fakenews
    - gambling
    - porn
    - social
    - all
  loop_control:
    loop_var: filename
  when: "download_result.results | map(attribute='changed') | join(',') | regex_search('True')"

- name: Backup the default hosts file
  ansible.windows.win_command: hostctl backup --path "{{ hostsfile_dest_dir }}/"

# @action `hostctl` versions after v1.0.15 check for unique domains in a profile which makes
# it extremely slow to process large hosts files. Revisit after a future version provides a
# switch like that in version v1.0.3
# - name: Create hostctl profiles using the various hosts files
#   command: "hostctl add {{ filename }} --from {{ hostsfile_dest_dir }}/{{ filename }}-clean -q"
#   loop:
#     - base
#     - fakenews
#     - gambling
#     - porn
#     - social
#     - all
#  loop_control:
#    loop_var: filename

- name: Read the content of the hosts files # noqa 301
  ansible.windows.win_shell: type "{{ hostsfile_dest_dir }}/{{ filename }}-clean"
  loop:
    - base
    - fakenews
    - gambling
    - porn
    - social
    - all
  loop_control:
    loop_var: filename
  register: clean_files
  when: "download_result.results | map(attribute='changed') | join(',') | regex_search('True')"

- name: Setup SwitchHosts
  include_tasks: user-Windows.yml
  loop: "{{ user_configs }}"
  loop_control:
    label: "{{ user.username }}"
    loop_var: user
  when:
    - "download_result.results | map(attribute='changed') | join(',') | regex_search('True')"
    - (user.system is not defined) or ((user.system is defined) and (not user.system))
