{ pkgs, lib, ... }:

let
  theme = import ../theme;
in with theme.colors;
{
  # Dependencies
  home.packages = with pkgs; [
    (pkgs.callPackage ./aozora.nix { })
    (pkgs.callPackage ./polybar-now-playing.nix { })
    lm_sensors
  ];
  services.polybar = {
    enable = true;
    script = "polybar &";
    package = pkgs.polybar.override {
      alsaSupport = true;
    };
    config = {
      "bar/top" = {
        tray-position = "right";
        font-0 = "Noto Sans Mono;2";
        font-1 = "M+ 2p;2";
        background = "${bg0}";
        foreground = "${fg0}";
        width = "100%";
        height = "24pt";
        line-size = "3pt";
        padding-right = 1;
        module-margin = 1;
        separator = "|";
        separator-foreground = "${disabled}";
        cursor-click = "pointer";
        cursor-scroll = "ns-resize";
        modules-left = [
          "xworkspaces"
          "xwindow"
          "now-playing"
        ];
        modules-right = [
          "aozora" 
          "filesystem"
          "pulseaudio"
          "memory"
          "cpu"
          "cputemp"
          "battery"
          "date"
        ];
      };
      "module/xworkspaces" = {
        type = "internal/xworkspaces";
        
        label-active-background = "${bg1}";
        label-active-underline = "${primary}";
        label-active-padding-right = 1;

        label-occupied-padding-right = 1;

        label-urgent-background = "${alert}";
        label-urgent-padding-right = 1;

        label-empty-foreground = "${disabled}";
        label-empty-padding-right = 1;
      };
      "module/xwindow" = {
        type = "internal/xwindow";
        label-maxlen = 32;
      };
      "module/now-playing" = {
        type = "custom/script";
        tail = true;
        format = "<label>";
        exec = "/etc/profiles/per-user/elnu/bin/polybar-now-playing";
        click-right = "kill -USR1 $(pgrep --oldest --parent %pid%)";
      };
      "module/aozora" = {
        type = "custom/script";
        format = "<label>";
        # Wait a few seconds before running to prevent no internet error on first start
        # TODO: make aozora wait blockingly for internet in --bar mode
        exec = "/run/current-system/sw/bin/sleep 3 && /etc/profiles/per-user/elnu/bin/aozora air-quality-in-lakewood-aw-341300 --bar 2> /dev/null";
        interval = 1800;
      };
      "module/filesystem" = {
        type = "internal/fs";

        interval = 25;
        mount-0 = "/";

        label-mounted = "%{F${primary}}%mountpoint%%{F-} %percentage_used%%";
        label-unmounted = "%mountpoint% not mounted";
        label-unmounted-foreground = "${disabled}";
      };
      "module/pulseaudio" = {
        type = "internal/pulseaudio";
        
        format-volume-prefix = "VOL ";
        format-volume-prefix-foreground = "${primary}";

        label-muted = "muted";
        label-muted-foreground = "${disabled}";
      };
      "module/memory" = {
        type = "internal/memory";

        interval = 2;

        format-prefix = "RAM ";
        format-prefix-foreground = "${primary}";
      };
      "module/cpu" = {
        type = "internal/cpu";

        interval = 2;
        
        format-prefix = "CPU ";
        format-prefix-foreground = "${primary}";
      };
      "module/date" = {
        type = "internal/date";

        internal = 1;

        date = "%H:%M";
        date-alt = "%Y-%m-%d %H:%M:%S";

        label-foreground = "${primary}";
      };
    };
  };
}
