---
- name: mpv | Create config directory
  file:
    path: '{{ xdg_dir.config_home }}/mpv/{{ item }}'
    state: directory
    mode: '755'
  loop: [fonts, shaders, scripts, script-opts]

- name: mpv | Copy config
  template:
    src: mpv/mpv.j2
    dest: '{{ xdg_dir.config_home }}/mpv/mpv.conf'
    mode: '644'

# Busybox has 'unzip' so use that
# 'zipinfo' is required for 'unarchive' module, but it is only available in 'unzip' package
- name: mpv | Install Anime4K shaders
  tags: [mpv-extend, mpv-anime4k]
  block:
    - name: mpv | Download Anime4K zip file
      get_url:
        url: 'https://github.com/bloc97/Anime4K/releases/download/v{{ anime4k_shaders_version }}/Anime4K_v4.0.zip'
        dest: /tmp/Anime4K.zip
        mode: '644'

    - name: mpv | Unzip Anime4K shaders # noqa: command-instead-of-module
      command: unzip /tmp/Anime4K.zip -d ~/.config/mpv/shaders
      args:
        creates: '{{ xdg_dir.config_home }}/mpv/shaders/Anime4K_Clamp_Highlights.glsl'

- name: mpv | Install and configure modernx OSC tweak
  tags: [mpv-extend, mpv-mordenx]
  block:
    - name: mpv | Download modernx OSC script
      get_url:
        url: https://github.com/cyl0/ModernX/raw/main/modernx.lua
        dest: '{{ xdg_dir.config_home }}/mpv/scripts/modernx.lua'
        mode: '644'

    - name: mpv | Download Material Design Iconic font zip file
      get_url:
        url: 'https://github.com/zavoloklom/material-design-iconic-font/releases/download/{{ material_iconic_font_version }}/material-design-iconic-font.zip'
        dest: /tmp/material-design-iconic-font.zip
        mode: '644'

    - name: mpv | Unzip Material Design Iconic font # noqa: command-instead-of-module
      command: unzip -j /tmp/material-design-iconic-font.zip fonts/Material-Design-Iconic-Font.ttf -d ~/.config/mpv/fonts
      args:
        creates: '{{ xdg_dir.config_home }}/mpv/fonts/Material-Design-Iconic-Font.ttf'

    - name: mpv | Disable default OSC and border in mpv config
      lineinfile:
        path: '{{ xdg_dir.config_home }}/mpv/mpv.conf'
        state: present
        regexp: '^{{ item | regex_replace("no$", "") }}'
        line: '{{ item }}'
      loop:
        - osc=no
        - border=no

    - name: mpv | Copy OSC config for mordenx script
      template:
        src: mpv/osc.j2
        dest: '{{ xdg_dir.config_home }}/mpv/script-opts/osc.conf'
        mode: '644'

- name: mpv | Install and configure thumbfast script
  tags: [mpv-extend, mpv-thumbfast]
  block:
    - name: mpv | Install thumbfast script
      get_url:
        url: https://github.com/po5/thumbfast/raw/master/thumbfast.lua
        dest: '{{ xdg_dir.config_home }}/mpv/scripts/thumbfast.lua'
        mode: '644'

    - name: mpv | Copy thumbfast configuration
      copy:
        src: mpv/thumbfast.conf
        dest: '{{ xdg_dir.config_home }}/mpv/script-opts/thumbfast.conf'
        mode: '644'

# - name: mpv | Install and configure autosubsync-mpv script
#   tags: [mpv-extend, mpv-subsync]
#   block:
#     - name: mpv | Clone autosubsync-mpv repository
#       git:
#         depth: 1
#         repo: https://github.com/joaquintorres/autosubsync-mpv.git
#         dest: '{{ xdg_dir.config_home }}/mpv/scripts/autosubsync'
#
#     - name: mpv | Copy autosubsync configuration
#       template:
#         src: mpv/autosubsync.j2
#         dest: '{{ xdg_dir.config_home }}/mpv/script-opts/autosubsync.conf'
#         mode: '644'

- name: mpv | Install mpv-gif script
  get_url:
    url: https://github.com/tyalie/mpv-gif-generator/raw/main/mpv-gif.lua
    dest: '{{ xdg_dir.config_home }}/mpv/scripts/mpv-gif.lua'
    mode: '644'
  tags: [mpv-extend, mpv-gif]

- name: mpv | Install mpv-webm script
  get_url:
    url: https://github.com/ekisu/mpv-webm/releases/download/latest/webm.lua
    dest: '{{ xdg_dir.config_home }}/mpv/scripts/webm.lua'
    mode: '644'
  tags: [mpv-extend, mpv-webm]

- name: mpv | Copy input config
  copy:
    src: mpv/input.conf
    dest: '{{ xdg_dir.config_home }}/mpv/input.conf'
    mode: '644'
  tags: mpv-extend
