{ pkgs, config, ... }:

let terminalExec = "${pkgs.lib.getExe config.defaultPrograms.terminal} -e";
in
rec {
  programs.waybar = {
    enable = true;
    settings = [{
      modules-left = [ "sway/workspaces" ];
      modules-right = [
        "pulseaudio"
        "bluetooth"
        "network"
        "cpu"
        "memory"
        "disk"
        "battery"
        "tray"
        "sway/language"
        "clock"
      ];
      "sway/workspaces" = {
        enable-bar-scroll = true;
        disable-scroll-wraparound = true;
        smooth-scrolling-threshold = 2.0;
      };
      pulseaudio = {
        format = "{volume}% {icon} {format_source}";
        format-bluetooth = "{volume}% {icon} {format_source}";
        format-muted = "{volume}% 🔇 {format_source}";
        format-source = "{volume}% ";
        format-source-muted = "{volume}% ";
        format-icons = [ "🔈" "🔊" ];
        on-click = "exec wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle";
        on-click-right = "exec wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle";
        on-click-middle = "exec pavucontrol";
        on-scroll-up = "";
        on-scroll-down = "";
      };
      network = {
        format-wifi = "{essid} ({signalStrength}%) ";
        format-ethernet = "{ipaddr}/{cidr} ";
        tooltip-format = "{ifname} via {gwaddr} ";
        format-linked = "{ifname} (No IP) ";
        format-disconnected = "Disconnected ⚠";
        format-alt = "{ifname}: {ipaddr}/{cidr}";
        on-click-right = "exec ${terminalExec} nmtui";
      };
      bluetooth = {
        format = " {status}";
        format-disabled = ""; # hide module
        format-connected = " {num_connections}";
        tooltip-format = "{controller_alias}	{controller_address}";
        tooltip-format-connected = ''
          {controller_alias}	{controller_address}

          {device_enumerate}'';
        tooltip-format-enumerate-connected = "{device_alias}	{device_address}";
        on-click = "exec blueman-manager";
        on-click-right = "exec bluetoothctl disconnect";
      };
      cpu = { format = "{usage}% 😀"; };
      memory = { format = "{used:0.1f}GB/{total:0.1f}GB "; };
      disk = {
        format = "{used} ";
        path = "/";
      };
      battery = {
        states = {
          good = 95;
          warning = 20;
          critical = 10;
        };
        format = "{capacity}% {icon}";
        format-charging = "{capacity}% ";
        format-plugged = "{capacity}% ";
        # format-alt = "{time} {icon}";
        format-icons = [ "" "" "" "" "" ];
        on-click = "";
      };
      tray = {
        icon-size = 20;
        spacing = 5;
      };
      "sway/language" = {
        format = "{shortDescription}";
        tooltip-format = "{long}";
        on-click = ''swaymsg input "*" xkb_switch_layout next'';
      };
      clock = {
        interval = 1;
        format = "<span size='10pt' font='Red Hat Mono'>{:L%H:%M:%S %a %F}</span>";
        locale =
          "fr_CA.utf8"; # TODO fix this; I don't want it to be hardcoded but rather tied to i18n.defaultLocale
        tooltip-format = ''
          <big>{:L%Y %B}</big>
          <tt><small>{calendar}</small></tt>'';
      };
    }];
    style = ''
      * {
        font-family: Rubik, "OpenMoji Color", sans-serif;
        font-size: 12pt;
        min-height: 20pt;
      }
    '' + ''
      window#waybar {
        background: rgba(34, 34, 34, 1);
        color: white;
      }
    '' + ''
      #workspaces button {
        padding: 0 3px;
        background: rgba(34, 34, 34, 1);
        border-style: hidden;
        border-radius: 0;
        color: white;
      }
    '' + ''
      #workspaces button.focused {
        background: rgba(40, 85, 119, 1);
      }

      .modules-right label,
      .modules-right box {
        border-left: 1px solid #d3d3d3;
        padding-left: 5px;
        padding-right: 5px;
      }
    '' + ''
      #battery.warning:not(.charging) {
        background: rgba(140, 0, 0, 1);
      }

      #battery.critical:not(.charging) {
        background: #ffffff;
        color: black;
      }
    '' + ''
      #language {
        min-width: 30px;
      }
    '' + "";
  };

  wayland.windowManager.sway.config.bars = [{
    command = "${pkgs.lib.getExe config.programs.waybar.package}";
    position = "top";
    fonts = config.wayland.windowManager.sway.config.fonts;
  }];
}
