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

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

  config = lib.mkIf cfg.enable {
    # Enable the Sway Tiling Compositor and add extra system packages.
    programs.sway = {
      enable = true;
      extraPackages = with pkgs; [
        swaylock # Locking
        swayidle # Idle Detection (For locking)
        foot # Terminal Emulator
        dmenu # Application Launcher
        wmenu # Application Launcher - Wayland
        networkmanager_dmenu # Network Manager dmenu script
        wdisplays # Manage wayland displays
        wlsunset # Day/night gamma adjustment/filter for wayland
        light # GNU/Linux application to control backlights
        sway-contrib.grimshot # Screenshot tool for wayland
        mako # Notification Daemon
        wlprop # Clone of X11 xprop for wayland (To get window class names etc)
        wev # Clone of X11 xev for wayland

      	#lxqt.pcmanfm-qt # File Manager
      	nautilus # 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
      ];
    };
    # XDG Portal Integration (For file chooser/screensharing etc)
    xdg.portal.enable = true;
    xdg.portal.wlr.enable = true;
    xdg.portal.extraPortals = with pkgs; [ kdePackages.xdg-desktop-portal-kde ];
    # Whether to enable Qt configuration, including theming.
    qt.enable = true;
    # Enable xwayland support
    programs.xwayland.enable = true;
    # Enable polkit
    security.polkit.enable = true;
    # For udiskie
    services.udisks2.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;
    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" ];
        }
      ];
    };

    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 = "lxqt-openssh-askpass";
      # Enable native wayland support for chromium and electron based applications
      NIXOS_OZONE_WL = "1";
    };
    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 = lib.mkForce "lxqt-openssh-askpass";
      # Enable native wayland support for chromium and electron based applications
      NIXOS_OZONE_WL = "1";
    };
    
  };
}
