{ pkgs, lib, ... }:

{
  programs.waybar.enable = true;

  programs.waybar.settings = [
    {
      layer    = "top";
      position = "top";
      spacing  = 0;
      height   = 26;

      "modules-left"   = [ "hyprland/workspaces" ];
      "modules-center" = [ "mpris" ];
      "modules-right"  = [
        "group/tray-expander"
        "cava"
        "clock"
        "bluetooth"
        "network"
        "pulseaudio"
        "cpu"
        "battery"
      ];

      "hyprland/workspaces" = {
        "on-click" = "activate";
        format = "{icon}";
        "format-icons" = {
          default = "";
          active  = "󱓻";
        };
      };

      cpu = {
        interval   = 5;
        format     = "󰍛";
        "on-click" = "kitty -e btop";
      };

      clock = {
        format          = "{:L%A %H:%M}";
        "format-alt"    = "{:L%d %B W%V %Y}";
        tooltip         = false;
        "on-click-right" = "omarchy-launch-floating-terminal-with-presentation omarchy-tz-select";
      };

      network = {
        "format-icons"          = [ "󰤯" "󰤟" "󰤢" "󰤥" "󰤨" ];
        format                  = "{icon}";
        "format-wifi"           = "{icon}";
        "format-ethernet"       = "󰀂";
        "format-disconnected"   = "󰤮";
        "tooltip-format-wifi"   = "{essid} ({frequency} GHz)\n⇣{bandwidthDownBytes}  ⇡{bandwidthUpBytes}";
        "tooltip-format-ethernet" = "⇣{bandwidthDownBytes}  ⇡{bandwidthUpBytes}";
        "tooltip-format-disconnected" = "Disconnected";
        interval                = 3;
        spacing                 = 1;
        "on-click"              = "omarchy-launch-wifi";
      };

      battery = {
        format                 = "{capacity}% {icon}";
        "format-discharging"   = "{icon}";
        "format-charging"      = "{icon}";
        "format-plugged"       = "";
        "format-icons" = {
          charging = [ "󰢜" "󰂆" "󰂇" "󰂈" "󰢝" "󰂉" "󰢞" "󰂊" "󰂋" "󰂅" ];
          default  = [ "󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹" ];
        };
        "format-full"          = "󰂅";
        "tooltip-format-discharging" = "{power:>1.0f}W↓ {capacity}%";
        "tooltip-format-charging"    = "{power:>1.0f}W↑ {capacity}%";
        interval               = 5;
        "on-click"             = "omarchy-menu power";
        states = {
          warning  = 20;
          critical = 10;
        };
      };

      bluetooth = {
        format            = "";
        "format-disabled"= "󰂲";
        "format-connected"= "";
        "tooltip-format"  = "Devices connected: {num_connections}";
        "on-click"        = "blueberry";
      };

      pulseaudio = {
        format        = "{icon}";
        "on-click"    = "kitty --class=Wiremix -e wiremix";
        "on-click-right" = "pamixer -t";
        "tooltip-format" = "Playing at {volume}%";
        "scroll-step" = 5;
        "format-muted" = "";
        "format-icons" = {
          default = [ "" "" "" ];
        };
      };
      
      mpris = {
        format = "{dynamic}";
        dynamic-order = ["title" "album" "artist" "position" "length"];
      };
      
      cava = {
        bars = 14;
        bar_delimiter = 0;
        format-icons = ["▁" "▂" "▃" "▄" "▅" "▆" "▇" "█"];
      };

      "group/tray-expander" = {
        orientation = "inherit";
        drawer = {
          "transition-duration" = 600;
          "children-class"      = "tray-group-item";
        };
        modules = [ "custom/expand-icon" "tray" ];
      };

      "custom/expand-icon" = {
        format  = "";
        tooltip = false;
      };

      tray = {
        "icon-size" = 12;
        spacing     = 17;
      };
    }
  ];

  programs.waybar.style = lib.mkAfter ''
    * {
      border: none;
      border-radius: 0;
      min-height: 0;
      font-family: 'CaskaydiaMono Nerd Font';
      font-size: 12px;
    }

    .modules-left {
      margin-left: 8px;
    }

    .modules-right {
      margin-right: 8px;
    }

    #workspaces button {
      all: initial;
      padding: 0 6px;
      margin: 0 1.5px;
      min-width: 9px;
    }

    #workspaces button.empty {
      opacity: 0.5;
    }
    
    .modules-left #workspaces button.active {
      border-bottom: 0px solid @base05;
    }
    
    .modules-left #workspaces button {
      border-bottom: 0px solid @base05;
    }

    #cpu,
    #battery,
    #pulseaudio,
    #custom-omarchy,
    #custom-screenrecording-indicator,
    #custom-update {
      min-width: 12px;
      margin: 0 7.5px;
    }

    #tray {
      margin-right: 16px;
    }

    #bluetooth {
      margin-right: 17px;
    }

    #network {
      margin-right: 13px;
    }

    #custom-expand-icon {
      margin-right: 20px;
    }

    tooltip {
      padding: 2px;
    }

    #custom-update {
      font-size: 10px;
    }

    #clock {
      margin-left: 8.75px;
    }

    .hidden {
      opacity: 0;
    }

    #custom-screenrecording-indicator {
      min-width: 12px;
      margin-left: 8.75px;
      font-size: 10px;
    }

    #custom-screenrecording-indicator.active {
      color: #a55555;
    }
  '';
}
