{
  lib,
  config,
  pkgs,
  ...
}: let
  userSettings.theme = "moss";
in {
  options = {
    desktop.hyprland.waybar.enable = lib.mkEnableOption "enable Waybar module";
  };

  config =
    lib.mkIf config.desktop.hyprland.waybar.enable {
      home.packages = [
        pkgs.waybar
      ];

      programs.waybar = {
        enable = true;

        settings = {
          mainBar = {
            layer = "bottom";
            position = "top";
            height = 20;
            margin-top = 2;
            exclusive = true;
            passthrough = false;
            gtk-layer-shell = true;
            reload_style_on_change = true;
            output = [
              "eDP-2"
            ];
            modules-left = ["custom/padd" "custom/l_end" "cpu" "memory" "custom/r_end" "custom/l_end" "idle_inhibitor" "clock" "custom/r_end" "" "custom/padd"];
            modules-center = ["custom/padd" "" "custom/l_end" "hyprland/workspaces" "hyprland/window" "custom/r_end" "custom/padd"];
            modules-right = ["custom/padd" "custom/l_end" "backlight" "network" "bluetooth" "pulseaudio" "pulseaudio#microphone" "custom/r_end" "custom/l_end" "tray" "battery" "custom/r_end" "custom/l_end" "custom/notification" "custom/power" "custom/r_end" "custom/padd"];

            "hyprland/workspaces" = {
              disable-scroll = true;
              #rotate = 1;
              all-outputs = true;
              active-only = false;
              on-click = "activate";
              on-scroll-up = "hyprctl dispatch workspace -1";
              on-scroll-down = "hyprctl dispatch workspace +1";
              persistent-workspaces = {};
            };

            "hyprland/window" = {
              format = "  {}";
              #rotate = ${r_deg};
              separate-outputs = true;
              rewrite = {
                #"${USER}@${set_sysname}:(.*)" = "$1 ";
                "(.*) — Mozilla Firefox" = "$1 󰈹";
                "(.*)Mozilla Firefox" = "Firefox 󰈹";
                "(.*) - Visual Studio Code" = "$1 󰨞";
                "(.*)Visual Studio Code" = "Code 󰨞";
                "(.*) — Dolphin" = "$1 󰉋";
                "(.*)Spotify" = "Spotify 󰓇";
                "(.*)Steam" = "Steam 󰓓";
              };
              max-length = 50;
            };

            "cpu" = {
              interval = 10;
              format = "󰍛 {usage}%";
              #rotate = ${r_deg};
              format-alt = "{icon0}{icon1}{icon2}{icon3}";
              format-icons = ["▁" "▂" "▃" "▄" "▅" "▆" "▇" "█"];
            };

            "memory" = {
              states = {
                "c" = 90; # critical
                "h" = 60; # high
                "m" = 30; # medium
              };
              interval = 30;
              format = "󰾆 {used}GB";
              #rotate = ${r_deg};
              format-m = "󰾅 {used}GB";
              format-h = "󰓅 {used}GB";
              format-c = " {used}GB";
              format-alt = "󰾆 {percentage}%";
              max-length = 10;
              tooltip = true;
              tooltip-format = "󰾆  {percentage}%\n  {used:0.1f}GB/{total:0.1f}GB";
            };

            "bluetooth" = {
              format = "";
              format-disabled = "";
              format-connected = " {num_connections}";
              format-connected-battery = "{icon} {num_connections}";
              format-icons = ["󰥇" "󰤾" "󰤿" "󰥀" "󰥁" "󰥂" "󰥃" "󰥄" "󰥅" "󰥆" "󰥈"];
              tooltip-format = "{controller_alias}\n{num_connections} connected";
              tooltip-format-connected = "{controller_alias}\n{num_connections} connected\n\n{device_enumerate}";
              tooltip-format-enumerate-connected = "{device_alias}";
              tooltip-format-enumerate-connected-battery = "{device_alias}\t{icon} {device_battery_percentage}%";
            };

            "tray" = {
              icon-size = 18;
              rotate = 0;
              spacing = 5;
            };

            "idle_inhibitor" = {
              format = "{icon}";
              rotate = 0;
              format-icons = {
                activated = "󰅶";
                deactivated = "󰛊";
              };
            };

            "clock" = {
              format = "{:%H:%M}";
              rotate = 0;
              format-alt = "{:%R 󰃭 %d·%m·%y}";
              tooltip-format = "<span size='8pt' font='JetBrains Mono'><tt>{calendar}</tt></span>";
              calendar = {
                mode-mon-col = 3;
                format = {
                  months = "<span color='#ffffff'><b>{}</b></span>";
                  weekdays = "<span color='#ffffff'><b>{}</b></span>";
                  today = "<span color='#ffffff'><b>{}</b></span>";
                };
              };
              actions = {
                on-click-right = "mode";
                on-click-forward = "tz_up";
                on-click-backward = "tz_down";
                on-scroll-up = "shift_up";
                on-scroll-down = "shift_down";
              };
            };

            "battery" = {
              interval = 1;
              states = {
                okay = 60;
                warning = 30;
                critical = 15;
                supercritical = 9;
              };
              format = "{icon}{capacity}%";
              rotate = 0;
              format-charging = "{icon}󱐋{capacity}%";
              format-plugged = "{capacity}%";
              format-alt = "{icon}";
              format-icons = ["󰂃" "󰂎" "󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹"];
            };

            "backlight" = {
              device = "intel_backlight";
              rotate = 0;
              format = "{icon} {percent}%";
              format-icons = ["" "" "" "" "" "" "" "" ""];
              on-scroll-up = "${pkgs.swayosd}/bin/swayosd-client --brightness raise";
              on-scroll-down = "${pkgs.swayosd}/bin/swayosd-client --brightness lower";
              min-length = 6;
            };

            "network" = {
              tooltip = true;
              format-wifi = " ";
              rotate = 0;
              format-ethernet = "󰈀 ";
              tooltip-format = "Network: <big><b>{essid}</b></big>\nSignal strength: <b>{signaldBm}dBm ({signalStrength}%)</b>\nFrequency: <b>{frequency}MHz</b>\nInterface: <b>{ifname}</b>\nIP: <b>{ipaddr}/{cidr}</b>\nGateway: <b>{gwaddr}</b>\nNetmask: <b>{netmask}</b>";
              format-linked = "󰈀 {ifname} (No IP)";
              format-disconnected = "󰖪 ";
              tooltip-format-disconnected = "Disconnected";
              format-alt = "<span foreground='#99ffdd'> {bandwidthDownBytes}</span> <span foreground='#ffcc66'> {bandwidthUpBytes}</span>";
              interval = 2;
            };

            "pulseaudio" = {
              format = "{icon} {volume}";
              rotate = 0;
              format-muted = "婢";
              on-click = "${pkgs.pavucontrol}/bin/pavucontrol -t 3";
              on-click-middle = "${pkgs.swayosd}/bin/swayosd-client --output-volume mute-toggle";
              on-scroll-up = "${pkgs.swayosd}/bin/swayosd-client --output-volume 5";
              on-scroll-down = "${pkgs.swayosd}/bin/swayosd-client --output-volume -5";
              tooltip-format = "{icon} {desc} // {volume}%";
              scroll-step = 5;
              format-icons = {
                headphone = " ";
                hands-free = " ";
                headset = " ";
                phone = " ";
                portable = " ";
                car = " ";
                default = ["" "" ""];
              };
            };

            "pulseaudio#microphone" = {
              format = "{format_source}";
              rotate = 0;
              format-source = "";
              format-source-muted = "";
              on-click = "${pkgs.pavucontrol}/bin/pavucontrol -t 4";
              on-click-middle = "${pkgs.swayosd}/bin/swayosd-client --input-volume mute-toggle";
              on-scroll-up = "${pkgs.swayosd}/bin/swayosd-client --input-volume 5";
              on-scroll-down = "${pkgs.swayosd}/bin/swayosd-client --input-volume -5";
              tooltip-format = "{format_source} {source_desc} // {source_volume}%";
              scroll-step = 5;
            };

            "custom/power" = {
              format = " {}";
              rotate = 0;
              exec = "echo ; echo  logout";
              on-click = "${pkgs.wlogout}/bin/wlogout -b 2";
              on-click-right = "${pkgs.wlogout}/bin/wlogout -b 2";
              interval = 86400; # once every day
              tooltip = true;
            };

            "custom/notification" = {
              tooltip = false;
              format = "{} {icon}";
              "format-icons" = {
                notification = "󱅫";
                none = "";
                "dnd-notification" = " ";
                "dnd-none" = "󰂛";
                "inhibited-notification" = " ";
                "inhibited-none" = "";
                "dnd-inhibited-notification" = " ";
                "dnd-inhibited-none" = " ";
              };
              "return-type" = "json";
              "exec-if" = "which swaync-client";
              exec = "swaync-client -swb";
              "on-click" = "sleep 0.1 && swaync-client -t -sw";
              "on-click-right" = "sleep 0.1 && swaync-client -d -sw";
              escape = true;
            };

            "custom/l_end" = {
              format = " ";
              interval = "once";
              tooltip = false;
            };

            "custom/r_end" = {
              format = " ";
              interval = "once";
              tooltip = false;
            };

            "custom/sl_end" = {
              format = " ";
              interval = "once";
              tooltip = false;
            };

            "custom/sr_end" = {
              format = " ";
              interval = "once";
              tooltip = false;
            };

            "custom/rl_end" = {
              format = " ";
              interval = "once";
              tooltip = false;
            };

            "custom/rr_end" = {
              format = " ";
              interval = "once";
              tooltip = false;
            };

            "custom/padd" = {
              format = "  ";
              interval = "once";
              tooltip = false;
            };
          };
        };

        style = ''
          ${
            if (userSettings.theme == "catppuccin-mocha")
            then "
            @define-color main-text rgba(0, 0, 40, 0.9);
            @define-color part-bg rgba(0, 0, 40, 0.5);
            @define-color ws-fg rgba(200, 200, 200, 1);
            @define-color ws-active-fg rgba(235, 235, 235, 1);
            @define-color ws-active-bg rgba(100, 100, 100, 0.2);
            @define-color ws-hover-fg rgba(255, 255, 255, 1);
            @define-color ws-hover-bg rgba(150, 150, 150, 0.3);"
            else if userSettings.theme == "tokyo-night-dark"
            then "
            @define-color main-text rgba(0, 20, 40, 0.9);
            @define-color part-bg rgba(0, 100, 150, 0.5);
            @define-color ws-fg rgba(200, 200, 200, 1);
            @define-color ws-active-fg rgba(235, 235, 235, 1);
            @define-color ws-active-bg rgba(100, 100, 100, 0.2);
            @define-color ws-hover-fg rgba(255, 255, 255, 1);
            @define-color ws-hover-bg rgba(150, 150, 150, 0.3);"
            else if userSettings.theme == "evangelion-blood"
            then "
            @define-color main-text rgba(150, 20, 20, 0.9);
            @define-color part-bg rgba(40, 0, 0, 0.5);
            @define-color ws-fg rgba(200, 200, 200, 1);
            @define-color ws-active-fg rgba(235, 235, 235, 1);
            @define-color ws-active-bg rgba(100, 100, 100, 0.2);
            @define-color ws-hover-fg rgba(255, 255, 255, 1);
            @define-color ws-hover-bg rgba(150, 150, 150, 0.3);"
            else if userSettings.theme == "purple-dawn"
            then "
            @define-color main-text rgba(200, 100, 200, 0.9);
            @define-color part-bg rgba(30, 0, 40, 0.5);
            @define-color ws-fg rgba(200, 100, 200, 1);
            @define-color ws-active-fg rgba(255, 150, 200, 1);
            @define-color ws-active-bg rgba(150, 100, 150, 0.4);
            @define-color ws-hover-fg rgba(255, 175, 225, 1);
            @define-color ws-hover-bg rgba(180, 120, 180, 0.5);"
            else if userSettings.theme == "moss"
            then "
@define-color main-text     rgba(174, 183, 134, 0.90);   /* base06 */
@define-color part-bg       rgba(28,  29,  17,  0.50);   /* base00 */
@define-color ws-fg         rgba(117, 124,  62, 1.00);   /* base05 */
@define-color ws-active-fg  rgba( 28,  29,  17, 1.00);   /* base00 */
@define-color ws-active-bg  rgba( 95, 100,  49, 0.20);   /* base04 */
@define-color ws-hover-fg   rgba(174, 183, 134, 1.00);   /* base06 */
@define-color ws-hover-bg   rgba( 95, 100,  49, 0.30);"
            /*
            base04
            */
            else "
            @define-color main-text rgba(200, 200, 200, 1);
            @define-color part-bg rgba(0, 0, 0, 1);
            @define-color ws-fg rgba(200, 200, 200, 1);
            @define-color ws-active-fg rgba(235, 235, 235, 1);
            @define-color ws-active-bg rgba(100, 100, 100, 0.4);
            @define-color ws-hover-fg rgba(255, 255, 255, 1);
            @define-color ws-hover-bg rgba(150, 150, 150, 0.5);"
          }


          * {
              border: none;
              font-family: "JetBrainsMono Nerd Font";
              font-size: 12px;
              min-height: 0;
              font-weight: bold;
          }

          #backlight,
          #battery,
          #bluetooth,
          #clock,
          #custom-cpuinfo,
          #cpu,
          #custom-gpuinfo,
          #idle_inhibitor,
          #custom-keybindhint,
          #language,
          #memory,
          #mpris,
          #network,
          #custom-notification,
          #custom-power,
          #pulseaudio,
          #custom-spotify,
          #taskbar,
          #custom-theme,
          #tray,
          #custom-updates,
          #custom-wallchange,
          #workspaces,
          #window,
          #custom-l_end,
          #custom-r_end,
          #custom-sl_end,
          #custom-sr_end,
          #custom-rl_end,
          #custom-rr_end {
              color: @main-text;
              background: @part-bg;
              padding-left: 4px;
              padding-right: 4px;
              margin: 0;
              border: 0;
          }

          window#waybar {
              background: transparent;
          }

          #workspaces button {
              box-shadow: none;
              text-shadow: none;
              padding: 0px;
              border-radius: 9px;
              margin-top: 3px;
              margin-bottom: 3px;
              margin-left: 0px;
              padding-left: 3px;
              padding-right: 3px;
              margin-right: 0px;
              color: @ws-fg;
          }

          #workspaces button.active {
              background: @ws-active-bg;
              color: @ws-active-fg;
              margin-left: 3px;
              padding-left: 12px;
              padding-right: 12px;
              margin-right: 3px;
          }

          #workspaces button:hover {
              background: @ws-hover-bg;
              color: @ws-hover-fg;
          }

          tooltip {
              color: @main-text;
              background: @part-bg;
              border-radius: 7px;
              border-width: 0px;
          }

          #custom-r_end {
              border-radius: 0px 20px 20px 0px;
              margin-right: 9px;
              padding-right: 3px;
          }

          #custom-l_end {
              border-radius: 20px 0px 0px 20px;
              margin-left: 9px;
              padding-left: 3px;
          }

          #custom-sr_end {
              border-radius: 0px;
              margin-right: 9px;
              padding-right: 3px;
          }

          #custom-sl_end {
              border-radius: 0px;
              margin-left: 9px;
              padding-left: 3px;
          }

          #custom-rr_end {
              border-radius: 0px 10px 10px 0px;
              margin-right: 9px;
              padding-right: 3px;
          }

          #custom-rl_end {
              border-radius: 10px 0px 0px 10px;
              margin-left: 9px;
              padding-left: 3px;
          }

          #battery {
              color: rgba(0, 150, 0, 0.9);
              padding-left: 0;
          }

          #battery.okay {
              color: rgba(150, 150, 0, 0.9);
          }

          #battery.warning {
              color: rgba(250, 100, 0, 0.9);
          }

          #battery.critical {
              color: rgba(250, 0, 0, 0.9);
          }

          @keyframes supercritical {
              from {
                  background: rgba(100, 0, 0, 1);
              }

              to {
                  background: rgba(250, 220, 0, 1);
              }
          }

          #battery.supercritical:not(.charging) {
              color: #000;
              background: rgba(100, 0, 0, 1);
              animation: supercritical 0.2s infinite alternate;
          }

          #battery.charging {
              color: rgba(0, 150, 0, 0.9);
          }
        '';
      };
    };
}
