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

let
  cfg = config.cspark-desktop-exwm;
in
{
  
  options.cspark-desktop-exwm = {
    enable 
      = lib.mkEnableOption "Enable Custom EXWM Desktop Setup";
  };

  config = lib.mkIf cfg.enable {
    services.libinput.enable = true;

    services.xserver = {
      enable = true;

      # Remove/disable xterm, we are using Foot.
      desktopManager.xterm.enable = false;
      excludePackages = with pkgs; [ xterm ];

      # windowManager.exwm.enable = true;
      windowManager.session = lib.lists.singleton {
	name = "exwm";
	start = ''
		emacs
	'';
      };

      displayManager.startx.enable = true;
    };

    programs.gnupg = {
      agent.enable = true;
      agent.pinentryPackage = pkgs.pinentry-qt;
    };

    environment.systemPackages = with pkgs; [
      xorg.libxcb
      gcr # For GNOME gpg pinentry
      pinentry-qt

      gtk3 # Includes gtk-launch which is used by counsel-linux-app inside emacs

      emacs-gtk
      #((inputs.emacs-overlay.packages.x86_64-linux.emacs-unstable-pgtk.overrideAttrs (oldAttrs: { 
      #    	buildInputs = oldAttrs.buildInputs ++ [ pkgs.webkitgtk ];
      #})).override {
      #		withXwidgets = true;
      #})
      # inputs.emacs-overlay.packages.x86_64-linux.emacs-unstable-pgtk
	    #emacs30-gtk3 # Emacs 30 currently bugged with lsp-bridge, see https://github.com/manateelazycat/lsp-bridge/issues/1123

      light # GNU/Linux application to control backlights

      udiskie # Automounting drives
      dunst # Notification daemon

      alacritty # External terminal just in case
      scrot # Screenshot tool
      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 "exwm-start-session"
        ''
      		export SDL_VIDEODRIVER="x11,windows"
      		export _JAVA_AWT_WM_NONREPARENTING="1"
      		export SSH_ASKPASS_REQUIRE="force"
      		export SSH_ASKPASS="/run/current-system/sw/bin/lxqt-openssh-askpass"

      		emacs
       '')
    ];
    # The minimal login manager I'm using for niri
    services.greetd = {
      enable = true;
      settings = {
        default_session = {
          command = "${pkgs.greetd.tuigreet}/bin/tuigreet --time --remember-session";
          user = "greeter";
        };
      };
    };
    
    # XDG Portal Integration (For file chooser/screensharing etc)
    xdg.portal.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;

    # KDE Connect
    programs.kdeconnect.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 = {
      EDITOR="emacsclient -c";
      QT_QPA_PLATFORMTHEME = "qt6ct";
      SDL_VIDEODRIVER = "x11,windows";
      _JAVA_AWT_WM_NONREPARENTING = "1";
      SSH_ASKPASS_REQUIRE = "force";
      SSH_ASKPASS = "/run/current-system/sw/bin/lxqt-openssh-askpass";
    };
    environment.variables = {
      EDITOR="emacsclient -c";
      QT_QPA_PLATFORMTHEME = "qt6ct";
      SDL_VIDEODRIVER = "x11,windows";
      _JAVA_AWT_WM_NONREPARENTING = "1";
      SSH_ASKPASS_REQUIRE = "force";
      SSH_ASKPASS = lib.mkForce "/run/current-system/sw/bin/lxqt-openssh-askpass";
    };

  };
}
