{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cfg = config.local.gui;
in {
  config = mkIf cfg.enable {
    home.packages = with pkgs; [
      wlr-randr
      bemenu
    ];

    home.sessionVariables = {
      "BEMENU_BACKEND" = "wayland";
    };

    services = {
      swayidle = {
        enable = true;
        timeouts = [
          {
            timeout = 600;
            command = "${getExe pkgs.gtklock} -d";
          }
        ];
      };
    };

    systemd.user.services.wl-gammarelay-rs = {
      Unit.After = ["sway-session.target"];
      Install.WantedBy = ["sway-session.target"];

      Service.ExecStart = getExe pkgs.wl-gammarelay-rs;
    };

    wayland.windowManager.sway = {
      enable = true;

      config = {
        output =
          lib.mapAttrs (k: v: {
            mode = "${toString v.width}x${toString v.height}@${v.rate}Hz";
            pos = "${toString v.posX} ${toString v.posY}";
          })
          cfg.monitors;

        modifier = "Mod4";

        focus = {
          followMouse = true;
          wrapping = "workspace";
        };

        input = {
          "*" = {
            xkb_layout = "us";
            xkb_variant = "altgr-intl";
          };
        };

        fonts = {
          names = ["JetBrains Mono"];
          style = "Regular";
          size = 8.0;
        };

        gaps = {
          inner = 8;
          outer = -10;
          smartBorders = "on";
          smartGaps = true;
        };

        bars = singleton {
          command = "waybar";
          position = "top";
        };

        window = {
          hideEdgeBorders = "both";
          titlebar = false;
          border = 2;
        };

        floating = {
          border = 0;
          titlebar = false;
        };

        menu = "bemenu-run";

        workspaceLayout = "tabbed";

        colors = {
          background = "#141414";
          focused = {
            border = "#444444";
            background = "#000000";
            text = "#eaeaea";
            indicator = "#000000";
            childBorder = "#000000";
          };
          focusedInactive = {
            border = "#000000";
            background = "#000000";
            text = "#eaeaea";
            indicator = "#000000";
            childBorder = "#000000";
          };
          unfocused = {
            border = "#000000";
            background = "#000000";
            text = "#eaeaea";
            indicator = "#000000";
            childBorder = "#000000";
          };
          urgent = {
            border = "#ff6666";
            background = "#ff6666";
            text = "#eaeaea";
            indicator = "#ff6666";
            childBorder = "#ff6666";
          };
          placeholder = {
            border = "#000000";
            background = "#000000";
            text = "#eaeaea";
            indicator = "#000000";
            childBorder = "#000000";
          };
        };

        keybindings = let
          mod = config.wayland.windowManager.sway.config.modifier;
          grimshot = getExe pkgs.sway-contrib.grimshot;
          bemenuCommand = ''bemenu-run --center --width-factor 0.2 --fixed-height --list 10 --scrollbar none --auto-select --accept-single --fn "JetBrains Mono 12" --prompt "" --tb "#000000" --tf "#EAEAEA" --fb "#000000" --ff "#EAEAEA" --cb "#EAEAEA" --cf "#000000" --nb "#000000" --nf "#EAEAEA" --sb "#000000" --sf "#EAEAEA" --hb "#000000" --hf "#EAEAEA" --fbb "#000000" --fbf "#000000" --ab "#000000" --af "#EAEAEA"'';
        in
          mkOptionDefault {
            "${mod}+a" = "focus parent";
            "${mod}+c" = "focus child";
            "${mod}+d" = "exec ${bemenuCommand}";
            "${mod}+l" = "exec ${getExe pkgs.gtklock} -d";
            "${mod}+Return" = "exec ${lib.getExe pkgs.foot} ${lib.getExe pkgs.tmux}";
            "${mod}+Shift+s" = "exec ${grimshot} copy area";
            "${mod}+Shift+a" = "exec ${grimshot} copy output";
            "${mod}+Tab" = "focus right";
            "${mod}+Shift+Tab" = "focus left";
            "${mod}+Shift+w" = "move workspace to output right";
          };

        startup = [
          {
            command = "${lib.getExe pkgs.sway} 'workspace 1; exec ${lib.getExe pkgs.firefox}'";
          }
          {
            command = "${lib.getExe pkgs.sway} 'workspace 2; exec ${lib.getExe pkgs.tdesktop}'";
          }
          {
            command = "${lib.getExe pkgs.sway} 'workspace 2; exec ${lib.getExe pkgs.gajim}'";
          }
          {
            command = "${lib.getExe pkgs.swaybg} -m fill -i ${config.home.homeDirectory}/Pictures/wallpapers/jupiter.png";
            always = true;
          }
          {
            command = "${lib.getExe pkgs.networkmanagerapplet}";
          }
        ];
      };

      extraSessionCommands = ''
        export SDL_VIDEODRIVER=wayland
        # needs qt5.qtwayland in systemPackages
        export QT_QPA_PLATFORM=wayland
        export QT_WAYLAND_DISABLE_WINDOWDECORATION="1"
        # Fix for some Java AWT applications (e.g. Android Studio),
        # use this if they aren't displayed properly:
        export _JAVA_AWT_WM_NONREPARENTING=1
      '';

      swaynag.enable = true;
      systemd.enable = true;
      xwayland = true;
    };
  };
}
