{ lib, config, pkgs, ... }:
let
  jsonOutput = name:
    { pre ? "", text ? "", tooltip ? "", alt ? "", class ? "", percentage ? ""
    }:
    "${
      pkgs.writeShellScriptBin "waybar-${name}" ''
        set -euo pipefail
        ${pre}
        ${pkgs.jq}/bin/jq -cn \
          --arg text "${text}" \
          --arg tooltip "${tooltip}" \
          --arg alt "${alt}" \
          --arg class "${class}" \
          --arg percentage "${percentage}" \
          '{text:$text,tooltip:$tooltip,alt:$alt,class:$class,percentage:$percentage}'
      ''
    }/bin/waybar-${name}";
in {
  config = lib.mkIf (config.usercfg.wm == "Wayland") {

    programs.waybar = {
      enable = true;
      package = pkgs.waybar;
      systemd.enable = false;
      style = ''
         * {
          border: none;
          border-radius: ${config.colorScheme.palette.border-radius}pt;
          font-family:'Monospace','Material Design Icons','Openmoji';
          font-size: 13pt;
          color: #${config.colorScheme.palette.base07};
        }

        window#waybar {
          background: transparent;
          padding-top: ${config.colorScheme.palette.gaps-screen}pt;
          padding-bottom: ${config.colorScheme.palette.gaps-screen}pt;
        }

        tooltip {
          background: #${config.colorScheme.palette.base01};
          border-radius: ${config.colorScheme.palette.border-radius}pt;
          border-color: #${config.colorScheme.palette.base03};
          border-width: ${config.colorScheme.palette.border-width}pt;
          border-style: solid;
          font-size: 13pt;
        }

        tooltip label {
          padding: 8px;
        }

        #workspaces {
          font-size: 64pt;

        }

        #workspaces button {
          padding: 2pt;
          padding-left: 4pt;
        }

        #workspaces button.focused {
          color: #${config.colorScheme.palette.base0C};
        }

        #workspaces button.active {
          color: #${config.colorScheme.palette.base0C};
        }

        #workspaces button.urgent {
          color: #${config.colorScheme.palette.base08};
        }

        #workspaces button:hover icon {
          color: #${config.colorScheme.palette.base04};
        }

        #workspaces,
        #network, 
        #pulseaudio,
        #backlight, #battery,
        #cpu,  #memory,  #custom-gpu,
        #tray,
        #custom-powermenu,
        #clock, #clock.date, #clock.time
        { 
          background: #${config.colorScheme.palette.base01};
          margin-bottom: ${config.colorScheme.palette.gaps-window}pt;
          padding-top: 2px;
          padding-left: 2px;
          padding-bottom: 2px;
          border-top-right-radius: 0px;
          border-bottom-right-radius: 0px;
          border-top: ${config.colorScheme.palette.border-width}px solid #${config.colorScheme.palette.base03};
          border-left: ${config.colorScheme.palette.border-width}px solid #${config.colorScheme.palette.base03};
          border-bottom: ${config.colorScheme.palette.border-width}px solid #${config.colorScheme.palette.base03};
        }

        #custom-powermenu {          
          margin-bottom: 0pt;
          border-bottom-left-radius: 0px;
          border-bottom: none;
          padding-bottom: 0px;
        }
      '';
      settings = [{
        "layer" = "top";
        "position" = "right";
        modules-left = [ "hyprland/workspaces" ];
        modules-center = [ ];
        modules-right = [
          "pulseaudio"
          "backlight"
          "cpu"
          "custom/gpu"
          "memory"
          "network"
          "battery"
          "tray"
          "clock"
          "custom/powermenu"
        ];
        "hyprland/workspaces" = {
          "format" = "{icon}";
          "on-click" = "activate";
          "active-only" = "false";
          "format-icons" = {
            "default" = "󰝦";
            "active" = "󰝥";
            "urgent" = "󰺕";
            "focused" = "󰪥";
          };
          "persistent-workspaces" = {
            "1" = [ ];
            "2" = [ ];
            "3" = [ ];
            "4" = [ ];
            "5" = [ ];
            "6" = [ ];
            "7" = [ ];
            "8" = [ ];
            "9" = [ ];
            "10" = [ ];
          };
          "on-scroll-up" = "hyprctl eval \"hl.dispatch(hl.dsp.focus({ workspace = '-1' }))\"";
          "on-scroll-down" = "hyprctl eval \"hl.dispatch(hl.dsp.focus({ workspace = '+1' }))\"";
        };

        "backlight" = {
          "device" = "amd_backlight";
          "on-scroll-up" = "light -A 5";
          "on-scroll-down" = "light -U 5";
          "format" = "{icon}";
          "format-icons" = [ "󱩎" "󱩑" "󱩔" "󰛨" ];
        };
        "pulseaudio" = {
          "scroll-step" = 5;
          "format" = "{icon}";
          "format-muted" = "󰝟";
          "format-icons" = { "default" = [ "󰕿" "󰖀" "󰕾" ]; };
          "on-click" = "amixer -D pulse sset Master toggle -q";
        };
        "battery" = {
          "interval" = 30;
          "states" = {
            "warning" = 20;
            "critical" = 10;
          };
          "format" = "{icon}";
          "format-icons" = [ "󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹" ];
          "format-full" = "{icon}";
          "format-charging" = "{icon}󱐋";
        };
        "clock" = {
          "interval" = 30;
          "format" = ''
            {:<b>%H</b>
            %M}'';
          "tooltip-format" = ''
            <big><b>{:%d/%m/%Y}</b></big>
            <tt><small>{calendar}</small></tt>'';
          "calendar" = { "months" = ""; };
        };
        "memory" = {
          "interval" = 5;
          "format" = ''
            󰍛
            {icon}'';
          "format-icons" = [ "󰝦" "󰪞" "󰪟" "󰪠" "󰪡" "󰪢" "󰪣" "󰪤" "󰪥" ];
          "states" = { "warning" = 85; };
        };
        "cpu" = {
          "interval" = 1;
          "format-icons" = [ "󰝦" "󰪞" "󰪟" "󰪠" "󰪡" "󰪢" "󰪣" "󰪤" "󰪥" ];
          "format" = ''
            󱛠
            {icon}'';
        };
        "custom/gpu" = {
          "exec" = jsonOutput "gpu" {
            tooltip = "...";
            percentage = ''$(${lib.getExe pkgs.custom.amdgpu_top} -J -n 1 | ${pkgs.jq}/bin/jq -r '.devices[0].GRBM2."Command Processor -  Graphics".value')'';
          };
          "return-type" = "json";
          "interval" = 5;
          "format-icons" = [ "󰝦" "󰪞" "󰪟" "󰪠" "󰪡" "󰪢" "󰪣" "󰪤" "󰪥" ];
          "format" = ''
            󰇞
            {icon}'';
        };
        "network" = {
          "format-disconnected" = "󰯡";
          "format-ethernet" = "󰀂";
          "format-linked" = "󰖪";
          "format-wifi" = "󰖩";
          "interval" = 1;
          "tooltip-format-disconnected" = "Disconnected";
          "tooltip-format-ethernet" = "{ifname} @ {ipaddr}";
          "tooltip-format-linked" = "{essid}: {ifname} @ -";
          "tooltip-format-wifi" = "{essid}: {ifname} @ {ipaddr}";
        };
        "temperature" = {
          # "hwmon-path"= "${env:HWMON_PATH}";
          #"critical-threshold"= 80;
          "format" = "󰔏 {temperatureC}°C";
        };
        "custom/powermenu" = {
          "format" = "{icon}";
          "format-icons" = [ "󰐥" ];
          "on-click" = "eww-open-on-current-screen powermenu";
          "tooltip" = false;
        };
        "tray" = {
          "icon-size" = 15;
          "spacing" = 5;
        };
      }];
    };
  };
}
