---
- name: "Konfigurer Stargate: Atlantis Hyprland Desktop"
  hosts: localhost
  connection: local
  become: true # De fleste opgaver kræver root

  # Definer variabler for at gøre playbook'en genbrugelig
  vars:
    vault_username: !vault |
              $ANSIBLE_VAULT;1.1;AES256
              61396435643466353531373139343364613266303130323533306461316333313631363130373465
              6632663034376535663566353661366132656539326566320a323734313061303131623962303339
              66333664613636653362316133666561353039623961393930346661613064306234326161343030
              3335353266393066320a336233366636633566363566613866613263303362656464353166346237
              3236

    username: "{{ vault_username }}" # ERSTAT med det brugernavn du oprettede i Arch-installationen.
                                     # Hvis du ikke har brug for at kryptere dit  brugernavn kan du skrive det direkte i her.
    aur_helper: "yay" # Vi bruger yay til AUR pakker

  tasks:
    - name: "SYSTEM :: Opdater systemet"
      community.general.pacman:
        update_cache: true
        upgrade: true

    - name: "SYSTEM :: Installer AUR helper ({{ aur_helper }})"
      block:
        - name: "Installer base-devel og git"
          community.general.pacman:
            name:
              - base-devel
              - git
            state: present

        - name: "Klon, byg og installer {{ aur_helper }}"
          become: true
          become_user: "{{ username }}"
          ansible.builtin.shell: |
            cd /tmp
            git clone https://aur.archlinux.org/{{ aur_helper }}.git
            cd {{ aur_helper }}
            makepkg -si --noconfirm
          args:
            creates: "/usr/bin/{{ aur_helper }}"

    - name: "PAKKER :: Installer alle nødvendige pakker fra Pacman og AUR"
      become: true
      become_user: "{{ username }}"
      kewlfft.aur.aur:
        use: "{{ aur_helper }}"
        name:
          # Hyprland og Wayland
          - hyprland
          - xdg-desktop-portal-hyprland
          - kitty # Terminal
          - waybar # Statuslinje
          - wofi # App starter
          - mako # Notifikationer
          - swaybg # Baggrund
          - polkit-kde-agent
          - qt5-wayland
          - qt6-wayland
          # Skrifttyper & Tema
          - ttf-font-awesome
          - ttf-jetbrains-mono-nerd
          - noto-fonts
          # Værktøjer
          - grim # Screenshot værktøj
          - slurp # Område-vælger til screenshots
          - network-manager-applet # Til Waybar tray
          - hyprpaper
          - sddm # Login Manager
          - firefox # Browser
          - flatpak
          - vscodium # Udviklings editor
          - papyrus-icon-theme # Ikon tema til wofi drun
          - thunar
          - thunar-volman
          - tumbler
          - gvfs
          - ranger
          - less
          - sof-firmware
          - pavucontrol
          - pipewire
          - pipewire-pulse
          - pipewire-audio
          - pipewire-alsa
          - wireplumber
          - alsa-utils
          - neofetch
          - swayidle
          - swaylock-effects
          - iptables-nft
          - ufw # Firewall
        state: present

    - name: "KEYBINDING SCRIPT :: Opret .local/bin mappe til brugerdefinerede scripts"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.file:
        path: "/home/{{ username }}/.local/bin"
        state: directory
        mode: '0755'

    - name: "BRUGER :: Opret konfigurationsmapper"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.file:
        path: "/home/{{ username }}/.config/{{ item }}"
        state: directory
        mode: '0755'
      loop:
        - hypr
        - kitty
        - waybar
        - mako
        - wofi

    - name: "DOTFILES :: Kopier skabeloner til brugerens .config"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.template:
        src: "templates/{{ item.src }}"
        dest: "/home/{{ username }}/.config/{{ item.dest }}"
        mode: '0644'
      loop:
        - { src: 'hyprland.conf.j2', dest: 'hypr/hyprland.conf' }
        - { src: 'kitty.conf.j2', dest: 'kitty/kitty.conf' }
        - { src: 'waybar-config.j2', dest: 'waybar/config' }
        - { src: 'waybar-style.css.j2', dest: 'waybar/style.css' }
        - { src: 'wofi-config.j2', dest: 'wofi/config' }
        - { src: 'wofi-style.css.j2', dest: 'wofi/style.css' }
        - { src: 'wofi-config-network.j2', dest: 'wofi/config-network'}

    - name: "SCRIPTS :: Kopier og gør brugerdefinerede scripts eksekverbare"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.template:
        src: "templates/{{ item.src }}"
        dest: "/home/{{ username }}/.local/bin/{{ item.dest }}"
        mode: '0755'  # Bemærk: '755' gør dem eksekverbare
      loop:
        - { src: 'hypr-keys.sh.j2', dest: 'hypr-keys.sh' }
        - { src: 'launch-wofi.sh.j2', dest: 'launch_wofi.sh' }
        - { src: 'launch-wifi-menu.sh.j2', dest: 'launch-wifi-menu.sh' }
        - { src: 'lock-screen.sh.j2', dest: 'lock-screen.sh' }

    - name: "TEMA :: Installer Atlantis skrifttype"
      ansible.builtin.copy:
        src: "files/fonts/"
        dest: "/home/{{ username }}/.local/share/fonts/"
        mode: '0644'
      notify: Opdater font cache

    - name: "BRUGER :: Opret Pictures-mappen"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.file:
        path: "/home/{{ username }}/Pictures"
        state: directory
        mode: '0755'

    - name: "TEMA :: Kopier Atlantis baggrundsbillede"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.copy:
        src: "files/wallpapers/atlantis_wallpaper.jpg"
        dest: "/home/{{ username }}/Pictures/atlantis-wallpaper.jpg"
        mode: '0644'

    - name: "TEMA :: Kopier Atlantis baggrundsbillede"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.copy:
        src: "files/wallpapers/atlantis_spaceship.jpg"
        dest: "/home/{{ username }}/Pictures/atlantis-spaceship.jpg"
        mode: '0644'

    - name: "TEMA :: Kopier hyprpaper konfiguration"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.template:
        src: "templates/hyprpaper.conf.j2"
        dest: "/home/{{ username }}/.config/hypr/hyprpaper.conf"
        mode: '0644'

    - name: "FIREWALL :: Konfigurer UFW's standardpolitikker"
      tags: firewall
      block:
        - name: "Sæt standard for indgående trafik til 'deny'"
          community.general.ufw:
            policy: deny
            direction: incoming

        - name: "Sæt standard for udgående trafik til 'allow'"
          community.general.ufw:
            policy: allow
            direction: outgoing

