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

let cfg = config.sydnix.users.msyds.dank-material-shell;
in {
  options.sydnix.users.msyds.dank-material-shell = {
    enable = lib.mkEnableOption "Dank Material Shell";
  };

  config = lib.mkIf cfg.enable
    (let
      theme = {
        dark = with config.lib.stylix.colors.withHashtag; {
          name = "Stylix generatated dark theme";
          primary = base0D;
          primaryText = base00;
          primaryContainer = base0C;
          secondary = base0E;
          surface = base01;
          surfaceText = base05;
          surfaceVariant = base02;
          surfaceVariantText = base04;
          surfaceTint = base0D;
          background = base00;
          backgroundText = base05;
          outline = base03;
          surfaceContainer = base01;
          surfaceContainerHigh = base02;
          surfaceContainerHighest = base03;
          error = base08;
          warning = base0A;
          info = base0C;
        } // {
          primary = base06;
          surfaceContainerHigh = "#2a2a37"; # Sunk Ink 2
          surfaceContainerHighest = base05;
        };
        light = with config.lib.stylix.colors.withHashtag; {
          name = "Stylix generatated light theme";
          primary = base0D;
          primaryText = base07;
          primaryContainer = base0C;
          secondary = base0E;
          surface = base06;
          surfaceText = base01;
          surfaceVariant = base07;
          surfaceVariantText = base02;
          surfaceTint = base0D;
          background = base07;
          backgroundText = base00;
          outline = base04;
          surfaceContainer = base06;
          surfaceContainerHigh = base05;
          surfaceContainerHighest = base04;
          error = base08;
          warning = base0A;
          info = base0C;
        };
      };
    in {
      programs.niri.settings = {
        # Open DMS windows as floating by default
        window-rules = [
          {
            matches = [{app-id = "#org.quickshell$";}];
            open-floating = true;
          }
        ];
        # Blur wallpaper when overview is open.
        layer-rules = [
          {
            matches = [{ namespace = "dms:blurwallpaper"; }];
            place-within-backdrop = true;
          }
        ];
        spawn-at-startup = [
          {
            command = [
              (lib.getExe' pkgs.wl-clipboard "wl-paste")
              "--watch" "cliphist" "store"
            ];
          }
        ];
      };
      sydnix.users.crumb.niri = {
        polkit.enable = false;
        swaylock.enable = false;
        portal.enable = false;
        swaync.enable = false;
        gammastep.enable = false;
        binds =
          let dms-ipc = config.lib.niri.actions.spawn "dms" "ipc";
          in {
            "Mod+D" = {
              action = dms-ipc "spotlight" "toggle";
              hotkey-overlay.title = "Toggle Application Launcher";
            };
            "Mod+P" = {
              action = dms-ipc "notepad" "toggle";
              hotkey-overlay.title = "Toggle Notepad";
            };
            "Mod+Ctrl+L" = {
              action = dms-ipc "lock" "lock";
              hotkey-overlay.title = "Toggle Lock Screen";
            };
            "XF86AudioRaiseVolume" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "increment" "3";
            };
            "XF86AudioLowerVolume" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "decrement" "3";
            };
            "XF86AudioMute" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "mute";
            };
            "XF86AudioMicMute" = {
              allow-when-locked = true;
              action = dms-ipc "audio" "micmute";
            };
            "XF86MonBrightnessUp" = {
              allow-when-locked = true;
              action = dms-ipc "brightness" "increment" "5" "";
            };
            "XF86MonBrightnessDown" = {
              allow-when-locked = true;
              action = dms-ipc "brightness" "decrement" "5" "";
            };
          };
      };
      sydnix.dank-material-shell = {
        enable = true;
        settings = {
          theme = "dark";
          currentThemeName = "custom";
          customThemeFile =
            pkgs.writeText
              "dankMaterialShell-stylix-theme.json"
              (builtins.toJSON theme);
          popupTransparency = 1;
          dockTransparency = 1;
          use24HourClock = true;
          showSeconds = false;
          useFahrenheit = false;
          nightModeEnabled = true;
          animationSpeed = 1;
          customAnimationDuration = 500;
          wallpaperFillMode = "Fill";
          blurredWallpaperLayer = false;
          blurWallpaperOnOverview = true;
          showLauncherButton = true;
          showWorkspaceSwitcher = true;
          showFocusedWindow = true;
          showWeather = false;
          showMusic = true;
          showClipboard = true;
          showCpuUsage = true;
          showMemUsage = true;
          showCpuTemp = true;
          showGpuTemp = true;
          selectedGpuIndex = 0;
          enabledGpuPciIds = [];
          showSystemTray = true;
          showClock = true;
          showNotificationButton = true;
          showBattery = true;
          showControlCenterButton = true;
          showCapsLockIndicator = true;
          controlCenterShowNetworkIcon = true;
          controlCenterShowBluetoothIcon = true;
          controlCenterShowAudioIcon = true;
          controlCenterShowVpnIcon = true;
          controlCenterShowBrightnessIcon = false;
          controlCenterShowMicIcon = false;
          controlCenterShowBatteryIcon = false;
          controlCenterShowPrinterIcon = false;
          showPrivacyButton = true;
          privacyShowMicIcon = false;
          privacyShowCameraIcon = false;
          privacyShowScreenShareIcon = false;
          controlCenterWidgets = [
            {
              id = "volumeSlider";
              enabled = true;
              width = 50;
            }
            {
              id = "brightnessSlider";
              enabled = true;
              width = 50;
            }
            {
              id = "wifi";
              enabled = true;
              width = 50;
            }
            {
              id = "bluetooth";
              enabled = true;
              width = 50;
            }
            {
              id = "audioOutput";
              enabled = true;
              width = 50;
            }
            {
              id = "audioInput";
              enabled = true;
              width = 50;
            }
            {
              id = "nightMode";
              enabled = true;
              width = 50;
            }
            {
              id = "darkMode";
              enabled = true;
              width = 50;
            }
          ];
          showWorkspaceIndex = false;
          showWorkspacePadding = false;
          workspaceScrolling = false;
          showWorkspaceApps = false;
          maxWorkspaceIcons = 3;
          workspacesPerMonitor = true;
          showOccupiedWorkspacesOnly = false;
          dwlShowAllTags = false;
          workspaceNameIcons = {};
          waveProgressEnabled = true;
          scrollTitleEnabled = true;
          clockCompactMode = false;
          focusedWindowCompactMode = false;
          runningAppsCompactMode = true;
          keyboardLayoutNameCompactMode = false;
          runningAppsCurrentWorkspace = false;
          runningAppsGroupByApp = false;
          centeringMode = "index";
          clockDateFormat = "";
          lockDateFormat = "";
          mediaSize = 1;
          appLauncherViewMode = "list";
          spotlightModalViewMode = "list";
          sortAppsAlphabetically = false;
          appLauncherGridColumns = 4;
          spotlightCloseNiriOverview = true;
          niriOverviewOverlayEnabled = true;
          weatherLocation = "Arvada, Colorado";
          weatherCoordinates = "39.8005505,-105.0811573";
          useAutoLocation = false;
          weatherEnabled = true;
          networkPreference = "auto";
          vpnLastConnected = "";
          iconTheme = "System Default";
          launcherLogoMode = "apps";
          launcherLogoCustomPath = "";
          launcherLogoColorOverride = "";
          launcherLogoColorInvertOnMode = false;
          launcherLogoBrightness = 0.5;
          launcherLogoContrast = 1;
          launcherLogoSizeOffset = 0;
          fontFamily = "Inter Variable";
          monoFontFamily = "Fira Code";
          fontWeight = 400;
          fontScale = 1;
          notepadUseMonospace = true;
          notepadFontFamily = "";
          notepadFontSize = 14;
          notepadShowLineNumbers = false;
          notepadTransparencyOverride = -1;
          notepadLastCustomTransparency = 0.7;
          soundsEnabled = true;
          useSystemSoundTheme = false;
          soundNewNotification = true;
          soundVolumeChanged = true;
          soundPluggedIn = true;
          acMonitorTimeout = 0;
          acLockTimeout = 0;
          acSuspendTimeout = 0;
          acSuspendBehavior = 0;
          acProfileName = "";
          batteryMonitorTimeout = 0;
          batteryLockTimeout = 0;
          batterySuspendTimeout = 0;
          batterySuspendBehavior = 0;
          batteryProfileName = "";
          lockBeforeSuspend = false;
          loginctlLockIntegration = true;
          fadeToLockEnabled = false;
          fadeToLockGracePeriod = 5;
          launchPrefix = "";
          brightnessDevicePins = {};
          wifiNetworkPins = {};
          bluetoothDevicePins = {};
          audioInputDevicePins.preferredInput =
	     "alsa_input.usb-Generic_Blue_Microphones_2041BAB03EZ8-00.analog-stereo";
	  audioOutputDevicePins.preferredOutput =
	    "alsa_output.usb-Jieli_Technology_CA-2890_USB_Speaker_Bar_4250315A3537380E-00.analog-stereo";
          gtkThemingEnabled = false;
          qtThemingEnabled = false;
          syncModeWithPortal = true;
          terminalsAlwaysDark = false;
          showDock = false;
          dockAutoHide = false;
          dockGroupByApp = false;
          dockOpenOnOverview = false;
          dockPosition = 1;
          dockSpacing = 4;
          dockBottomGap = 0;
          dockMargin = 0;
          dockIconSize = 40;
          dockIndicatorStyle = "circle";
          dockBorderEnabled = false;
          dockBorderColor = "surfaceText";
          dockBorderOpacity = 1;
          dockBorderThickness = 1;
          notificationOverlayEnabled = false;
          modalDarkenBackground = true;
          lockScreenShowPowerActions = true;
          enableFprint = false;
          maxFprintTries = 15;
          lockScreenActiveMonitor = "all";
          lockScreenInactiveColor = "#000000";
          hideBrightnessSlider = false;
          notificationTimeoutLow = 5000;
          notificationTimeoutNormal = 5000;
          notificationTimeoutCritical = 0;
          notificationPopupPosition = 0;
          osdAlwaysShowValue = false;
          osdPosition = 5;
          osdVolumeEnabled = true;
          osdMediaVolumeEnabled = true;
          osdBrightnessEnabled = true;
          osdIdleInhibitorEnabled = true;
          osdMicMuteEnabled = true;
          osdCapsLockEnabled = true;
          osdPowerProfileEnabled = false;
          osdAudioOutputEnabled = true;
          powerActionConfirm = true;
          powerActionHoldDuration = 0.5;
          powerMenuActions = [
            "reboot"
            "logout"
            "poweroff"
            "lock"
            "suspend"
            "restart"
          ];
          powerMenuDefaultAction = "logout";
          powerMenuGridLayout = false;
          customPowerActionLock = "";
          customPowerActionLogout = "";
          customPowerActionSuspend = "";
          customPowerActionHibernate = "";
          customPowerActionReboot = "";
          customPowerActionPowerOff = "";
          updaterUseCustomCommand = false;
          updaterCustomCommand = "";
          updaterTerminalAdditionalParams = "";
          displayNameMode = "system";
          screenPreferences.wallpaper = [];
          showOnLastDisplay = {};
          barConfigs = [
            {
              id = "default";
              name = "Main Bar";
              enabled = true;
              position = 2;
              screenPreferences = [
                "all"
              ];
              showOnLastDisplay = true;
              leftWidgets = [
                "launcherButton"
                "workspaceSwitcher"
                "focusedWindow"
              ];
              centerWidgets = [
                "music"
                "clock"
                "weather"
              ];
              rightWidgets = [
                "systemTray"
                "clipboard"
                "cpuUsage"
                "memUsage"
                "notificationButton"
                "battery"
                "controlCenterButton"
              ];
              spacing = 4;
              innerPadding = 4;
              bottomGap = 0;
              transparency = 1;
              widgetTransparency = 1;
              squareCorners = false;
              noBackground = false;
              gothCornersEnabled = false;
              gothCornerRadiusOverride = false;
              gothCornerRadiusValue = 12;
              borderEnabled = false;
              borderColor = "surfaceText";
              borderOpacity = 1;
              borderThickness = 1;
              fontScale = 1;
              autoHide = false;
              autoHideDelay = 250;
              openOnOverview = false;
              visible = true;
              popupGapsAuto = true;
              popupGapsManual = 4;
            }
          ];
          configVersion = 2;
        };
      };
    });
}
