# vim: et:ts=2:sw=2:
{
  niri,
  pkgs,
  lib,
  deprekages,
  ...
}:
{
  # Use niri.
  nixpkgs.overlays = [ niri.overlays.niri ];
  imports = [

    # Imports that add additional functionality
    ./waybar
    ./niri.nix
    ./mako.nix
    ./nirilock-fancy.nix

    # Core niri import.
    niri.nixosModules.niri
    (
      { ... }:
      {

        # Enable Niri system-wide.
        programs.niri = {
          enable = true;
          package = pkgs.niri-unstable;
        };

        # We configure a cache ourselves; skip the upstream autoconf.
        niri-flake.cache.enable = false;
      }
    )
  ];

  # Enable espanso, when not on wayland.
  services.espanso.enable = true;

  # Espanso will try to run in any session; so we'll tell it not to run for _system_ users.
  systemd.user.services.espanso.serviceConfig.ConditionUser = "|1000";

  environment.systemPackages = with pkgs; [
    wtype
    wl-clipboard
    brightnessctl
    nirilock-fancy

    # required by xdg-desktop-portal-gnome
    nautilus

    xdg-desktop-portal-gtk
    xdg-desktop-portal-gnome
    kdePackages.kwallet

    # Terrible hack to type things in Wayland.
    (pkgs.writeScriptBin "haxtype" ''
            #!/usr/bin/env bash

           	declare -A direct_windows=(
      				[\"org.gajim.Gajim\"]=1
        			[\"nheko\"]=1
           	)

      			# Get the current application ID; we'll use it to
           	appid=$(niri msg -j focused-window | ${pkgs.jq}/bin/jq .app_id)

      			# If this is a Direct window, inject data from wtype directly.
            if [[ -n "''${direct_windows[$appid]}" ]]; then
              ${wtype}/bin/wtype "$1 "
              echo "Using direct input!"
              exit 0
            fi

      			# If this is a Wezterm window, use its special text input.
            if [[ $appid == \""org.wezfurlong.wezterm\"" ]]; then
      				wezterm cli send-text "$1 "
      				exit 0
            fi

      			#
            # Otherwise, use the clipboard (ugh).
            #
            TEMPFILE=$(${coreutils-full}/bin/coreutils --coreutils-prog=mktemp)

            # Save the clipboard.
            ${wl-clipboard}/bin/wl-paste -n > $TEMPFILE
            MIMETYPE=$(${wl-clipboard}/bin/wl-paste -l | head -n 1)

            # Type our our message.
            echo -n "$1" | ${wl-clipboard}/bin/wl-copy
            ${wtype}/bin/wtype -M Ctrl v -m Ctrl
            ${wtype}/bin/wtype ' '

            # Resetore the clipboard and clear our buffer.
            cat $TEMPFILE | ${wl-clipboard}/bin/wl-copy -t $MIMETYPE
            rm $TEMPFILE
    '')

    # Set up kwallet to run as a dbus handler.
    (pkgs.writeTextDir "share/dbus-1/services/org.freedesktop.secrets.service" ''[D-BUS Service]
Name=org.freedesktop.secrets
Exec=${pkgs.kdePackages.kwallet}/bin/kwalletd6'')
  ];

  xdg.portal = {
    enable = true;

    # These enable common operations and screencasting.
    extraPortals = with pkgs; [
      xdg-desktop-portal-gtk
      xdg-desktop-portal-gnome

      # Make kwallet work under niri, too.
      (pkgs.writeTextDir "share/xdg-desktop-portal/portals/kwallet-niri.portal" ''[portal]
DBusName=org.freedesktop.impl.portal.desktop.kwallet
Interfaces=org.freedesktop.impl.portal.Secret;
UseIn=niri'')
    ];
  };


  # Set up everything per-user but niri; which is in its own module.
  home-manager.users.deprekated =
    { pkgs, config, ... }:
    {

      # Use mako as a notification daemon.
      services.mako = {
        enable = true;
        defaultTimeout = 10000;
      };

      # Use fuzzel as a launcher.
      programs.fuzzel = {
        enable = true;
        settings.main.terminal = "wezterm";
      };

      #When using plasma, set things up to be more like Niri.
      home.file.".local/share/kwin/scripts/karousel".source = deprekages.kde-karousel;
      home.file.".local/share/kwin/effects/animations".source = pkgs.fetchFromGitHub {
        owner = "peterfajdiga";
        repo = "kwin4_effect_geometry_change";
        rev = "071609913450fc0b0ea5c39ad985e2fab39a5235";
        hash = "sha256-H3cslx6ceAJGXSa0+gNzmUINRoLeYODhGt4pSFfgNbQ=";
      };
    };

  # Use a graphical greeter.
  services.displayManager = {
    defaultSession = "niri";

    sddm = {
      enable = true;
      autoNumlock = true;
      wayland.enable = true;
    };
  };

  # Use KDE connect.
  programs.kdeconnect.enable = true;

  # Also enable plasma, for when we need to use X11 for e.g. Talon.
  services.xserver.enable = true;
  services.desktopManager.plasma6.enable = true;
  services.power-profiles-daemon.enable = lib.mkForce false;

  # Enable sound with pipewire.
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
  };
}
