{
  lib,
  config,
  pkgs,
  ...
}: let
  cfg = config.marleyos.programs.waybar;
in {
  options.marleyos.programs.waybar = {
    enable = lib.mkEnableOption "waybar";
  };

  config = lib.mkIf cfg.enable {
    marleyos = {
      services.swaync.enable = true;
      programs.wlogout.enable = true;
    };

    stylix.targets.waybar.addCss = false;

    programs.waybar = {
      enable = true;
      systemd.enable = true;

      # https://gitlab.com/Zaney/zaneyos/-/blob/main/config/waybar.nix
      settings = let
        niri =
          config.programs.niri
          # HM module doesn't include an enable option...
          // {
            enable = config.marleyos.wayland.niri.enable;
          };
      in [
        {
          layer = "top";
          position = "top";

          modules-left =
            [
              "custom/startmenu"
            ]
            ++ (lib.optionals niri.enable [
              "niri/window"
            ])
            ++ [
              "pulseaudio"
              "cpu"
              "memory"
            ];
          modules-center =
            []
            ++ (lib.optionals niri.enable [
              "niri/workspaces"
            ]);
          modules-right =
            [
              "mpd"
              "custom/notifications"
              "tray"
            ]
            ++ (lib.optionals (!config.marleyos.isDesktop) [
              "battery"
            ])
            ++ [
              "network"
              "custom/exit"
              "clock"
            ];

          "custom/startmenu" = {
            tooltip = false;
            format = "";
            on-click = let
              inherit (config.marleyos.apps) launcher;
            in "sleep 0.1 && ${lib.getExe launcher.package} ${launcher.command}";
          };

          "niri/window" = lib.mkIf niri.enable {
            rewrite = {
              "" = " 🙈 No Windows? ";
            };
            max-length = 22;
          };

          "pulseaudio" = {
            format = "{icon} {volume}% {format_source}";
            format-bluetooth = "{volume}% {icon} {format_source}";
            format-bluetooth-muted = " {icon} {format_source}";
            format-muted = " {format_source}";
            format-source = " {volume}%";
            format-source-muted = "";
            format-icons = {
              headphone = "";
              hands-free = "󰏳";
              headset = "󰋎";
              phone = "";
              portable = "";
              car = "";
              default = [
                ""
                ""
                ""
              ];
            };
            on-click = "sleep 0.1 && ${lib.getExe pkgs.pwvucontrol}";
          };

          "cpu" = {
            interval = 5;
            format = " {usage:2}%";
            tooltip = true;
          };

          "memory" = {
            interval = 5;
            format = " {}%";
            tooltip = true;
          };

          "niri/workspaces" = lib.mkIf niri.enable (let
            niriMsg = msg: "${lib.getExe niri.package} msg action ${msg}";
          in {
            format = "{value}";
            format-icons = {
              default = " ";
              active = " ";
              urgent = " ";
            };
            on-scroll-up = niriMsg "focus-workspace-up";
            on-scroll-down = niriMsg "focus-workspace-down";
          });

          "mpd" = {
            format = "{stateIcon} {title} - {artist}  ({elapsedTime:%M:%S}/{totalTime:%M:%S})";
            format-disconnected = "󰝛";
            format-stopped = "";
            state-icons = {
              paused = "";
              playing = "";
            };
          };

          "custom/notifications" = let
            swaync-client =
              lib.getExe' config.services.swaync.package "swaync-client";
          in {
            tooltip = false;
            format = "{icon} {}";
            format-icons = {
              notification = "<span foreground='red'><sup></sup></span>";
              none = "";
              dnd-notification = "<span foreground='red'><sup></sup></span>";
              dnd-none = "";
              inhibited-notification = "<span foreground='red'><sup></sup></span>";
              inhibited-none = "";
              dnd-inhibited-notification = "<span foreground='red'><sup></sup></span>";
              dnd-inhibited-none = "";
            };
            return-type = "json";
            exec = "${swaync-client} -swb";
            on-click = "sleep 0.1 && ${swaync-client} -t";
            escape = true;
          };

          "tray" = {
            spacing = 12;
          };

          "battery" = {
            states = {
              warning = 30;
              critical = 15;
            };
            format = "{icon} {capacity}%";
            format-charging = "󰂄 {capacity}%";
            format-plugged = "󱘖 {capacity}%";
            format-icons = [
              "󰁺"
              "󰁻"
              "󰁼"
              "󰁽"
              "󰁾"
              "󰁿"
              "󰂀"
              "󰂁"
              "󰂂"
              "󰁹"
            ];
            on-click = "";
            tooltip = false;
          };

          "network" = {
            format-icons = [
              "󰤯"
              "󰤟"
              "󰤢"
              "󰤥"
              "󰤨"
            ];
            format-ethernet = " {bandwidthDownOctets}";
            format-wifi = "{icon} {signalStrength}%";
            format-disconnected = "󰤮";
            tooltip = false;
          };

          "custom/exit" = {
            tooltip = false;
            format = "";
            on-click = "sleep 0.1 && ${lib.getExe config.programs.wlogout.package}";
          };

          "clock" = {
            format = " {:L%I:%M %p}";
            tooltip = true;
            tooltip-format = "<big>{:%A, %d.%B %Y }</big>\n<tt><small>{calendar}</small></tt>";
          };
        }
      ];

      style = let
        transition = "all 0.3s cubic-bezier(.55,-0.68,.48,1.682)";
      in
        # css
        ''
          * {
            font-family: ${config.stylix.fonts.monospace.name};
            font-size: 16px;
            border-radius: 0;
            border: none;
            min-height: 0;
          }

          window#waybar {
            background: transparent;
          }

          tooltip {
            background: @base00;
            border: 1px solid @base08;
            border-radius: 12px;
          }

          tooltip label {
            color: @base08;
          }

          #custom-startmenu {
            color: @base0B;
            background: @base02;
            font-size: 28px;
            margin: 0;
            padding: 0 30px 0 15px;
            border-radius: 0 0 40px 0;
          }

          #window,
          #pulseaudio,
          #cpu,
          #memory {
            font-weight: bold;
            margin: 4px 0;
            margin-left: 7px;
            padding: 0 18px;
            background: @base04;
            color: @base00;
            border-radius: 24px 10px 24px 10px;
          }

          #workspaces {
            color: @base00;
            background: @base01;
            margin: 4px 4px;
            padding: 5px 5px;
            border-radius: 16px;
          }

          #workspaces button {
            font-weight: bold;
            padding: 5px;
            margin: 0 3px;
            border-radius: 16px;
            color: @base00;
            background: linear-gradient(45deg, @base08, @base0D);
            opacity: 0.5;
            transition: ${transition};
          }

          #workspaces button.active {
            font-weight: bold;
            padding: 0 5px;
            margin: 0 3px;
            border-radius: 16px;
            color: @base00;
            background: linear-gradient(45deg, @base08, @base0D);
            opacity: 1.0;
            transition: ${transition};
            min-width: 40px;
          }

          #workspaces button:hover {
            font-weight: bold;
            border-radius: 16px;
            color: @base00;
            background: linear-gradient(45deg, @base08, @base0D);
            opacity: 0.8;
            transition: ${transition};
          }

          #mpd,
          #network,
          #battery,
          #custom-notifications,
          #tray,
          #custom-exit {
            font-weight: bold;
            background: @base0D;
            color: @base00;
            margin: 4px 0;
            margin-right: 7px;
            border-radius: 10px 24px 10px 24px;
            padding: 0 18px;
          }

          #clock {
            font-weight: bold;
            color: @base00;
            background: linear-gradient(90deg, @base0E, @base0C);
            margin: 0;
            padding: 0 15px 0 30px;
            border-radius: 0 0 0 40px;
          }
        '';
    };
  };
}