{ pkgs, lib, ... }:

let
  mod = "Mod4";
in {
  # Dependencies
  home.packages = with pkgs; [
    i3lock-color # TODO: add as dependency of ./lock
    (pkgs.callPackage ./lock { })
    playerctl # pactl comes with PulseAudio
    scrot # Screnshot keybindings
    xclip # Copying screenshots to clipboard
  ];

  xsession.windowManager.i3 = {
    enable = true;
    config = {
      defaultWorkspace = "workspace number 1"; # defaults to 10 for some reason
      modifier = mod;
      # i3-gaps-specific configuration
      gaps = {
        inner = 12;
        smartGaps = true;
      };
      startup = [
        {
          command = "(systemctl --user restart polybar &) && lock";
          always = true;
          notification = false;
        }
      ];
      keybindings = lib.mkOptionDefault {
        "${mod}+Return" = "exec kitty";
        "${mod}+Shift+e" = "exec i3-msg exit"; # bypass default session exit confirmation menu
        "Mod1+Q" = "exec /etc/profiles/per-user/elnu/bin/rofi-power";

        # Audio controls
        "XF86AudioRaiseVolume" = "exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +10%";
        "XF86AudioLowerVolume" = "exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ -10%";
        "XF86AudioMute" = "exec --no-startup-id pactl set-sink-mute @DEFAULT_SINK@ toggle";
        "XF86AudioMicMute" = "exec --no-startup-id pactl set-source-mute @DEFAULT_SOURCE@ toggle";

        # Media player controls
        "XF86AudioPlay" = "exec playerctl play-pause";
        "XF86AudioNext" = "exec playerctl next";
        "XF86AudioPrev" = "exec playerctl previous";

        # Multi-screen screenshots with scrot
        "Print" = "exec scrot 'screenshot_%Y%m%d_%H%M%S.png' -e 'mkdir -p ~/Pictures/screenshots && mv $f ~/Pictures/screenshots && xclip -selection clipboard -t image/png -i ~/Pictures/screenshots/`ls -1 -t ~/Pictures/screenshots | head -1`"; # All screens

        # OSX-like area selection screenshots
        "Shift+Print" = "exec scrot -s 'screenshot_%Y%m%d_%H%M%S.png' -e 'mkdir -p ~/Pictures/screenshots && mv $f ~/Pictures/screenshots && xclip -selection clipboard -t image/png -i ~/Pictures/screenshots/`ls -1 -t ~/Pictures/screenshots | head -1`'"; # Area selection
              };
      keycodebindings = {
        "133" = "--release exec rofi -show run -theme";
      };
      window.commands = [
        {
          command = "border pixel 0";
          criteria = { class = "^.*"; };
        }
      ];
      bars = [];
    };
  };
}
