{ config, pkgs, ... }:
let
  wallpaper = "${../../wallpapers/astronaut.png}";
in {
  services.swayidle = {
    enable = true;
    timeouts = [
      {
        timeout = 300;
        command = "${pkgs.swaylock}/bin/swaylock -f -i ${wallpaper}";
      }
      {
        timeout = 600;
        command = "${pkgs.niri}/bin/niri msg action power-off-monitors";
      }
    ];
    events = {
      before-sleep = "${pkgs.swaylock}/bin/swaylock -f -i ${wallpaper}";
    };
  };

  home.sessionVariables = {
    XDG_CURRENT_DESKTOP                 = "niri";
    XDG_SESSION_DESKTOP                 = "niri";
    QT_QPA_PLATFORM                     = "wayland;xcb";
    QT_AUTO_SCREEN_SCALE_FACTOR         = "1";
    QT_ENABLE_HIGHDPI_SCALING           = "1";
    QT_SCALE_FACTOR_ROUNDING_POLICY     = "RoundPreferFloor";
    QT_WAYLAND_DISABLE_WINDOWDECORATION = "1";
    XDG_MENU_PREFIX                     = "plasma-";
  };

  home.packages = with pkgs; [
    # Niri session tools
    fuzzel
    swaylock
    awww
    rofimoji
    libnotify

    # Screenshot stack
    grim
    slurp
    satty
    wl-clipboard
  ];

  home.file.".local/bin/screenshot" = {
    executable = true;
    text = ''
      #!/usr/bin/env bash
      MODE="''${1:-region}"
      SAVE_DIR="${config.xdg.userDirs.pictures}/screenshots"
      mkdir -p "$SAVE_DIR"
      FILENAME="$SAVE_DIR/$(date '+%Y%m%d-%H%M%S').png"

      case "$MODE" in
        region)
          grim -g "$(slurp)" -t ppm - | satty --filename - --output-filename "$FILENAME" --copy-command wl-copy --early-exit
          ;;
        screen)
          grim -t ppm - | satty --filename - --output-filename "$FILENAME" --copy-command wl-copy --early-exit
          ;;
        monitor)
          grim -o "$(niri msg focused-output | grep 'Output' | awk '{print $2}')" -t ppm - \
            | satty --filename - --output-filename "$FILENAME" --copy-command wl-copy --early-exit
          ;;
      esac
    '';
  };

  home.file.".local/bin/power-menu" = {
    executable = true;
    text = ''
      #!/usr/bin/env bash
      CHOICE=$(printf "Lock\nSuspend\nReboot\nShutdown\nLogout" | fuzzel --dmenu --prompt "Power: " --lines 5)
      case "$CHOICE" in
        Lock)     swaylock -f -i ${wallpaper} ;;
        Suspend)  systemctl suspend ;;
        Reboot)   systemctl reboot ;;
        Shutdown) systemctl poweroff ;;
        Logout)   niri msg action quit ;;
      esac
    '';
  };

  home.file.".local/bin/awww-init" = {
    executable = true;
    text = ''
      #!/usr/bin/env bash
      awww-daemon &
      awww img "${wallpaper}" --transition-type none
    '';
  };

  xdg.configFile."quickshell".source =
    config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/repos/nixos/quickshell";

  xdg.configFile."niri/config.kdl".text = ''
    input {
      keyboard {
        xkb {
          layout "us,br"
        }
      }
      touchpad {
        tap
        natural-scroll
      }
    }

    layout {
      gaps 6
      border {
        width 2
        active-color "#fabd2f"
        inactive-color "#504945"
      }

      focus-ring {
        width 2
        active-color "#fabd2f"
        inactive-color "transparent"
      }
    }

    prefer-no-csd

    spawn-at-startup "kded6"
    spawn-at-startup "quickshell"
    spawn-at-startup "bash" "-c" "$HOME/.local/bin/awww-init"

    screenshot-path "${config.xdg.userDirs.pictures}/screenshots/%Y-%m-%d %H-%M-%S.png"

    binds {
      // Applications
      Mod+Return  { spawn "kitty"; }
      Mod+D       { spawn "fuzzel"; }
      Mod+Q       { close-window; }
      Mod+Period  { spawn "rofimoji" "--selector" "fuzzel" "--clipboarder" "wl-copy"; }
      Mod+Shift+P { spawn "bash" "-c" "$HOME/.local/bin/power-menu"; }

      // Focus
      Mod+Left  { focus-column-left; }
      Mod+Right { focus-column-right; }
      Mod+Up    { focus-window-up; }
      Mod+Down  { focus-window-down; }

      // Move windows
      Mod+Shift+Left  { move-column-left; }
      Mod+Shift+Right { move-column-right; }
      Mod+Shift+Up    { move-window-up; }
      Mod+Shift+Down  { move-window-down; }

      // Workspaces
      Mod+1 { focus-workspace 1; }
      Mod+2 { focus-workspace 2; }
      Mod+3 { focus-workspace 3; }
      Mod+4 { focus-workspace 4; }
      Mod+5 { focus-workspace 5; }
      Mod+Shift+1 { move-window-to-workspace 1; }
      Mod+Shift+2 { move-window-to-workspace 2; }
      Mod+Shift+3 { move-window-to-workspace 3; }
      Mod+Shift+4 { move-window-to-workspace 4; }
      Mod+Shift+5 { move-window-to-workspace 5; }

      // System
      Mod+Shift+E { quit; }
      Mod+L       { spawn "swaylock" "-i" "${wallpaper}"; }

      // Volume
      XF86AudioRaiseVolume { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "5%+"; }
      XF86AudioLowerVolume { spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "5%-"; }
      XF86AudioMute        { spawn "wpctl" "set-mute" "@DEFAULT_AUDIO_SINK@" "toggle"; }

      // Toggle quickshell bar (zen mode)
      Mod+Z { spawn "quickshell" "ipc" "call" "zen" "toggle"; }

      // Input layout
      Mod+Space { switch-layout "next"; }

      // Screenshots
      Mod+Shift+S          { spawn "bash" "-c" "$HOME/.local/bin/screenshot region"; }
      Mod+Shift+Alt+S      { spawn "bash" "-c" "$HOME/.local/bin/screenshot screen"; }
      Mod+Shift+Ctrl+S     { spawn "bash" "-c" "$HOME/.local/bin/screenshot monitor"; }
    }
  '';
}
