---
- name: Ensure sway config directories exist
  tags: [dotfiles, dotfiles:desktop, sway]
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    owner: "{{ username }}"
    group: "{{ user_group }}"
    mode: "0755"
  loop:
    - "{{ user_home }}/.config/alacritty"
    - "{{ user_home }}/.config/dunst"
    - "{{ user_home }}/.config/flameshot"
    - "{{ user_home }}/.config/rofi"
    - "{{ user_home }}/.config/sway"
    - "{{ user_home }}/.config/waybar"
    - "{{ user_home }}/.config/wlogout"
  when:
    - (desktop_environment | default('minimal')) == 'minimal'
    - "'sway' in (desktop_sessions_enabled | default([]))"

- name: Install allowed emptty Wayland sessions
  tags: [packages, services, emptty, sway]
  ansible.builtin.copy:
    src: sway.desktop
    dest: /etc/emptty/wayland-sessions/sway.desktop
    owner: root
    group: root
    mode: "0644"
  when:
    - (desktop_environment | default('minimal')) == 'minimal'
    - "'sway' in (desktop_sessions_enabled | default([]))"

- name: Install Sway session launcher in /usr/local/bin
  tags: [packages, services, sway]
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/../dotfiles/desktop/.local/bin/start-sway"
    dest: /usr/local/bin/start-sway
    owner: root
    group: root
    mode: "0755"
  when:
    - (desktop_environment | default('minimal')) == 'minimal'
    - "'sway' in (desktop_sessions_enabled | default([]))"

- name: Ensure /etc/xdg/xdg-desktop-portal exists
  tags: [packages, services, sway, portal]
  ansible.builtin.file:
    path: /etc/xdg/xdg-desktop-portal
    state: directory
    owner: root
    group: root
    mode: "0755"
  when:
    - (desktop_environment | default('minimal')) == 'minimal'
    - "'sway' in (desktop_sessions_enabled | default([]))"

- name: Configure xdg-desktop-portal backend preferences for sway
  tags: [packages, services, sway, portal]
  ansible.builtin.copy:
    src: sway-portals.conf
    dest: /etc/xdg/xdg-desktop-portal/sway-portals.conf
    owner: root
    group: root
    mode: "0644"
  when:
    - (desktop_environment | default('minimal')) == 'minimal'
    - "'sway' in (desktop_sessions_enabled | default([]))"

- name: Copy sway desktop dotfiles
  tags: [dotfiles, dotfiles:desktop, sway]
  ansible.builtin.copy:
    src: "{{ playbook_dir }}/../dotfiles/desktop/{{ item.src }}"
    dest: "{{ user_home }}/{{ item.dest }}"
    owner: "{{ username }}"
    group: "{{ user_group }}"
    mode: "{{ item.mode }}"
  loop: "{{ desktop_sway_dotfiles | default([]) }}"
  loop_control:
    label: "{{ item.dest }}"
  when:
    - (desktop_environment | default('minimal')) == 'minimal'
    - "'sway' in (desktop_sessions_enabled | default([]))"