# Hvis du har brug for ssh så tilføj følgende regel og tilføj OpenSSH til installations listen
#    - name: "FIREWALL :: Tillad SSH-forbindelser (vigtigt for fjernadgang)"
#      community.general.ufw:
#        rule: allow
#        name: OpenSSH # Bruger UFW's applikationsprofil for SSH
#      tags: firewall

    - name: "FLATPAK :: Installer LibreOffice"
      become: true
      become_user: "{{ username }}"
      community.general.flatpak:
        name: org.libreoffice.LibreOffice
        state: present

    - name: "SHELL :: Tilføj neofetch til .bashrc for automatisk kørsel"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.blockinfile:
        path: "/home/{{ username }}/.bashrc"
        create: true # Opretter .bashrc filen, hvis den ikke findes
        owner: "{{ username }}"
        group: "{{ username }}"
        mode: '0644'
        marker: "# {mark} ANSIBLE MANAGED BLOCK - NEOFETCH"
        block: |
          # Kør neofetch når en ny interaktiv terminal åbnes
          # Tjekker om 'neofetch' kommandoen eksisterer først
          if command -v neofetch &> /dev/null
          then
              neofetch
          fi

    - name: "FIREWALL :: Aktiver og start UFW service"
      community.general.ufw:
        state: enabled # Dette aktiverer firewallen, så den starter ved boot
      tags: firewall

    - name: "SERVICES :: Aktiver NetworkManager"
      ansible.builtin.systemd:
        name: NetworkManager
        enabled: true
        state: started

    - name: "SERVICES :: Aktiver Display Manager (SDDM)"
      ansible.builtin.systemd:
        name: sddm.service
        enabled: true

    - name: "STYLING :: Opret GTK3 konfigurationsmappe"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.file:
        path: "/home/{{ username }}/.config/gtk-3.0"
        state: directory
        mode: '0755'

    - name: "STYLING :: Sæt GTK-tema og Papirus som standard ikon-tema"
      become: true
      become_user: "{{ username }}"
      ansible.builtin.copy:
        dest: "/home/{{ username }}/.config/gtk-3.0/settings.ini"
        content: |
          [Settings]
          gtk-theme-name=Adwaita
          gtk-icon-theme-name=Papirus
          gtk-font-name=JetBrains Mono 11
          gtk-cursor-theme-name=Adwaita
        mode: '0644'

  # Handlers køres kun hvis en opgave 'notifierer' dem
  handlers:
    - name: Opdater font cache
      ansible.builtin.command: fc-cache -fv
      become: true
      become_user: "{{ username }}"
      changed_when: false
