---

- name: install chocolatey packages
  win_chocolatey:
    name: neovim
    state: latest

- set_fact:
    vim_config_dir: '{{ansible_env.LOCALAPPDATA}}\nvim'

- name: clone config repo
  win_git:
    repo: git@code.infektor.net:config/nvim.git
    dest: '{{vim_config_dir}}'
    branch: main

# - TODO: neovim set repo email
#   win_git_config:
# - TODO: neovim install pip packages
#   win_pip:
#     name: '{{neovim_pip_packages}}'
#     state: latest

- name: create nvim start menu shortcut
  win_shortcut:
    src: '{{ansible_env.ChocolateyToolsLocation}}/neovim/nvim-win64/bin/nvim-qt.exe'
    dest: '{{ansible_env.ProgramData}}/Microsoft/Windows/Start Menu/Programs/nvim-qt.lnk'
    icon: '{{ansible_env.ChocolateyToolsLocation}}/neovim/nvim-win64/bin/nvim-qt.exe,0'
    directory: '{{ansible_env.USERPROFILE}}'

- name: check for config repo tasks.yaml
  win_stat:
    path: '{{vim_config_dir}}/tasks.yaml'
  register: config_repo_tasks

# TODO: this doesn't work for non localhost setups
# probably need to copy the tasks.yaml and plugins.yaml to the controller in a
# temporary directory then include them
- when: config_repo_tasks.stat.exists
  fetch:
    src: '{{vim_config_dir}}/tasks.yaml'
    dest: vim_config_tasks.yaml
    flat: true
  changed_when: false

- when: config_repo_tasks.stat.exists
  include_tasks: vim_config_tasks.yaml

- name: remove fetched tasks
  file:
    state: absent
    path: vim_config_tasks.yaml
  changed_when: false
  delegate_to: localhost

- when: ansible_os_family != "Windows" and
        plugin_dir is defined and plugins is defined
  include_tasks: 'Unix-plugins.yaml'
- when: ansible_os_family == "Windows" and
        plugin_dir is defined and plugins is defined
  include_tasks: 'Windows-plugins.yaml'