{ config, pkgs, ... }:
{
  programs.waybar = let
    swayWorkspaceModule = {
      format = "{name}";
      sort-by-number = true;
      enable-bar-scroll = true;
      warp-on-scroll = false;
      disable-scroll-wraparound = true;
    };

    windowModule = {
      icon = true;
      icon-size = 15;
      all-outputs = true;
      tooltip = false;
      rewrite = {
        "(.*) — LibreWolf" = "   $1";
        "LibreWolf" = "   Firefox";
        "(.*) - LibreWolf — Firefox" = "󰗃   $1";
      };
    };

    pulseModule = {
      format = "{icon}  {volume}%";
      format-bluetooth = "{icon} {volume}%";
      format-muted = " muted";
      format-icons = {
        headphone = "󰋋 ";
        headset = "󰋋 ";
        default = [ " " " " ];
      };
      on-click = "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle";
      on-click-middle = "wpctl set-volume @DEFAULT_AUDIO_SINK@ 100%";
      on-click-right = "wpctl set-volume @DEFAULT_AUDIO_SINK@ 60%";
      ignored-sinks = [
        "Easy Effects Sink"
        "USB FS AUDIO Analog Stereo"
      ];
    };

    # Monitoring
    cpuModule = {
      format = "  {usage}%";
      interval = 3;
    };

    ramModule = {
      format = "  {used}G";
      tooltip = false;
      interval = 3;
    };

    vramModule = {
      exec = pkgs.writeScript "vramScript" ''
        # Don't run the script if running on integrated graphics
        gpu_driver=$(${pkgs.pciutils}/bin/lspci -k | grep -Eo "in use: (nvidia|amdgpu)" | awk '{print $3}')

        if [ -n "$gpu_driver" ]; then
          if [ "$gpu_driver" == "nvidia" ]; then
            vram_usage_mb=$(nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits)
            temperature=$(nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits)

          elif [ "$gpu_driver" == "amdgpu" ]; then
            vram_usage_mb=$(echo "$(cat /sys/class/drm/card0/device/mem_info_vram_used || cat /sys/class/drm/card1/device/mem_info_vram_used) / 1024 / 1024" | bc)
            temperature=$(${pkgs.lm_sensors}/bin/sensors | grep 'edge' | awk '{print $2}' | sed 's/[^0-9.-]//g')
          fi

          # Check if VRAM usage is under 1GB
          if [ $vram_usage_mb -lt 1024 ]; then
            vram_usage_display="$(echo $vram_usage_mb)M"
          else
            # Convert MB to GiB
            vram_usage_gib=$(bc <<< "scale=2; $vram_usage_mb / 1024")
            vram_usage_display="$(echo $vram_usage_gib)G"
          fi

          # Print the VRAM usage and temperature
	  echo "{\"text\":\"󰢮  $(echo $vram_usage_display)\",\"tooltip\":\"$(echo $temperature)°C\"}"
        fi
      '';
      format = "{}";
      return-type = "json";
      interval = 3;
    };

    # Clocks
    longClockModule = {
      exec = pkgs.writeScript "longClock" ''
        time=$(date +'%a %b %-d %-I:%M:%S%p')
        date=$(date "+%Y-%m-%d")
        echo "{\"text\":\"  $time\",\"tooltip\":\"$date\"}"
      '';
      format = "{}";
      return-type = "json";
      interval = 1;
      tooltip = true;
    };

    shortClockModule = {
      exec = "echo '  '$(date +'%l:%M%p' | sed 's/^ //')";
      interval = 60;
      tooltip = false;
    };

    # Misc
    trayModule.spacing = 5;

    networkModule = {
      format-ethernet = "󰈀";
      format-wifi = "";
      format-disconnected = "󰖪";
      format-linked = "";
      tooltip-format-ethernet = "{ipaddr}\n{ifname} ";
      tooltip-format-wifi = "{ipaddr}\n{essid} ({signalStrength}%)";
      tooltip-format-disconnected = "Disconnected";
    };

    bluetoothModule = {
      format = "";
      format-disabled = "";
      format-no-controller = "";
      tooltip-format-on = "No devices connected.";
      tooltip-format-connected = "{num_connections} connected\n{device_enumerate}";
      tooltip-format-enumerate-connected = "{device_alias}";
      tooltip-format-enumerate-connected-battery = "{device_alias} {device_battery_percentage}%";
      on-click = "rofi-bluetooth";
    };

    scratchpadModule = {
      format = "   {count}";
      show-empty = false;
      tooltip = true;
      tooltip-format = "{title}";
    };

    gamemodeModule = {
      format = "{glyph}";
      hide-not-running = true;
      use-icon = true;
      icon-size = 19;
      icon-spacing = 0;
      tooltip = true;
    };

    privacyModule = {
      icon-spacing = 5;
      icon-size = 15;
    };

    mediaModule = {
      format = "  {title}";
      format-paused = "  {artist}";
      format-stopped = "";
      format-disconnected = "";
      tooltip-format = "{artist} - {title}";
      interval = 2;
      max-length = 30;
      on-click = "mpc toggle";
    };
    notificationModule = {
      exec = pkgs.writeScript "notificationScript" ''
        # Run makoctl mode and store the output in a variable
        mode_output=$(makoctl mode)

        # Extract the second line after "default"
        mode_line=$(echo "$mode_output" | sed -n '/default/{n;p}')

        # Print the notification status with the tooltip
        if [[ "$mode_line" == "do-not-disturb" ]]; then
          printf '{"text":"󱆥  Off","class":"disabled","tooltip":"Notifications Disabled."}'
        else
          printf '{"text":"  On","tooltip":"Notifications Enabled."}';
        fi
      '';
      format = "{}";
      return-type = "json";
      interval = 2;
      on-click = "makotoggle";
    };

    weatherModule = {
      exec = "${pkgs.wttrbar}/bin/wttrbar --ampm";
      format = "{}°";
      tooltip = true;
      return-type = "json";
      interval = 3600;
    };

    # Laptop modules
    backlightModule = {
      format = "{icon}  {percent}%";
      format-icons = [ "" "󰖨" ];
      tooltip = false;
    };
    batteryModule = {
      interval = 60;
      states = {
        warning = 30;
        critical = 15;
      };
      format = "{icon}   {capacity}%";
      format-icons = [ "" "" "" "" "" ];
    };
  in {
    enable = config.home.desktop.enable;
    settings = {
      display1 = {
        name = "bar1";
        position = "top";
        layer = "top";
        output = [
          config.displays.d1
          "VGA-1"
        ];
        modules-left = [
          "sway/workspaces"
          "sway/window"
          "niri/workspaces"
          "niri/window"
          "hyprland/workspaces"
          "hyprland/window"
        ];
        modules-right = [
          "pulseaudio"
          "cpu"
          "memory"
          "custom/vram"
          "custom/clock-long"
          "gamemode"
          "sway/scratchpad"
          "privacy"
          "tray"
          "bluetooth"
          "network"
        ];
        "sway/workspaces" = swayWorkspaceModule;
        "sway/window" = windowModule;
        "niri/window" = windowModule;
        "hyprland/window" = windowModule;
        "pulseaudio" = pulseModule;
        "cpu" = cpuModule;
        "memory" = ramModule;
        "custom/vram" = vramModule;
        "custom/clock-long" = longClockModule;
        "gamemode" = gamemodeModule;
        "privacy" = privacyModule;
        "sway/scratchpad" = scratchpadModule;
        "tray" = trayModule;
        "bluetooth" = bluetoothModule;
        "network" = networkModule;
      };

      display2 = {
        name = "bar2";
        position = "top";
        layer = "top";
        output = [ config.displays.d2 ];
        modules-left = [
          "sway/workspaces"
          "sway/window"
          "niri/workspaces"
          "niri/window"
          "hyprland/workspaces"
          "hyprland/window"
        ];
        modules-right = [
          "pulseaudio"
          "mpd"
          "custom/notifs"
          "cpu"
          "memory"
          "custom/vram"
          "custom/clock-long"
        ];
        "sway/workspaces" = swayWorkspaceModule;
        "sway/window" = windowModule;
        "niri/window" = windowModule;
        "hyprland/window" = windowModule;
        "pulseaudio" = pulseModule;
        "mpd" = mediaModule;
        "custom/notifs" = notificationModule;
        "cpu" = cpuModule;
        "memory" = ramModule;
        "custom/vram" = vramModule;
        "custom/clock-long" = longClockModule;
      };

      display3 = {
        name = "bar3";
        position = "top";
        layer = "top";
        output = [ config.displays.d3 ];
        modules-left = [
          "sway/workspaces"
          "sway/window"
          "niri/workspaces"
          "niri/window"
          "hyprland/workspaces"
          "hyprland/window"
        ];
        modules-right = [
          "pulseaudio"
          "custom/weather"
          "cpu"
          "memory"
          "custom/vram"
          "custom/clock-short"
        ];
        "sway/workspaces" = swayWorkspaceModule;
        "sway/window" = windowModule;
        "niri/window" = windowModule;
        "hyprland/window" = windowModule;
        "pulseaudio" = pulseModule;
        "custom/weather" = weatherModule;
        "cpu" = cpuModule;
        "memory" = ramModule;
        "custom/vram" = vramModule;
        "custom/clock-short" = shortClockModule;
      };
      displayLap = {
        name = "laptop";
        position = "top";
        layer = "top";
        output = [
	  "eDP-1"
	  "LVDS-1"
	  "DSI-1"
	  "HDMI-A-1"
	];
        modules-left = [
          "sway/workspaces"
          "sway/window"
          "niri/workspaces"
          "niri/window"
          "hyprland/workspaces"
          "hyprland/window"
        ];
        modules-right = [
          "pulseaudio"
          "mpd"
          "custom/notifs"
          "custom/weather2"
          "cpu"
          "memory"
          "custom/vram"
          "backlight"
          "battery"
          "custom/clock-long"
          "gamemode"
          "sway/scratchpad"
          "tray"
          "bluetooth"
          "network"
        ];
        "sway/workspaces" = swayWorkspaceModule;
        "sway/window" = windowModule;
        "niri/window" = windowModule;
        "hyprland/window" = windowModule;
        "pulseaudio" = pulseModule;
        "mpd" = mediaModule;
        "custom/notifs" = notificationModule;
        "custom/weather2" = weatherModule;
        "cpu" = cpuModule;
        "memory" = ramModule;
        "custom/vram" = vramModule;
        "backlight" = backlightModule;
        "battery" = batteryModule;
        "custom/clock-long" = longClockModule;
        "sway/scratchpad" = scratchpadModule;
        "tray" = trayModule;
        "bluetooth" = bluetoothModule;
        "network" = networkModule;
      };
    };

    style = ''
      * {
        border: 0;
        border-radius: 0;
        min-height: 0;
        font-family: ${config.look.fonts.main}, ${config.look.fonts.nerd};
        font-size: 15.5px;
        color: #${config.look.colors.text};
      }
      #waybar {
        background: #${config.look.colors.dark};
      }
      #workspaces {
        padding: 0 6px 0 0;
      }
      #tray {
        padding: 0 2px 0 5px;
      }
      #network {
        padding: 0 10px 0 4px;
      }
      #network.disconnected,#bluetooth.off {
        color: #424242;
      }
      #bluetooth {
        margin: 0 6px 0 4px;
        font-size: 13.4px;
      }
      #workspaces button {
        padding: 0 3px;
        color: white;
        border-bottom: 3px solid transparent;
        min-width: 20px;
      }
      #workspaces button.visible, #workspaces button.active {
        border-bottom: 3px solid #${config.look.colors.prime};
        background: #${config.look.colors.mid};
      }
      #workspaces button.urgent {
        border-bottom: 3px solid #${config.look.colors.urgent};
      }
      #workspaces button:hover {
        box-shadow: none;
        background: #${config.look.colors.light};
      }
      #scratchpad {
        margin-left: 4px;
      }
      #privacy {
        margin-left: 4px;
      }
      #cpu, #memory, #custom-vram, #mpd, #backlight, #battery, #custom-weather, #custom-weather2, #custom-notifs {
        margin: 0 5px 0 2px;
      }
      #custom-clock-long, #custom-clock-short {
        margin: 0 2px 0 2px;
      }
      #cpu {
        border-bottom: 3px solid #f90000;
      }
      #memory {
        border-bottom: 3px solid #4bffdc;
      }
      #custom-vram {
        border-bottom: 3px solid #33FF00;
      }
      #mpd {
        border-bottom: 3px solid #ffb066;
      }
      #mpd.paused {
        color: #888;
      }
      #custom-clock-long {
        border-bottom: 3px solid #0a6cf5;
      }
      #custom-clock-short {
        border-bottom: 3px solid #0a6cf5;
      }
      #backlight {
        border-bottom: 3px solid #5ffca3;
      }
      #battery {
        border-bottom: 3px solid #fcfc16;
      }
      #custom-weather {
        border-bottom: 3px solid #3823C4;
      }
      #custom-weather2 {
        border-bottom: 3px solid #c75bd3;
      }
      #custom-notifs {
        border-bottom: 3px solid #3823C4;
      }
      #custom-notifs.disabled {
        color: #888;
      }
      #pulseaudio {
        margin-right: 5px;
      }
      #pulseaudio.muted {
        color: #424242;
      }
    '';
  };
}