{ config, pkgs, inputs, lib, ... }:
let
  noctalia =
    cmd:
    [
      "${pkgs.lib.getExe' inputs.noctalia.packages.${pkgs.system}.default "noctalia-shell"}"
      "ipc"
      "call"
    ]
    ++ (pkgs.lib.splitString " " cmd);

    wallpaper = (inputs.self + /backgrounds/pacific.png);
    matugenSchemeType = "scheme-tonal-spot";
in
{
  home.packages = with pkgs; [
    matugen
    # gsettings
    glib
    dconf
    gsettings-desktop-schemas
    # gtk
    nwg-look
    # qt config tool
    kdePackages.qt6ct
  ];

  home.sessionVariables = {
    QT_QPA_PLATFORMTHEME = "qt6ct";
  };

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

      mouse = {
        accel-speed = -0.5;
      };

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

    gestures.hot-corners.enable = false;

    layout = {
      gaps = 5;
      center-focused-column = "never";

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

      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}";
        inactive.color = "#${config.programs.matugen.theme.colors.surface.default}";
        urgent.color = "#${config.programs.matugen.theme.colors.error.default}";
      };

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

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

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

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

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

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

    workspaces."browser" = { };
    workspaces."chat" = { };

    window-rules = [
      {
        matches = [ { app-id = "^1Password$"; } ];
        open-floating = true;
        open-focused = true;
        block-out-from = "screen-capture";
      }

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

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

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

      {
        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 = [ "${pkgs.lib.getExe' inputs.noctalia.packages.${pkgs.system}.default "noctalia-shell"}" ]; }
    ];

    animations = {};

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

      "Mod+D" = {
        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.1+";
      };
      "XF86AudioLowerVolume" = {
        allow-when-locked = true;
        action.spawn-sh = "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.1-";
      };
      "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 previous";
      };
      "Mod+Comma" = {
        allow-when-locked = true;
        action.spawn-sh = "playerctl next";
      };
      "Mod+Slash" = {
        allow-when-locked = true;
        action.spawn-sh = "playerctl next";
      };

      # 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+O" = {
        repeat = false;
        action.toggle-overview = { };
      };
      "Mod+Q" = {
        repeat = false;
        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+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+Page_Down".action.focus-workspace-down = { };
      "Mod+Page_Up".action.focus-workspace-up = { };
      # TODO
      "Mod+Shift+Ctrl+Page_Down".action.move-column-to-workspace-down = { };
      "Mod+Shift+Ctrl+Page_Up".action.move-column-to-workspace-up = { };

      "Mod+Shift+Page_Down".action.move-workspace-down = { };
      "Mod+Shift+Page_Up".action.move-workspace-up = { };

      "Mod+WheelScrollDown" = {
        cooldown-ms = 150;
        action.focus-workspace-down = { };
      };
      "Mod+WheelScrollUp" = {
        cooldown-ms = 150;
        action.focus-workspace-up = { };
      };
      "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+F".action.maximize-column = { };
      "Mod+Shift+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 = "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;
  };

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