{ lib, config, pkgs, modulesPath, inputs, ... }:

let
  cfg = config.cspark-desktop-niri;
in
{
  imports =
    [
      inputs.niri.nixosModules.niri
    ];
  
  options.cspark-desktop-niri = {
    enable 
      = lib.mkEnableOption "Enable Custom Niri Desktop Setup";
  };

  config = lib.mkIf cfg.enable {
    # Enable the Niri Compositor and add extra system packages.
    programs.niri.enable = true;
    environment.systemPackages = with pkgs; [
      wl-clipboard
      swaylock # Locking
      swayidle # Idle Detection (For locking)
      swaybg # Set background wallpaper
      foot # Terminal Emulator
      fuzzel # Application Launcher - Wayland
      wdisplays # Manage wayland displays
      wlr-randr # Manage wayland displays - xrandr clone
      wlsunset # Day/night gamma adjustment/filter for wayland
      light # GNU/Linux application to control backlights
      mako # Notification Daemon
      wlprop # Clone of X11 xprop for wayland (To get window class names etc)
      wev # Clone of X11 xev for wayland
      waybar # Status bar
      xwayland-satellite # Xwayland support outside your Wayland compositor

      swayimg # Image viewer
      lxqt.pcmanfm-qt # File Manager
      lxqt.lxqt-archiver # Archiver
      qt6Packages.qt6ct # Manage QT6 Themes
      libsForQt5.qt5ct # Manage QT5 Themes
      lxappearance # Manage GTK Themes
      kdePackages.polkit-kde-agent-1 # Polkit Prompt for password prompts etc
      lxqt.lxqt-openssh-askpass # GUI to query passwords on behalf of SSH agents
      pavucontrol # Audio control

      (writeShellScriptBin "niri-start-session"
        ''
      		export TERM="foot"
      		export QT_QPA_PLATFORM="wayland"
      		export QT_QPA_PLATFORMTHEME="qt6ct"
      		export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
      		export SDL_VIDEODRIVER="wayland,x11,windows"
      		export _JAVA_AWT_WM_NONREPARENTING="1"
      		export SSH_ASKPASS_REQUIRE="force"
      		export SSH_ASKPASS="/run/current-system/sw/bin/lxqt-openssh-askpass"
      		export MOZ_ENABLE_WAYLAND="1"
      		# Enable native wayland support for chromium and electron based applications
      		export NIXOS_OZONE_WL="1"

		# Xwayland support via xwayland-satellite-flake
		export DISPLAY=":0"

      		niri-session
       '')
    ];
    # The minimal login manager I'm using for niri
    services.greetd = {
      enable = true;
      settings = {
        default_session = {
          command = "${pkgs.greetd.tuigreet}/bin/tuigreet --time --cmd niri-start-session";
          user = "greeter";
        };
      };
    };
    
    # XDG Portal Integration (For file chooser/screensharing etc)
    xdg.portal.enable = true;
    xdg.portal.wlr.enable = true;
    xdg.portal.extraPortals = with pkgs; [ xdg-desktop-portal-gnome xdg-desktop-portal-gtk ];
    # Whether to enable Qt configuration, including theming.
    qt.enable = true;
    # Enable polkit
    security.polkit.enable = true;
    # For udiskie
    services.udisks2.enable = true;

    # Xwayland setup
    programs.xwayland.enable = true;
    services.xserver.display = 0;
    services.xserver.enable = true;

    # Whether to enable GNOME Keyring daemon, a service designed to take care of the user’s security credentials, such as user names and passwords.
    services.gnome.gnome-keyring.enable = true;

    # GTK themes are not applied in Wayland applications / Window Decorations missing / Cursor looks different
    programs.dconf.enable = true;

    # Enable CUPS to print documents.
    services.printing.enable = true;

    # Enable sound with pipewire.
    # sound.enable = true; Deprecated option
    hardware.pulseaudio.enable = false;
    security.rtkit.enable = true;
    services.pipewire = {
      enable = true;
      alsa.enable = true;
      alsa.support32Bit = true;
      pulse.enable = true;
      # If you want to use JACK applications, uncomment this
      jack.enable = true;

      # use the example session manager (no others are packaged yet so this is enabled by default,
      # no need to redefine it in your config for now)
      #media-session.enable = true;
    };

    security.sudo = {
      extraRules = [
        {
          commands = [
            {
              command = "/run/current-system/sw/bin/light";
              options = [ "NOPASSWD" ];
            }
          ];
          groups = [ "wheel" ];
        }
      ];
    };

    programs.ssh = {
      enableAskPassword = true;
      askPassword = "/run/current-system/sw/bin/lxqt-openssh-askpass";
    };

    environment.sessionVariables = {
      TERM = "foot";
      QT_QPA_PLATFORM = "wayland";
      QT_QPA_PLATFORMTHEME = "qt6ct";
      QT_WAYLAND_DISABLE_WINDOWDECORATION = "1";
      SDL_VIDEODRIVER = "wayland,x11,windows";
      _JAVA_AWT_WM_NONREPARENTING = "1";
      SSH_ASKPASS_REQUIRE = "force";
      SSH_ASKPASS = "/run/current-system/sw/bin/lxqt-openssh-askpass";
      MOZ_ENABLE_WAYLAND = "1";
      # Enable native wayland support for chromium and electron based applications
      NIXOS_OZONE_WL = "1";

      # Xwayland support via xwayland-satellite-flake
      DISPLAY = ":0";
    };
    environment.variables = {
      TERM = "foot";
      QT_QPA_PLATFORM = "wayland";
      QT_QPA_PLATFORMTHEME = "qt6ct";
      QT_WAYLAND_DISABLE_WINDOWDECORATION = "1";
      SDL_VIDEODRIVER = "wayland,x11,windows";
      _JAVA_AWT_WM_NONREPARENTING = "1";
      SSH_ASKPASS_REQUIRE = "force";
      SSH_ASKPASS = lib.mkForce "/run/current-system/sw/bin/lxqt-openssh-askpass";
      MOZ_ENABLE_WAYLAND = "1";
      # Enable native wayland support for chromium and electron based applications
      NIXOS_OZONE_WL = "1";

      # Xwayland support via xwayland-satellite-flake
      DISPLAY = ":0";
    };

    environment.extraInit = ''
    	export DISPLAY=:0
    '';
    environment.shellInit = ''
    	export DISPLAY=:0
    '';

    environment.profileRelativeSessionVariables = {
      DISPLAY = [ 
      	":0"
      ];
    };

  };
}
