{
  config,
  lib,
  pkgs,
  ...
}: {
  imports = [../../programs/swaylock];

  options.dotfiles.inputs.home-manager.services.swayidle = {
    displayTimeout = {
      enable = lib.dotfiles.mkEnableOption;

      timeouts = {
        command = lib.mkOption {
          default = "";
          description = "Command to turn all displays off.";
          example = "swaymsg 'output * dpms off'";
          type = lib.types.str;
        };

        resumeCommand = lib.mkOption {
          default = "";
          description = "Command to turn all displays on.";
          example = "swaymsg 'output * dpms on'";
          type = lib.types.str;
        };

        timeout = lib.mkOption {
          default = 5 * 60;
          description = "Timeout in seconds.";
          example = 10 * 60;
          type = lib.types.ints.positive;
        };
      };
    };

    enable = lib.dotfiles.mkEnableOption;
  };

  config = let
    cfg = config.dotfiles.inputs.home-manager.services.swayidle;
  in
    lib.mkIf cfg.enable {
      dotfiles.inputs.home-manager.programs.swaylock.enable = true;

      services.swayidle = let
        command = "${lib.getExe pkgs.swaylock} --daemonize";
      in {
        enable = true;

        events = [
          {
            inherit command;
            event = "before-sleep";
          }

          {
            inherit command;
            event = "lock";
          }
        ];

        systemdTarget = "graphical-session-pre.target";

        timeouts = lib.mkIf cfg.displayTimeout.enable [
          {
            inherit command;
            inherit (cfg.displayTimeout.timeouts) timeout;
          }

          {
            inherit (cfg.displayTimeout.timeouts) command resumeCommand;

            timeout = builtins.floor (
              cfg.displayTimeout.timeouts.timeout * 1.2 + 0.5
            );
          }
        ];
      };
    };
}
