{
  lib,
  config,
  pkgs,
  ...
}:
let
  inherit (lib) mkEnableOption mkIf getExe;

  cfg = config.marleyos.services.polybar;
  hasXorg = config.xsession.enable;
  xrandr = getExe pkgs.xorg.xrandr;
  grep = getExe pkgs.gnugrep;
  cut = "${pkgs.coreutils}/bin/cut";
in
{
  options.marleyos.services.polybar.enable = mkEnableOption "polybar";

  config = mkIf (cfg.enable && hasXorg) {
    services.polybar = {
      enable = true;
      package = config.lib.nixGL.wrap pkgs.polybar;

      script = # bash
        ''
          for m in $(${xrandr} --query | ${grep} " connected" | ${cut} -d" " -f1); do
            bar="main"

            if [[ "$m" == "DP-0" ]]; then
              bar="tray"
            fi

            MONITOR="$m" polybar -q "$bar" &
          done
        '';

      # colorblock theme from
      # https://github.com/adi1090x/polybar-themes
      settings =
        let
          color = rec {
            background = "#26232f";
            foreground = background;
            foreground-alt = "#c8c8cb";
            alpha = "#00000000";
            shade1 = "#d6adb5";
            shade2 = "#d7bdc3";
            dark = "#0a0a0a";
          };

          fmt = {
            prefix.font = 2;
            padding = 2;
          };
          pref = {
            font = 2;
            padding = 2;
          };

          hasRofi = config.marleyos.programs.rofi.enable;
          cfgHome = config.xdg.configHome;
        in
        {
          # Global Settings - - - - - - - - - - - - - - - - - - - - - - - - - -
          "settings" = {
            screenchange.reload = true;
            compositing = {
              background = "source";
              foreground = "over";
              overline = "over";
              underline = "over";
              border = "over";
            };
            pseudo.transparency = false;
          };
          "global/wm" = {
            margin.bottom = 0;
          };

          # Bars - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          "bar/main" = {
            monitor.text = "\${env:MONITOR:}";
            monitor.strict = false;
            override.redirect = true;
            bottom = false;
            fixed.center = true;
            width = "99%";
            height = 40;
            offset.x = "0.5%";
            offset.y = "1%";
            background = color.alpha;
            inherit (color) foreground;
            radius.top = 0.0;
            radius.bottom = 0.0;
            underline.size = 2;
            underline.color = color.foreground;
            border.size = 0;
            border.color = color.background;
            padding = 0;
            module.margin.left = 0;
            module.margin.right = 0;
            font =
              let
                mkFt = s: "Maple Mono NF:pixelsize=${toString s};4";
              in
              [
                (mkFt 10)
                (mkFt 15)
              ];
            modules.left = "launcher sep workspaces sep title sep mpd";
            modules.right = "filesystem sep cpu memory pulseaudio network date sep sysmenu";
            dim.value = 1.0;
            wm.restack = "i3";
            enable.ipc = true;
            scroll.up = "i3-msg workspace next_on_output";
            scroll.down = "i3-msg workspace prev_on_output";
          };

          "bar/tray" = {
            "inherit" = "bar/main";
            modules.right = "tray filesystem sep cpu memory pulseaudio network date sep sysmenu";
          };

          # Modules - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          "module/sep" = {
            type = "custom/text";
            format = "  ";
            content.background = color.alpha;
            content-foreground = color.alpha;
          };

          "module/launcher" = {
            type = "custom/text";
            content = {
              text = "";
              inherit (color) background;
              foreground = color.shade2;
              padding = 2;
            };
            click.left = mkIf hasRofi "${cfgHome}/rofi/launchers/type-1/launcher.sh &";
            click.right = mkIf hasRofi "${cfgHome}/rofi/applets/bin/screenshot.sh &";
          };

          "module/workspaces" = {
            type = "internal/xworkspaces";
            pin.workspaces = true;
            enable.click = true;
            enable.scroll = true;
            icon.text = [
              "1;1"
              "2;2"
              "3;3"
              "4;4"
              "5;5"
              "6;6"
              "7;7"
              "8;8"
              "9;9"
              "10;10"
            ];
            icon.default = 0;
            format = {
              text = "<label-state>";
              inherit (color) background;
              inherit (color) foreground;
            };
            label = {
              monitor = "%name%";
              active = {
                text = "%icon%";
                background = color.shade2;
                foreground = color.dark;
                padding = 2;
              };
              occupied = {
                text = "%icon%";
                inherit (color) background;
                foreground = color.shade2;
                padding = 2;
              };
              urgent = {
                text = "%icon%";
                inherit (color) background;
                foreground = "#cc6666";
                padding = 2;
              };
              empty = {
                text = "%icon%";
                inherit (color) background;
                foreground = color.foreground-alt;
                padding = 2;
              };
            };
          };

          "module/title" = {
            type = "internal/xwindow";
            format.text = "<label>";
            format.prefix = pref // {
              text = "﯑";
              background = color.shade2;
              foreground = color.dark;
            };
            label = {
              text = "%title%";
              maxlen = 30;
              inherit (color) background;
              foreground = "${color.foreground-alt}";
              padding = 2;
              empty = {
                text = "Desktop";
                inherit (color) background;
                foreground = "${color.foreground-alt}";
                padding = 2;
              };
            };
          };

          "module/mpd" = {
            type = "internal/mpd";
            interval = 1;
            format.online = "<icon-prev><toggle><icon-next><label-song>";
            format.offline = {
              text = "<label-offline>";
              prefix = pref // {
                text = "";
                background = color.shade1;
                inherit (color) foreground;
              };
            };
            label = {
              song = {
                text = "%artist% - %title%";
                maxlen = 25;
                ellipsis = true;
                inherit (color) background;
                foreground = color.foreground-alt;
                padding = 2;
              };
              time = "%elapsed / %total%";
              offline = {
                text = "Offline";
                inherit (color) background;
                foreground = color.foreground-alt;
                padding = 2;
              };
            };
            icon =
              let
                mkIcon =
                  i:
                  pref
                  // {
                    text = i;
                    foreground = color.dark;
                    background = color.shade1;
                  };
              in
              {
                play = mkIcon "契";
                pause = mkIcon "";
                stop = "";
                prev = mkIcon " 玲";
                next = mkIcon "怜 ";
                seekb = "";
                seekf = "";
                random = "";
                repeat = "";
                repeatone = "";
                single = "";
                consume = "";
              };
            toggle = {
              on.foreground = color.foreground;
              off.foreground = color.background;
            };
          };

          "module/filesystem" = {
            type = "internal/fs";
            mount = [
              "/"
              "/mnt/babeshare/marley"
              "/mnt/babeshare/babez"
            ];
            interval = 30;
            fixed.values = true;
            format = {
              mounted = fmt // {
                text = "<label-mounted>";
                prefix.text = "";
                background = color.shade2;
              };
              unmounted = fmt // {
                text = "<label-unmounted>";
                prefix.text = "";
                background = color.shade2;
              };
            };
            label = {
              mounted = " %free%";
              unmounted = " %mountpoint: not mounted";
            };
          };

          "module/cpu" = {
            type = "internal/cpu";
            interval = 1;
            format = fmt // {
              text = "<label>";
              prefix.text = "";
              background = color.shade2;
              inherit (color) foreground;
            };
            label = " %percentage%%";
          };

          "module/memory" = {
            type = "internal/memory";
            interval = 1;
            format = fmt // {
              text = "<label>";
              prefix.text = "";
              background = color.shade1;
            };
            label = " %mb_used%";
          };

          "module/pulseaudio" = {
            type = "internal/pulseaudio";
            sink = "alsa_output.pci-0000_03_00.6.analog-stereo";
            use.ui.max = false;
            interval = 5;
            format = {
              volume = {
                text = "<ramp-volume> <label-volume>";
                background = color.shade2;
                padding = 2;
              };
              muted = fmt // {
                text = "<label-muted>";
                prefix.text = "";
                background = color.shade2;
              };
            };
            ramp.volume = {
              text = [
                ""
                ""
                ""
              ];
              font = 2;
            };
          };

          "module/network" = {
            type = "internal/network";
            interface = "enp4s0";
            interval = 1.0;
            accumulate.stats = true;
            unknown.as.up = true;
            format = {
              connected = fmt // {
                text = "<label-connected>";
                prefix.text = "直";
                background = color.shade2;
              };
              disconnected = fmt // {
                text = "<label-disconnected>";
                prefix.text = "睊";
                background = color.shade2;
              };
            };
            label = {
              connected = "%{A1:networkmanager_dmenu &:} %netspeed%%{A}";
              disconnected = "%{A1:networkmanager_dmenu &:} Offline%{A}";
            };
          };

          "module/date" = {
            type = "internal/date";
            interval = 1.0;
            time.text = " %I:%M %p";
            time.alt = " %a, %d %b %Y";
            format = fmt // {
              text = "<label>";
              prefix.text = "";
              background = color.shade1;
              inherit (color) foreground;
            };
            label = "%time%";
          };

          "module/sysmenu" = {
            type = "custom/text";
            format = {
              text = "";
              inherit (color) background;
              foreground = color.shade2;
              padding = 2;
            };
            click.left = mkIf hasRofi "${cfgHome}/rofi/powermenu/type-1/powermenu.sh &";
          };

          "module/tray" = {
            type = "internal/tray";
            tray = {
              size = 16;
              background = color.shade2;
              spacing = "5px";
            };
            format = {
              background = color.shade2;
              padding = 2;
            };
          };
        };
    };
  };
}
