{
  config,
  pkgs,
  lib,
  osConfig,
  ...
}: let
  inherit (lib) optionalString primaryMonitor;

  sys = osConfig.modules.system;
in {
  mainBar = {
    layer = "top";
    position = "left";
    # monitor configuration, kind of dirty since it assumes DP-1 is my main monitor
    output = primaryMonitor osConfig; #builtins.elemAt monitors 0;
    width = 55;
    spacing = 7;
    margin-left = 6;
    margin-top = 9;
    margin-bottom = 9;
    margin-right = null;
    fixed-center = true;
    exclusive = true;
    modules-left = [
      "custom/search"
      "hyprland/workspaces"
      "custom/lock"
      "backlight"
      "battery"
      "custom/weather"
      "custom/todo"
    ];
    modules-center = [];
    modules-right = [
      "cpu"
      (optionalString sys.bluetooth.enable "bluetooth")
      "gamemode"
      "pulseaudio"
      "network"
      "custom/swallow"
      "clock"
      "custom/power"
    ];

    "hyprland/workspaces" = let
      hyprctl = config.wayland.windowManager.hyprland.package + "/bin/hyprctl";
    in {
      on-click = "activate";
      on-scroll-up = "${hyprctl} dispatch workspace m+1";
      on-scroll-down = "${hyprctl} dispatch workspace m-1";
      format = "{icon}";
      active-only = true;
      all-outputs = true;
      format-icons = {
        "1" = "‰∏Ä";
        "2" = "‰∫å";
        "3" = "‰∏â";
        "4" = "Âõõ";
        "5" = "‰∫î";
        "6" = "ÂÖ≠";
        "7" = "‰∏É";
        "8" = "ÂÖ´";
        "9" = "‰πù";
        "10" = "ÂçÅ";
      };
    };

    "custom/search" = {
      format = " ";
      tooltip = false;
      on-click = "${lib.getExe pkgs.killall} rofi || run-as-service $(rofi -show drun)";
    };

    "custom/todo" = {
      format = "{}";
      tooltip = true;
      interval = 7;
      exec = let
        todo = pkgs.todo + "/bin/todo";
        sed = pkgs.gnused + "/bin/sed";
        wc = pkgs.coreutils + "/bin/wc";
      in
        pkgs.writeShellScript "todo-waybar" ''
          #!/bin/sh

          total_todo=$(${todo} | ${wc} -l)
          todo_raw_done=$(${todo} raw done | ${sed} 's/^/      ‚óâ /' | ${sed} -z 's/\n/\\n/g')
          todo_raw_undone=$(${todo} raw todo | ${sed} 's/^/     ‚óâ /' | ${sed} -z 's/\n/\\n/g')
          done=$(${todo} raw done | ${wc} -l)
          undone=$(${todo} raw todo | ${wc} -l)
          tooltip=$(${todo})

          left="$done/$total_todo"

          header="<b>todo</b>\\n\\n"
          tooltip=""
          if [[ $total_todo -gt 0 ]]; then
          	if [[ $undone -gt 0 ]]; then
          		export tooltip="$headerüë∑ Today, you need to do:\\n\\n $(echo $todo_raw_undone)\\n\\n‚úÖ You have already done:\\n\\n $(echo $todo_raw_done)"
          		export output=" üóíÔ∏è \\n $left"
          	else
          		export tooltip="$header‚úÖ All done!\\nü•§ Remember to stay hydrated!"
          		export output=" üéâ \\n $left"
          	fi
          else
          	export tooltip=""
          	export output=""
          fi

          printf '{"text": "%s", "tooltip": "%s" }' "$output" "$tooltip"
        '';
      return-type = "json";
    };

    "custom/weather" = let
      waybar-wttr = pkgs.stdenv.mkDerivation {
        name = "waybar-wttr";
        buildInputs = [(pkgs.python3.withPackages (pythonPackages: with pythonPackages; [requests]))];
        unpackPhase = "true";
        installPhase = ''
          mkdir -p $out/bin
          cp ${../../scripts/waybar-wttr.py} $out/bin/waybar-wttr
          chmod +x $out/bin/waybar-wttr
        '';
      };
    in {
      format = "{}";
      tooltip = true;
      interval = 30;
      exec = "${waybar-wttr}/bin/waybar-wttr";
      return-type = "json";
    };

    "custom/lock" = {
      tooltip = false;
      on-click = "${pkgs.bash}/bin/bash -c '(sleep 0.5s; ${lib.getExe pkgs.swaylock-effects} --grace 0)' & disown";
      format = "ÔÄ£";
    };

    "custom/swallow" = {
      tooltip = false;
      on-click = let
        hyprctl = config.wayland.windowManager.hyprland.package + "/bin/hyprctl";
        notify-send = pkgs.libnotify + "/bin/notify-send";
        rg = pkgs.ripgrep + "/bin/rg";
      in
        pkgs.writeShellScript "waybar-swallow" ''
          #!/bin/sh
          if ${hyprctl} getoption misc:enable_swallow | ${rg} -q "int: 1"; then
          	${hyprctl} keyword misc:enable_swallow false >/dev/null &&
          	${notify-send} "Hyprland" "Turned off swallowing"
          else
          	${hyprctl} keyword misc:enable_swallow true >/dev/null &&
          	${notify-send} "Hyprland" "Turned on swallowing"
          fi
        '';
      format = "Û∞ä∞";
    };

    "custom/power" = {
      tooltip = false;
      on-click = let
        sudo = pkgs.sudo + "/bin/sudo";
        rofi = config.programs.rofi.package + "/bin/rofi";
        poweroff = pkgs.systemd + "/bin/poweroff";
        reboot = pkgs.systemd + "/bin/reboot";
      in
        pkgs.writeShellScript "shutdown-waybar" ''

          #!/bin/sh

          off="ÔÄë Shutdown"
          reboot="Ó´í Reboot"
          cancel="Û∞Öñ Cancel"

          sure="$(printf '%s\n%s\n%s' "$off" "$reboot" "$cancel" |
          	${rofi} -dmenu -p 'ÔÄë Are you sure?')"

          if [ "$sure" = "$off" ]; then
          	${sudo} ${poweroff}
          elif [ "$sure" = "$reboot" ]; then
          	${sudo} ${reboot}
          fi
        '';
      format = "Û∞ê•";
    };
    clock = {
      format = ''
        {:%H
        %M}'';
      tooltip-format = ''
        <big>{:%Y %B}</big>
        <tt><small>{calendar}</small></tt>
      '';
    };

    backlight = let
      brightnessctl = lib.getExe pkgs.brightnessctl;
    in {
      format = "{icon}";
      format-icons = ["Û∞ãô" "Û∞´É" "Û∞´Ñ" "Û∞´Ö" "Û∞´Ü" "Û∞´á" "Û∞´à"];
      #format-icons = ["Óéç" "Óèì" "Óèë" "Óèè" "Óèé" "Óèç" "Óèä" "Óèà" "Óéõ"];
      on-scroll-up = "${brightnessctl} s 1%-";
      on-scroll-down = "${brightnessctl} s +1%";
    };

    battery = {
      states = {
        warning = 30;
        critical = 15;
      };
      format = "{icon}";
      format-charging = "Û∞ÇÑ";
      format-plugged = "Û∞ÇÑ";
      format-alt = "{icon}";
      format-icons = ["Û∞ÇÉ" "Û∞Å∫" "Û∞Åª" "Û∞Åº" "Û∞ÅΩ" "Û∞Åæ" "Û∞Åæ" "Û∞Åø" "Û∞ÇÄ" "Û∞ÇÅ" "Û∞ÇÇ" "Û∞Åπ"];
    };
    network = let
      nm-editor = "${pkgs.networkmanagerapplet}/bin/nm-connection-editor";
    in {
      format-wifi = "Û∞§®";
      format-ethernet = "Û∞àÄ";
      format-alt = "Û±õá";
      format-disconnected = "Û∞§≠";
      tooltip-format = "{ipaddr}/{ifname} via {gwaddr} ({signalStrength}%)";
      on-click-right = "${nm-editor}";
    };

    pulseaudio = {
      scroll-step = 5;
      tooltip = true;
      tooltip-format = "{volume}";
      on-click = "${pkgs.killall}/bin/killall pavucontrol || ${pkgs.pavucontrol}/bin/pavucontrol";
      format = "{icon}";
      format-muted = "Û∞ùü";
      format-icons = {
        default = ["ÔÄ¶" "ÔÄß" "ÔÄ®"];
      };
    };

    cpu = {
      interval = 10;
      format = "Ôíº";
      max-length = 10;
      states = {
        "50" = 50;
        "60" = 75;
        "70" = 90;
      };
    };

    bluetooth = {
      # controller = "controller1", // specify the alias of the controller if there are more than 1 on the system
      format = "Ôäî";
      format-disabled = "Û∞Ç≤"; # an empty format will hide the module
      format-connected = "Û∞Ç±";
      tooltip-format = "{controller_alias}\t{controller_address}";
      tooltip-format-connected = "{controller_alias}\t{controller_address}\n\n{device_enumerate}";
      tooltip-format-disabled = "";
      tooltip-format-enumerate-connected = "{device_alias}\t{device_address}";
    };

    gamemode = {
      format = "Û∞ä¥";
      format-alt = "{glyph}";
      glyph = "Û∞ä¥";
      hide-not-running = true;
      use-icon = true;
      icon-name = "input-gaming-symbolic";
      icon-spacing = 4;
      icon-size = 20;
      tooltip = true;
      tooltip-format = "Games running: {count}";
    };
  };
}
