{ pkgs, lib, osConfig, ... }:
let
  flakestat = pkgs.writeShellApplication {
    name = "flakestat";
    runtimeInputs = with pkgs; [
      git
      jq
    ];
    text = builtins.readFile ./flakestat.sh;
  };
  arrhist = pkgs.stdenv.mkDerivation {
    name = "arrhist";
    propagatedBuildInputs = [
      (pkgs.python3.withPackages (pythonPackages: with pythonPackages; [
        requests
      ]))
    ];
    src = ./arrhist.py;
    dontUnpack = true;
    installPhase = "install -Dm755 ${./arrhist.py} $out/bin/arrhist";
  };
  withArrhist = if (osConfig.system.name == "gunter") then true else false;
  withBattery = if (osConfig.system.name == "magicman") then true else false;
in
{
  sops.secrets."sonarr_base_url" = { };
  sops.secrets."sonarr_api_key" = { };
  sops.secrets."radarr_base_url" = { };
  sops.secrets."radarr_api_key" = { };

  xdg.configFile."waybar/macchiato.css" = {
    source = pkgs.fetchFromGitHub
      {
        owner = "catppuccin";
        repo = "waybar";
        rev = "f74ab1eecf2dcaf22569b396eed53b2b2fbe8aff";
        sha256 = "WLJMA2X20E5PCPg0ZPtSop0bfmu+pLImP9t8A8V4QK8=";
      } + "/themes/macchiato.css";
  };

  programs.waybar = {
    enable = true;
    systemd = {
      enable = false; # disable it,autostart it in hyprland conf
      target = "graphical-session.target";
    };
    style = ''
      @import "macchiato.css";
      * {
        border: none;
        font-family: "JetbrainsMono Nerd Font";
        font-size: 15px;
        transition-property: background-color;
        transition-duration: 0.5s;
        color: @text;
        border-radius: 10px;
        min-height: 10px;
      }

      window#waybar {
        background-color: @surface2;
        border-radius: 0px;
      }

      window#waybar.hidden {
        opacity: 0.2;
      }

      #window {
        margin-top: 6px;
        padding-left: 10px;
        padding-right: 10px;
        border-radius: 0px;
        transition: none;
        color: transparent;
        background-color: @surface2;
      }

      @keyframes blink_red {
        to {
          background-color: @red;
        }
      }
      .warning,
      .critical,
      .urgent {
        animation-name: blink_red;
        animation-duration: 1s;
        animation-timing-function: linear;
        animation-iteration-count: infinite;
        animation-direction: alternate;
      }
      #workspaces {
        padding-left: 12px;
        padding-right: 12px;
        background-color: @base;
      }
      #workspaces button {
        padding-left: 12px;
        padding-right: 12px;
      }
      #workspaces button.active {
        background-color: @surface2;
      }
      #workspaces button.urgent {
        color: alpha(@red, 0.4);
      }
      #workspaces button:hover {
        background-color: @surface2;
      }
      tooltip {
        background-color: @base;
      }
      tooltip label {
        color: @text;
      }
      #custom-launcher {
        font-size: 15px;
        padding-left: 5px;
        padding-right: 12px;
        background-color: @base;
      }
      .modules-right {
        background-color: @base;
        border-radius: 10px 0px 0px 10px;
      }
      .modules-left {
        background-color: @base;
        border-radius: 0px 10px 10px 0px;
      }
      #mode,
      #clock,
      #memory,
      #temperature,
      #cpu,
      #mpd,
      #custom-wall,
      #temperature,
      #backlight,
      #pulseaudio,
      #network,
      #battery,
      #custom-powermenu,
      #custom-flakestat,
      #custom-arrhist {
        padding-left: 12px;
        padding-right: 12px;
      }
      #battery.charging,
      #battery.full,
      #battery.discharging {
        color: @maroon;
      }
      #battery.critical:not(.charging) {
        color: @red;
      }
    '';
    settings = [
      (
        let
          volInterval = if (osConfig.system.name == "gunter") then "5" else "1";
        in
        {
          "layer" = "top";
          "position" = "top";
          modules-left = [ "custom/flakestat" ];
          modules-center = [ "hyprland/workspaces" ];
          modules-right =
            [
              (lib.mkIf (withArrhist) "custom/arrhist")
              "pulseaudio"
              "pulseaudio#microphone"
              "memory"
              "cpu"
              (lib.mkIf (withBattery) "battery")
              "clock"
              "tray"
            ];
          "hyprland/workspaces" = {
            "format" = "{name}";
            "on-click" = "activate";
            "on-scroll-up" = "hyprctl dispatch workspace e+1";
            "on-scroll-down" = "hyprctl dispatch workspace e-1";
          };
          "pulseaudio" = {
            "scroll-step" = 5;
            "format" = "{icon} {volume}%";
            "format-muted" = "";
            "format-icons" = { "default" = [ "" "" "" ]; };
            "on-scroll-up" = "pamixer -i ${volInterval}";
            "on-scroll-down" = "pamixer -d ${volInterval}";
            "on-click" = "pamixer -t";
          };
          "pulseaudio#microphone" = {
            "format" = "{format_source}";
            "format-source" = " {volume}%";
            "format-source-muted" = "";
            "on-click" = "pamixer --default-source -t";
            "on-scroll-up" = "pamixer --default-source -i ${volInterval}";
            "on-scroll-down" = "pamixer --default-source -d ${volInterval}";
            "scroll-step" = 5;
            "on-click-right" = "pavucontrol";
          };
          "clock" = {
            "interval" = 1;
            "format" = "{:%H:%M %A %b %d}";
            "tooltip" = true;
          };
          "memory" = {
            "interval" = 3;
            "format" = " {percentage}%";
            "states" = { "warning" = 85; };
          };
          "cpu" = {
            "interval" = 3;
            "format" = " {usage}%";
          };
          "tray" = {
            "icon-size" = 15;
            "spacing" = 6;
          };
          "battery" = {
            "interval" = 60;
            "states" = {
              "warning" = 20;
              "critical" = 5;
            };
            "format" = "{icon} {capacity}%";
            "format-icons" = [
              ""
              ""
              ""
              ""
              ""
            ];
          };
          "custom/flakestat" = {
            "exec" = "${flakestat}/bin/flakestat";
            "format" = "  {}";
            "return-type" = "json";
            "interval" = 600;
          };
        } // lib.optionalAttrs (withArrhist) {
          "custom/arrhist" = {
            "exec" = "${arrhist}/bin/arrhist";
            "return-type" = "json";
            "interval" = 600;
          };
        }
      )
    ];
  };
}