inputs@{ machine, ... }:
{
  imports = machine.program {
    name = "niri";
    inherit inputs;
    requirements = [ "graphical" ];
    home-config =
      { config
      , pkgs
      , flakes
      , lib
      , ...
      }:
      let
        noctalia =
          cmd:
          [
            "${pkgs.lib.getExe' flakes.noctalia.packages.${pkgs.system}.default "noctalia-shell"}"
            "ipc"
            "call"
          ]
          ++ (pkgs.lib.splitString " " cmd);

        wallpaper = ("${pkgs.custom.raw-data}/pacific.png");
        matugenSchemeType = "scheme-tonal-spot";
      in
      {
        imports = [
          flakes.niri.homeModules.niri
          flakes.matugen.nixosModules.default
          flakes.noctalia.homeModules.default
        ];

        home.packages =
          with pkgs;
          [
            matugen
            glib
            dconf
            gsettings-desktop-schemas
            xwayland-satellite
            # gtk
            nwg-look
            # qt config tool
            kdePackages.qt6ct

            # media control
            playerctl
            # brightness control
            brightnessctl
            pavucontrol

            fira
            jetbrains-mono
            fira-mono
            noto-fonts
          ]
          ++ custom.maple-fonts-pack;

        programs.niri.settings = {
          # main laptop screen
          outputs."eDP-1" = {
            mode = {
              width = 1928;
              height = 1200;
              refresh = 59.987;
            };
            position = {
              x = 0;
              y = 0;
            };
          };

          outputs."LG Electronics LG ULTRAWIDE 411NTBK28189" = {
            mode = {
              width = 3440;
              height = 1440;
              refresh = 59.987;
            };
            position = {
              x = -3440;
              y = 240;
            };

            # focus the external screen first
            focus-at-startup = true;
          };

          outputs."LG Electronics LG ULTRAWIDE 409NTAB3P496" = {
            mode = {
              width = 3440;
              height = 1440;
              refresh = 59.987;
            };
            position = {
              x = -3440;
              y = 240;
            };

            # focus the external screen first
            focus-at-startup = true;
          };
        };

        home.sessionVariables = {
          QT_QPA_PLATFORMTHEME = "qt6ct";
          XCURSOR_THEME = "Adwaita";
          XCURSOR_SIZE = "10";
          DEFAULT_BROWSER = "${config.programs.firefox.package}/bin/firefox";
          BROWSER = "${config.programs.firefox.package}/bin/firefox";
        };

        programs.niri.settings = {
          input = {
            keyboard = {
              xkb = {
                layout = "us";
                options = "grp:win_space_toggle,compose:ralt";
              };
              numlock = true;
            };

            mouse = {
              accel-speed = 0.5;
            };

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

            focus-follows-mouse = {
              enable = true;
              max-scroll-amount = "0%";
            };

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

          debug = {
            render-drm-device = "/dev/dri/by-path/pci-0000:00:02.0-render";
          };

          cursor = {
            theme = "Adwaita";
            size = 10;
          };

          gestures.hot-corners.enable = true;

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

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

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

            shadow = {
              softness = 20;
              spread = 5;
              offset = {
                x = 0;
                y = 5;
              };
            };

            focus-ring = {
              width = 1;
              active.color = "${config.programs.matugen.theme.colors.primary.default.color}";
              inactive.color = "${config.programs.matugen.theme.colors.surface.default.color}";
              urgent.color = "${config.programs.matugen.theme.colors.error.default.color}";
            };

            border = {
              active.color = "${config.programs.matugen.theme.colors.primary.default.color}";
              inactive.color = "${config.programs.matugen.theme.colors.surface.default.color}";
              urgent.color = "${config.programs.matugen.theme.colors.error.default.color}";
            };

            shadow = {
              color = "${config.programs.matugen.theme.colors.shadow.default.color}70";
            };

            tab-indicator = {
              active.color = "${config.programs.matugen.theme.colors.primary.default.color}";
              inactive.color = "${config.programs.matugen.theme.colors.primary_container.default.color}";
              urgent.color = "${config.programs.matugen.theme.colors.error.default.color}";
            };

            insert-hint = {
              display.color = "${config.programs.matugen.theme.colors.primary.default.color}80";
            };
          };

          hotkey-overlay.skip-at-startup = true;

          screenshot-path = "~/Documents/personal/pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png";

          workspaces."01-browser" = {
            name = "browser";
          };
          workspaces."02-programming" = {
            name = "programming";
          };
          workspaces."03-chat" = {
            name = "chat";
          };

          window-rules = [
            {
              matches = [
                {

                  app-id = "firefox$";
                  title = "Extension: (Bitwarden Password Manager)";
                }
              ];
              open-floating = true;
              open-focused = true;
              block-out-from = "screen-capture";
            }
            {
              matches = [
                {
                  app-id = "firefox$";
                  title = "^Picture-in-Picture$";
                }
              ];
              open-floating = true;
            }

            {
              matches = [{ app-id = "firefox"; }];
              open-on-workspace = "browser";
            }

            {
              matches = [
                {
                  app-id = "org.gnome.Nautilus";
                  title = "Open Files";
                }
                {
                  app-id = "steam";
                  title = "Steam Settings";
                }
                { app-id = "pavucontrol"; }
              ];
              open-floating = true;
            }

            {
              matches = [
                { app-id = "discord"; }
                { app-id = "org.element.desktop"; } # TODO
                { app-id = "signal"; } # TODO
              ];
              open-on-workspace = "chat";
            }

            {
              matches = [
                { app-id = "dev.zed.Zed"; }
              ];
              open-on-workspace = "programming";
            }

            {
              geometry-corner-radius = {
                top-left = 8.0;
                top-right = 8.0;
                bottom-right = 8.0;
                bottom-left = 8.0;
              };
              clip-to-geometry = true;
            }
          ];

          spawn-at-startup = [
            { argv = [ "firefox" ]; }
            { argv = [ "discord" ]; }
            { argv = [ "signal-desktop" ]; }
            { argv = [ "zeditor" ]; }
            {
              argv = [ "${pkgs.lib.getExe' flakes.noctalia.packages.${pkgs.system}.default "noctalia-shell"}" ];
            }
            {
              sh = ''
                systemctl --user import-environment NIRI_SOCKET
                systemctl --user restart kanata
              '';
            }
          ];

          animations = { };

          binds = {
            "Ctrl+Alt+Delete" = {
              hotkey-overlay.title = "Power menu";
              action.spawn = noctalia "sessionMenu toggle";
            };

            "Mod+P" = {
              hotkey-overlay.title = "Run an Application";
              action.spawn = noctalia "launcher toggle";
            };

            "Mod+L" = {
              hotkey-overlay.title = "Lock the Screen";
              action.spawn = noctalia "lockScreen lock";
            };

            "Mod+Shift+Slash".action.show-hotkey-overlay = { };

            "Mod+Return" = {
              hotkey-overlay.title = "Open a Terminal";
              action.spawn = "kitty";
            };

            "XF86AudioRaiseVolume" = {
              allow-when-locked = true;
              action.spawn-sh = "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.05+";
            };
            "XF86AudioLowerVolume" = {
              allow-when-locked = true;
              action.spawn-sh = "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.05-";
            };
            "XF86AudioMute" = {
              allow-when-locked = true;
              action.spawn-sh = "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle";
            };
            "XF86AudioMicMute" = {
              allow-when-locked = true;
              action.spawn-sh = "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle";
            };
            "XF86AudioPlay" = {
              allow-when-locked = true;
              action.spawn-sh = "playerctl play-pause";
            };
            "XF86AudioStop" = {
              allow-when-locked = true;
              action.spawn-sh = "playerctl stop";
            };
            "XF86AudioPrev" = {
              allow-when-locked = true;
              action.spawn-sh = "playerctl previous";
            };
            "XF86AudioNext" = {
              allow-when-locked = true;
              action.spawn-sh = "playerctl next";
            };
            "Mod+Period" = {
              allow-when-locked = true;
              action.spawn-sh = "playerctl nest";
            };
            "Mod+Comma" = {
              allow-when-locked = true;
              action.spawn-sh = "playerctl previous";
            };
            "Mod+Slash" = {
              allow-when-locked = true;
              action.spawn-sh = "playerctl play-pause";
            };

            # TODO
            "XF86MonBrightnessUp" = {
              allow-when-locked = true;
              action.spawn = [
                "brightnessctl"
                "--device=amdgpu_bl1"
                "--class=backlight"
                "set"
                "+10%"
              ];
            };
            "XF86MonBrightnessDown" = {
              allow-when-locked = true;
              action.spawn = [
                "brightnessctl"
                "--device=amdgpu_bl1"
                "--class=backlight"
                "set"
                "10%-"
              ];
            };

            "Mod+Q" = {
              repeat = false;
              action.close-window = { };
            };

            "Mod+Left".action.focus-column-left = { };
            "Mod+Down".action.focus-window-or-workspace-down = { };
            "Mod+Up".action.focus-window-or-workspace-up = { };
            "Mod+Right".action.focus-column-right = { };

            "Mod+Shift+Left".action.move-column-left = { };
            "Mod+Shift+Down".action.move-window-down-or-to-workspace-down = { };
            "Mod+Shift+Up".action.move-window-up-or-to-workspace-up = { };
            "Mod+Shift+Right".action.move-column-right = { };

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

            "Mod+Ctrl+Left".action.focus-monitor-left = { };
            "Mod+Ctrl+Down".action.focus-monitor-down = { };
            "Mod+Ctrl+Up".action.focus-monitor-up = { };
            "Mod+Ctrl+Right".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+1".action.focus-workspace = 1;
            "Mod+2".action.focus-workspace = 2;
            "Mod+3".action.focus-workspace = 3;
            "Mod+4".action.focus-workspace = 4;
            "Mod+5".action.focus-workspace = 5;
            "Mod+6".action.focus-workspace = 6;
            "Mod+7".action.focus-workspace = 7;
            "Mod+8".action.focus-workspace = 8;
            "Mod+9".action.focus-workspace = 9;
            "Mod+Shift+1".action.move-column-to-workspace = 1;
            "Mod+Shift+2".action.move-column-to-workspace = 2;
            "Mod+Shift+3".action.move-column-to-workspace = 3;
            "Mod+Shift+4".action.move-column-to-workspace = 4;
            "Mod+Shift+5".action.move-column-to-workspace = 5;
            "Mod+Shift+6".action.move-column-to-workspace = 6;
            "Mod+Shift+7".action.move-column-to-workspace = 7;
            "Mod+Shift+8".action.move-column-to-workspace = 8;
            "Mod+Shift+9".action.move-column-to-workspace = 9;

            "Mod+WheelScrollDown" = {
              cooldown-ms = 150;
              action.focus-workspace-down = { };
            };
            "Mod+WheelScrollUp" = {
              cooldown-ms = 150;
              action.focus-workspace-up = { };
            };
            "Mod+Shift+WheelScrollDown".action.focus-column-left = { };
            "Mod+Shift+WheelScrollUp".action.focus-column-right = { };

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

            # "Mod+WheelScrollRight".action.focus-column-right = { };
            # "Mod+WheelScrollLeft".action.focus-column-left = { };
            # "Mod+Shift+WheelScrollRight".action.move-column-right = { };
            # "Mod+Shift+WheelScrollLeft".action.move-column-left = { };

            "Mod+BracketLeft".action.consume-or-expel-window-left = { };
            "Mod+BracketRight".action.consume-or-expel-window-right = { };

            "Mod+Shift+BracketLeft".action.consume-window-into-column = { };
            "Mod+Shift+BracketRight".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+D".action.maximize-column = { };
            "Mod+F".action.fullscreen-window = { };
            "Mod+S".action.expand-column-to-available-width = { };
            "Mod+C".action.center-column = { };
            "Mod+Shift+C".action.center-visible-columns = { };

            "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+E".action.toggle-window-floating = { };
            "Mod+Shift+E".action.switch-focus-between-floating-and-tiling = { };

            "Mod+W".action.toggle-column-tabbed-display = { };

            "Mod+Shift+S".action.screenshot = { };

            "Mod+Escape" = {
              allow-inhibiting = false;
              action.toggle-keyboard-shortcuts-inhibit = { };
            };

            "Mod+Shift+P".action.power-off-monitors = { };
          };
        };

        programs.noctalia-shell = {
          enable = true;
          systemd.enable = false;
          settings = {
            general = {
              # avatarImage = cfg.pfp;
            };
            colorSchemes = {
              darkMode = true;
              generateTemplatesForPredefined = true;
              inherit matugenSchemeType;
              predefinedScheme = "Noctalia (default)";
              useWallpaperColors = true;
            };
            location = {
              monthBeforeDay = false;
              name = "Amsterdam";
            };
            wallpaper = {
              enabled = true;
              setWallpaperOnAllMonitors = true;
              fillMode = "crop";
            };
            appLauncher = {
              enableClipboardHistory = true;
              terminalCommand = "kitty -e";
            };
            sessionMenu = {
              enableCountdown = true;
              countdownDuration = 5000;
            };
            controlCenter = {
              position = "close_to_bar_button";
              shortcuts = {
                left = [
                  {
                    id = "WiFi";
                  }
                  {
                    id = "Bluetooth";
                  }
                  {
                    id = "PowerProfile";
                  }
                  {
                    id = "KeepAwake";
                  }
                ];
                right = [ ];
              };
            };
            bar = {
              density = "compact";
              position = "right";
              backgroundOpacity = 0.5;
              widgets = {
                left = [
                  {
                    id = "ControlCenter";
                    useDistroLogo = true;
                  }
                  {
                    id = "NotificationHistory";
                  }
                  {
                    id = "plugin:catwalk";
                  }
                ];
                center = [
                  {
                    hideUnoccupied = false;
                    id = "Workspace";
                    labelMode = "none";
                  }
                ];
                right = [
                  {
                    id = "Tray";
                    drawerEnabled = false;
                  }
                  {
                    id = "WiFi";
                  }
                  {
                    id = "Bluetooth";
                  }
                  {
                    id = "Brightness";
                  }
                  {
                    id = "Volume";
                  }
                ]
                ++ [{ id = "Battery"; }]
                ++ [
                  {
                    id = "KeyboardLayout";
                    displayMode = "forceOpen";
                  }
                  {
                    formatHorizontal = "HH:mm";
                    formatVertical = "HH mm";
                    id = "Clock";
                    useMonospacedFont = true;
                    usePrimaryColor = true;
                  }
                ];
              };
            };
            templates = {
              gtk = true;
              qt = true;
              niri = true;
            };
          };
        };

        home.file.".cache/noctalia/wallpapers.json" = {
          text = builtins.toJSON {
            defaultWallpaper = wallpaper;
          };
        };

        home.activation.themeFiles = lib.hm.dag.entryAfter [ "writeBoundary" ] ''
          mkdir -p ${config.xdg.configHome}/gtk-4.0
          mkdir -p ${config.xdg.configHome}/gtk-3.0
          mkdir -p ${config.xdg.configHome}/qt5ct/colors
          mkdir -p ${config.xdg.configHome}/qt6ct/colors

          touch ${config.xdg.configHome}/gtk-4.0/gtk.css
          touch ${config.xdg.configHome}/gtk-3.0/gtk.css
          touch ${config.xdg.configHome}/qt5ct/colors/noctalia.conf
          touch ${config.xdg.configHome}/qt6ct/colors/noctalia.conf
        '';

        programs.matugen = {
          enable = true;
          wallpaper = wallpaper;
          type = matugenSchemeType;
        };

        gtk.cursorTheme = {
          package = pkgs.adwaita-icon-theme;
          name = "Adwaita";
          size = 24;
        };

        dconf.settings = {
          # appearance
          "org/gnome/desktop/interface" = {
            color-scheme = "prefer-dark";
            enable-hot-corners = true;
            gtk-enable-primary-paste = false;
          };
        };
      };
  };
}
