{ config, lib, pkgs, ... }:
{
  config = {
    services.mako = {
      enable = true;
      font = "Fira Sans 10";
      format = "<i>%s</i>\\n%b";
      margin = "2";
      maxVisible = -1;
      backgroundColor = "#000000dd";
      progressColor = "source #223544ff";
      width = 384;
      extraConfig = ''
        outer-margin=1
        max-history=100
        max-icon-size=48

        [grouped]
        format=<b>(%g)</b> <i>%s</i>\n%b

        [urgency=low]
        text-color=#999999ff

        [urgency=critical]
        background-color=#900000dd

        [app-name=Element]
        group-by=summary

        [mode=silent]
        invisible=1
      '';
      package = pkgs.symlinkJoin {
        name = "${pkgs.mako.name}-wrapped";
        paths = with pkgs; [ mako ];
        inherit (pkgs.mako) meta;
        postBuild = ''
          rm -r $out/share/dbus-1
        '';
      };
    };
    systemd.user.services.mako = {
      Unit = {
        Description = "Mako notification daemon";
        PartOf = [ "graphical-session.target" ];
      };
      Install = {
        WantedBy = [ "graphical-session.target" ];
      };
      Service = {
        Type = "dbus";
        BusName = "org.freedesktop.Notifications";
        ExecStart = lib.getExe config.services.mako.package;
        RestartSec = 5;
        Restart = "always";
      };
    };

    systemd.user.services.mako-follows-focus = {
      Unit = {
        BindsTo = [ "niri.service" "mako.service" ];
        After = [ "niri.service" "mako.service" ];
      };
      Service = {
        Type = "simple";
        Restart = "always";
        ExecStart = pkgs.writers.writePython3 "mako-follows-focus" {
          libraries = with pkgs.python3Packages; [];
        } ''
          import os
          import socket
          import json
          import subprocess


          current_output = None
          workspaces = []


          def output_changed(new_output):
              global current_output

              if current_output == new_output:
                  return

              current_output = new_output
              subprocess.run(["makoctl", "reload"])


          sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
          sock.connect(os.environ["NIRI_SOCKET"])
          sock.send(b"\"EventStream\"\n")
          for line in sock.makefile(buffering=1, encoding='utf-8'):
              if line_json := json.loads(line):
                  if "WorkspacesChanged" in line_json:
                      workspaces = line_json["WorkspacesChanged"]["workspaces"]
                      for workspace in workspaces:
                          if not workspace["is_focused"]:
                              continue
                          output_changed(workspace["output"])
                          break
                  if "WorkspaceActivated" in line_json and line_json["WorkspaceActivated"]["focused"]:  # noqa: E501
                      for workspace in workspaces:
                          if not workspace["id"] == line_json["WorkspaceActivated"]["id"]:  # noqa: E501
                              continue
                          output_changed(workspace["output"])
                          break
        '';
      };
      Install = {
        WantedBy = [ "mako.service" ];
      };
    };
  };
}