---
- name: fish | Create necessary directories
  file:
    path: '{{ xdg_dir.config_home }}/fish/{{ item }}'
    state: directory
    mode: '755'
  loop:
    - completions
    - conf.d
    - functions
    - themes

- name: fish | Copy function and config files
  copy:
    src: 'fish/{{ item.src }}'
    dest: '{{ xdg_dir.config_home }}/fish/{{ item.dest }}'
    mode: '644'
  loop:
    - src: fish_greeting.fish
      dest: functions/
    - src: fish_user_key_bindings.fish
      dest: functions/
    - src: ghq_cd.fish
      dest: functions/
    - src: ghq_rm.fish
      dest: functions/

- name: fish | Copy config templates
  template:
    src: 'fish/{{ item.src }}.j2'
    dest: '{{ xdg_dir.config_home }}/fish/{{ item.dest }}.fish'
    mode: '644'
  loop:
    - src: aliases
      dest: conf.d/aliases
    - src: env
      dest: conf.d/env
    - src: config
      dest: config

- name: fish | Copy shell themes
  copy:
    src: 'fish/{{ item }}'
    dest: '{{ xdg_dir.config_home }}/fish/themes/{{ item }}'
    mode: '644'
  loop:
    - Onedark.theme
    - Nord.theme

# Overwrite theme? [y/N]
- name: fish | Set shell theme to '{{ theme }}'
  shell:
    cmd: fish_config theme save {{ theme | capitalize }}
    executable: /usr/bin/fish
    stdin: y
    stdin_add_newline: true
  changed_when: true

- name: fish | Download fzf_key_bindings.fish
  get_url:
    url: https://github.com/junegunn/fzf/raw/master/shell/key-bindings.fish
    dest: '{{ xdg_dir.config_home }}/fish/functions/fzf_key_bindings.fish'
    mode: '644'

- name: fish | Install fisher # noqa: risky-shell-pipe command-instead-of-module
  shell: /usr/bin/curl -sSL https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish | source && fisher install jorgebucaran/fisher
  args:
    executable: /usr/bin/fish
    creates: '{{ xdg_dir.config_home }}/fish/functions/fisher.fish'

- name: fish | Install replay.fish
  command: /usr/bin/fish -c 'fisher install jorgebucaran/replay.fish'
  args:
    creates: '{{ xdg_dir.config_home }}/fish/functions/replay.fish'
