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

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

  config = lib.mkIf cfg.enable {
    # Config Files (typically $HOME/.config)
    xdg.configFile = {
      "vesktop/themes".source = ./resources/home-manager/.config/vesktop/themes;
      "waybar".source = ./resources/home-manager/.config/waybar;
      "niri/config.kdl".source = ./resources/home-manager/.config/niri/config.kdl;
      "mako/config".source = ./resources/home-manager/.config/mako/config;
      "foot/foot.ini".source = ./resources/home-manager/.config/foot/foot.ini;
      "networkmanager-dmenu/config.ini".source = ./resources/home-manager/.config/networkmanager-dmenu/config.ini;
    };

    services.udiskie.enable = true;
    services.udiskie.tray = "never";

    # Automatically executed on niri startup
    home.packages = with pkgs; [
      (writeShellScriptBin "niri-autostart"
        ''
		  [[ $(hostname) == *"laptop"* ]] && waybar &
                  mako &
		  #swaybg -i $(find ${config.home.homeDirectory}/Pictures/Wallpapers/ggg/. -type f | shuf -n1) -m stretch &
		  swaybg -i ${config.home.homeDirectory}/Pictures/Wallpapers/solarisdolphins_olive.png -m tile &
                  wlsunset -l 51.5 -L 0.1 &
		  kdeconnect-indicator &

		  # Xwayland support via xwayland-satellite-flake
		  export DISPLAY=":0"
      		  xwayland-satellite &
        '')
      (writeShellScriptBin "fuzzel-xwayland"
        ''
		DISPLAY=:0 fuzzel --background=fbf1c7ff --border-color=d79921ff --text-color=3c3836ff --match-color=f9f5d7ff --selection-color=d79921ff --selection-text-color=fbf1c7ff
        '')
	(writeShellScriptBin "fuzzel-pass"
	''
shopt -s nullglob globstar

typeit=0
if [[ $1 == "--type" ]]; then
typeit=1
shift
fi

prefix=''${PASSWORD_STORE_DIR-~/.password-store}
password_files=( "$prefix"/**/*.gpg )
password_files=( "''${password_files[@]#"$prefix"/}" )
password_files=( "''${password_files[@]%.gpg}" )

password=$(printf '%s\n' "''${password_files[@]}" | fuzzel --background=fbf1c7ff --border-color=d79921ff --text-color=3c3836ff --match-color=f9f5d7ff --selection-color=d79921ff --selection-text-color=fbf1c7ff --dmenu "$@")

[[ -n $password ]] || exit

if [[ $typeit -eq 0 ]]; then
        pass_output=$(pass show -c "$password" 2>/dev/null)
        notify-send "$pass_output"
else
        pass_output=$(pass show "$password" | { IFS= read -r pass; printf %s "$pass"; }) 
        notify-send "$pass_output"
fi
	'')
    ];

    home.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";
    };
  programs.bash = {
    initExtra = ''
      export DISPLAY=":0"
    '';
  };

    # Start sway after login if on TTY1 (Default Linux TTY upon boot/login)
    #programs.bash.profileExtra = ''
    #                 if [[ $(tty) == "/dev/tty1" ]]; then
    #                                   niri-session
		#		                               exit
    #                 fi
    #                 '';
  };
}
