{
  config,
  nix-config,
  pkgs,
  ...
}:
with pkgs.lib.nix-rice;
let
  strPalette = palette.toRGBHex pkgs.rice.colorPalette;
in
{

  programs = {
    niri = {
      settings = {
        # hotkey-overlay.skip-at-startup = true;
        screenshot-path = "~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png";
        prefer-no-csd = true;

        spawn-at-startup = [
          {
            command = [
              "clipse"
              "-listen"
            ];
          }
          {
            command = [
              "systemctl"
              "--user"
              "restart"
              "xwayland-satellite.service"
            ];
          }
          { command = [ "wpaperd" "--daemon" ]; }
          { command = [ "/home/emenel/.local/bin/niri-delayed-launch-at-startup" ]; }
        ];

        workspaces = {
          "main" = { };
          "scratchpad" = { };
        };

        input = {
          keyboard.xkb = {
            layout = "us";
            options = "compose:ralt";
          };

          touchpad = {
            natural-scroll = true;
            scroll-method = "two-finger";
            dwt = false;
            tap = false;
            tap-button-map = "left-right-middle";
            click-method = "clickfinger";
          };

          mouse = {
            natural-scroll = true;
            accel-speed = 0.1;
            accel-profile = "adaptive";
          };

          workspace-auto-back-and-forth = true;
        };

        outputs = {
          "eDP-1" = {
            mode = {
              width = 2560;
              height = 1600;
            };
            background-color = strPalette.normal.background;
            scale = 1;
            variable-refresh-rate = true;
          };

          "HDMI-A-1" = {
            background-color = strPalette.normal.background;
            scale = 1;
          };

          "PNP(BNQ) BenQ PD2725U N2N00414019" = {
            mode = {
              width = 3840;
              height = 2160;
            };
            background-color = strPalette.normal.background;
            scale = 1;
          };
        };

        environment = {
          DISPLAY = ":0";
          QT_QPA_PLATFORM = "xcb";
          XDG_CURRENT_DESKTOP = "niri";
          XDG_SESSION_TYPE = "wayland";
          MOZ_ENABLE_WAYLAND = "1";
        };

        cursor = {
          theme = "Bibata-Modern-Classic";
          # size = 32;
          hide-after-inactive-ms = 30000;
        };

        animations.slowdown = 1.0;

        layout = {
          gaps = 10;
          center-focused-column = "never";
          always-center-single-column = true;

          preset-column-widths = [
            { proportion = 0.33333; }
            { proportion = 0.5; }
            { proportion = 0.66667; }
          ];

          preset-window-heights = [
            { proportion = 1. / 3.; }
            { proportion = 1. / 2.; }
            { proportion = 2. / 3.; }
          ];

          default-column-width = {
            proportion = 0.5;
          };

          focus-ring = {
            enable = true;
            active.color = with pkgs.rice.colorPalette.normal.border;
              "rgba(${toString r} ${toString g} ${toString b} ${toString a})";
            width = pkgs.rice.border-width;
          };

          border = {
            enable = true;
            inactive = {
              gradient = {
                from = "#333333";
                to = "#111111";
                angle = 180;
                in' = "srgb-linear";
              };
            };
            active.color = "#000";
            width = pkgs.rice.border-width + 1;
          };

          shadow = {
            enable = true;
          };
        };

        window-rules = [
          # global window rule for shared config
          {
            geometry-corner-radius = {
              top-left = pkgs.rice.corner-radius;
              top-right = pkgs.rice.corner-radius;
              bottom-left = pkgs.rice.corner-radius;
              bottom-right = pkgs.rice.corner-radius;
            };
            clip-to-geometry = true;
          }

          {
            matches = [
              { app-id = "Calculator"; }
              { app-id = "org.gnome.Calculator$"; }
              { app-id = "pavucontrol$"; }
              { app-id = "Color Picker"; }
              { app-id = "Volume Control"; }
              { app-id = "processing-core-PApplet"; }
              { app-id = "file-roller$"; }
              { app-id = "Preferences$"; }
              { app-id = "Open File$"; }
              { app-id = "Save File$"; }
              { app-id = "Open Folder$"; }
            ];
            open-floating = true;
          }

          {
            matches = [
              { app-id = "Plexamp"; }
              { app-id = "scratchpad"; }
            ];
            open-on-workspace = "scratchpad";
            open-floating = true;
          }

          {
            matches = [ { app-id = "launcher"; } ];
            open-floating = true;
            focus-ring = {
              enable = true;
              active.gradient = {
                from = strPalette.normal.magenta;
                to = strPalette.normal.cyan;
                angle = 120;
                in' = "srgb-linear";
              };
              width = 2;
            };
          }

          {
            # Open the Firefox picture-in-picture player as floating by default.
            # This app-id regular expression will work for both:
            # host Firefox (app-id is "firefox")
            # Flatpak Firefox (app-id is "org.mozilla.firefox")
            matches = [ { app-id = "r#\"librewolf$\"# title=\"^Picture-in-Picture$\""; } ];
            open-floating = true;
          }

          {
            matches = [
              { app-id = "Renoise"; }
              { app-id = "Bitwig"; }
              { app-id = "labwc"; }
            ];
            open-maximized = true;
          }
        ];

        # switch-events = with config.lib.niri.actions; {
        #   lid-close.action = spawn "shikanectl switch desk-clam";
        #   lid-open.action = spawn "niri msg output eDP-1 on && shikanectl reload";
        # };

        binds =
          with config.lib.niri.actions;
          let
            wez =
              spawn "wezterm" "--config-file" "/home/emenel/.config/wezterm/launcher.lua" "start"
                "--always-new-process";
            wezlaunch = wez "--class" "launcher";
          in
          {
            # shows a list of important hotkeys.
            "Mod+Shift+Slash".action = show-hotkey-overlay;

            "Mod+Space" = {
              hotkey-overlay.title = "Launcher";
              action = wezlaunch "sway-launcher-desktop";
            };

            "Print" = {
              hotkey-overlay.title = "Launcher (alternative key)";
              action = wezlaunch "sway-launcher-desktop";
            };

            "Mod+Ctrl+T" = {
              hotkey-overlay.title = "Wezterm";
              action = spawn "wezterm";
            };
            "Mod+Ctrl+E" = {
              hotkey-overlay.title = "Emacs (client)";
              action = spawn "emacsclient" "-r" "-n";
            };
            "Mod+Ctrl+B" = {
              hotkey-overlay.title = "Librewolf Browser";
              action = spawn "librewolf";
            };
            "Mod+Ctrl+V" = {
              hotkey-overlay.title = "Clipboard History";
              action = wezlaunch "clipse";
            };
            "Mod+Ctrl+F" = {
              hotkey-overlay.title = "Yazi";
              action = wez "yazi";
            };
            "Mod+Ctrl+N" = {
              hotkey-overlay.title = "Notifications";
              action = spawn "swaync-client" "-t";
            };
            "Mod+Ctrl+O" = {
              hotkey-overlay.title = "Obsidian";
              action = spawn "obsidian";
            };
            "Mod+Ctrl+S" = {
              hotkey-overlay.title = "Signal";
              action = spawn "signal-desktop";
            };
            "Mod+Ctrl+C" = {
              hotkey-overlay.title = "Calendar";
              action = spawn "gnome-calendar";
            };
            "Mod+Ctrl+A" = {
              hotkey-overlay.title = "btop++";
              action = wez "btop";
            };
            "Mod+Ctrl+P" = {
              hotkey-overlay.title = "plexamp";
              action = spawn "plexamp";
            };


            "Mod+Shift+A" = {
              hotkey-overlay.title = "Lock Session";
              action = spawn "hyprlock";
            };

            "XF86AudioPlay" = {
              allow-when-locked = true;
              action = spawn "playerctl" "play-pause";
            };
            "XF86AudioNext" = {
              allow-when-locked = true;
              action = spawn "playerctl" "next";
            };
            "XF86AudioPrev" = {
              allow-when-locked = true;
              action = spawn "playerctl" "previous";
            };

            "XF86AudioRaiseVolume" = {
              allow-when-locked = true;
              action = spawn "swayosd-client" "--output-volume" "raise";
            };

            "XF86AudioLowerVolume" = {
              allow-when-locked = true;
              action = spawn "swayosd-client" "--output-volume" "lower";
            };

            "XF86AudioMute" = {
              allow-when-locked = true;
              action = spawn "swayosd-client" "--output-volume" "mute-toggle";
            };

            "XF86AudioMicMute" = {
              allow-when-locked = true;
              action = spawn "swayosd-client" "--input-volume" "mute-toggle";
            };

            "XF86MonBrightnessUp" = {
              allow-when-locked = true;
              action = spawn "swayosd-client" "--brightness" "raise";
            };

            "XF86MonBrightnessDown" = {
              allow-when-locked = true;
              action = spawn "swayosd-client" "--brightness" "lower";
            };

            "Mod+Q".action = close-window;

            "Mod+Left".action = focus-column-left;
            "Mod+Down".action = focus-window-down;
            "Mod+Up".action = focus-window-up;
            "Mod+Right".action = focus-column-right;
            "Mod+H".action = focus-column-left;
            "Mod+J".action = focus-window-down;
            "Mod+K".action = focus-window-up;
            "Mod+L".action = focus-column-right;

            "Mod+Ctrl+Left".action = move-column-left;
            "Mod+Ctrl+Down".action = move-window-down;
            "Mod+Ctrl+Up".action = move-window-up;
            "Mod+Ctrl+Right".action = move-column-right;
            "Mod+Ctrl+H".action = move-column-left;
            "Mod+Ctrl+J".action = move-window-down;
            "Mod+Ctrl+K".action = move-window-up;
            "Mod+Ctrl+L".action = move-column-right;

            "Mod+Home".action = focus-column-first;
            "Mod+End".action = focus-column-last;
            "Mod+Ctrl+Home".action = move-column-to-first;
            "Mod+Ctrl+End".action = move-column-to-last;

            "Mod+Shift+Left".action = focus-monitor-left;
            "Mod+Shift+Down".action = focus-monitor-down;
            "Mod+Shift+Up".action = focus-monitor-up;
            "Mod+Shift+Right".action = focus-monitor-right;
            "Mod+Shift+H".action = focus-monitor-left;
            "Mod+Shift+J".action = focus-monitor-down;
            "Mod+Shift+K".action = focus-monitor-up;
            "Mod+Shift+L".action = focus-monitor-right;

            "Mod+Shift+Ctrl+Left".action = move-column-to-monitor-left;
            "Mod+Shift+Ctrl+Down".action = move-column-to-monitor-down;
            "Mod+Shift+Ctrl+Up".action = move-column-to-monitor-up;
            "Mod+Shift+Ctrl+Right".action = move-column-to-monitor-right;
            "Mod+Shift+Ctrl+H".action = move-column-to-monitor-left;
            "Mod+Shift+Ctrl+J".action = move-column-to-monitor-down;
            "Mod+Shift+Ctrl+K".action = move-column-to-monitor-up;
            "Mod+Shift+Ctrl+L".action = move-column-to-monitor-right;

            "Mod+Page_Down".action = focus-workspace-down;
            "Mod+Page_Up".action = focus-workspace-up;
            "Mod+U".action = focus-workspace-down;
            "Mod+I".action = focus-workspace-up;
            "Mod+Ctrl+Page_Down".action = move-column-to-workspace-down;
            "Mod+Ctrl+Page_Up".action = move-column-to-workspace-up;
            "Mod+Ctrl+U".action = move-column-to-workspace-down;
            "Mod+Ctrl+I".action = move-column-to-workspace-up;

            "Mod+Shift+Page_Down".action = move-workspace-down;
            "Mod+Shift+Page_Up".action = move-workspace-up;
            "Mod+Shift+U".action = move-workspace-down;
            "Mod+Shift+I".action = move-workspace-up;

            "Mod+WheelScrollDown" = {
              action = focus-workspace-down;
              cooldown-ms = 150;
            };
            "Mod+WheelScrollUp" = {
              action = focus-workspace-up;
              cooldown-ms = 150;
            };
            "Mod+Ctrl+WheelScrollDown" = {
              action = move-column-to-workspace-down;
              cooldown-ms = 150;
            };
            "Mod+Ctrl+WheelScrollUp" = {
              action = move-column-to-workspace-up;
              cooldown-ms = 150;
            };

            "Mod+WheelScrollRight".action = focus-column-right;
            "Mod+WheelScrollLeft".action = focus-column-left;
            "Mod+Ctrl+WheelScrollRight".action = move-column-right;
            "Mod+Ctrl+WheelScrollLeft".action = move-column-left;

            "Mod+Shift+WheelScrollDown".action = focus-column-right;
            "Mod+Shift+WheelScrollUp".action = focus-column-left;
            "Mod+Ctrl+Shift+WheelScrollDown".action = move-column-right;
            "Mod+Ctrl+Shift+WheelScrollUp".action = move-column-left;

            "Mod+BracketLeft".action = consume-or-expel-window-left;
            "Mod+BracketRight".action = consume-or-expel-window-right;
            "Mod+Comma".action = consume-window-into-column;
            "Mod+Period".action = expel-window-from-column;

            "Mod+R".action = switch-preset-column-width;
            "Mod+Shift+R".action = switch-preset-window-height;
            "Mod+Ctrl+R".action = reset-window-height;
            "Mod+F".action = maximize-column;
            "Mod+Shift+F".action = fullscreen-window;
            "Mod+C".action = center-column;

            "Mod+Minus".action = set-column-width "-10%";
            "Mod+Equal".action = set-column-width "+10%";
            "Mod+Shift+Minus".action = set-window-height "-10%";
            "Mod+Shift+Equal".action = set-window-height "+10%";

            "Mod+Ctrl+X".action = toggle-window-floating;
            "Mod+X".action = switch-focus-between-floating-and-tiling;
            "Mod+S".action = focus-workspace "scratchpad";

            "Mod+Print".action = screenshot;
            # "Mod+Ctrl+Print".action = screenshot-screen "false";
            # "Mod+Alt+Print".action = screenshot-window "false";

            # The quit action will show a confirmation dialog to avoid accidental exits.
            "Mod+Shift+E".action = quit;

            # Powers off the monitors. To turn them back on, do any input like
            # moving the mouse or pressing any other key.
            "Mod+Shift+P".action = power-off-monitors;
            "Mod+Shift+Alt+P" = {
              hotkey-overlay.title = "Turn Displays On";
              action = power-on-monitors;
            };
          };
      };
    };
  };
}
