{
  lib,
  config,
  pkgs,
  ...
}: let
  cfg = config.myConfig.sway.waybar;

  schema = "org.gnome.desktop.a11y.applications";
  key = "screen-keyboard-enabled";

  squeekboard_toggle = pkgs.writeShellScriptBin "squeekboard_toggle" ''
    if [ "$(gsettings get ${schema} ${key})" == "true" ]; then
      gsettings set ${schema} ${key} false
    else
      gsettings set ${schema} ${key} true
    fi
  '';

  squeekboard_format = pkgs.writeShellScriptBin "squeekboard_format" ''
    if [ "$(gsettings get ${schema} ${key})" == "true" ]; then
      echo '{ "text": "󰌌", "tooltip": "enabled", "percentage": 100 }'
    else
      echo '{ "text": "󰌐", "tooltip": "disabled", "percentage": 0 }'
    fi
  '';
in {
  options.myConfig.sway.waybar = {
    enable = lib.mkEnableOption "waybar, a status bar for wayland compositors";
    squeekboard = lib.mkEnableOption "squeekboard";
    mpd = lib.mkEnableOption "mpd";
    battery = lib.mkEnableOption "battery";
    backlight = lib.mkEnableOption "backlight";
  };

  config = lib.mkIf cfg.enable {
    wayland.windowManager.sway.config.startup = [{command = "${pkgs.waybar}/bin/waybar";}];
    programs.waybar = {
      enable = true;
      settings = {
        mainBar = {
          layer = "top";
          position = "top";
          height = 26;
          modules-left = ["sway/workspaces" "sway/mode"];
          modules-center = ["sway/window"];
          modules-right = ["mpd" "wireplumber" "tray" "custom/squeekboard" "sway/language" "clock" "backlight" "battery" "network"];

          "sway/workspaces" = {
            disable-scroll = true;
            all-outputs = false;
            format = "<span size='150%'>{icon}</span> {name}";
            format-icons = {
              "main" = "󰈹";
              "term" = "";
              "msg" = "󰷹";
              "music" = "󰫔";
              "urgent" = "";
              "focused" = "";
              "default" = "";
            };
          };

          "mpd" = lib.mkIf cfg.mpd {
            format = "{stateIcon}{consumeIcon}{randomIcon}{repeatIcon}{singleIcon} {title} ({elapsedTime:%M:%S}/{totalTime:%M:%S}) {volume}";
            format-stopped = "{stateIcon}{randomIcon}{repeatIcon}{singleIcon} Stopped {volume}";
            format-disconnected = "";
            unkown-tag = "N/A";
            interval = 2;
            consume-icons = {
              on = "󰮯 ";
              off = "";
            };
            random-icons = {
              on = "󰒟 ";
              off = "󰒞 ";
            };
            repeat-icons = {
              on = "󰑖 ";
              off = "󰑗 ";
            };
            single-icons = {
              on = "󰑘 ";
              off = "";
            };
            state-icons = {
              paused = "󰏤 ";
              playing = "󰐊 ";
            };
            max-length = 50;
          };

          "tray" = {
            spacing = 10;
          };

          "custom/squeekboard" = lib.mkIf cfg.squeekboard {
            format = "{}";
            exec = "${squeekboard_format}/bin/squeekboard_format";
            return-type = "json";
            exec-on-event = true;
            interval = 2;
            on-click = "${squeekboard_toggle}/bin/squeekboard_toggle";
          };

          "sway/language" = {
            format = "{shortDescription}";
            tooltip = false;
          };

          "clock" = {
            interval = 60;
            format = "{:%a %d.%m %H:%M}";
          };

          "backlight" = lib.mkIf cfg.backlight {
            format = "{percent}% {icon}";
            format-icons = ["󰹐" "󱩎" "󱩏" "󱩐" "󱩑" "󱩒" "󱩓" "󱩔" "󱩕" "󱩖" "󰛨"];
          };

          "battery" = lib.mkIf cfg.battery {
            format = "{capacity}% {icon}";
            format-charging = "{capacity}% {icon}󱐋";
            format-icons = ["󰂎" "󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹"];
            states = {
              warning = 30;
              critical = 15;
            };
          };

          "network" = {
            format-wifi = "{essid} ({signalStrength}%) 󰖩";
            format-ethernet = "{ipaddr}/{cidr} 󰈀";
            tooltip-format = "{ifname} via {gwaddr} 󰌘";
            format-disconnected = "󰌙";
          };

          "wireplumber" = {
            format = "{volume}% {icon}";
            format-muted = "";
            format-icons = ["" "" ""];
          };
        };
      };
      style = ./waybar.css;
    };
  };
}
