{
  config,
  pkgs,
  myConfig,
  ...
}: let
  hakase-popup-launch = pkgs.writeShellScriptBin "hakase-popup-launch" ''
    tui_name=$1
    active_popup=$(${pkgs.hyprland}/bin/hyprctl clients -j | ${pkgs.jq}/bin/jq -r '.[] | select(.class | startswith("org.hakase.")) | .class')

    if [[ -z "''${tui_name}" ]]; then
      echo "Usage: $(basename "$0") [tui-name]"
      exit 1
    fi

    popup_checker() {
      if [[ -z "$active_popup" ]]; then
        echo "no_popup"
      elif [[ "$active_popup" != "org.hakase.popup.''${tui_name}" ]]; then
        echo "different"
      else
        echo "same"
      fi
    }

    launch() {
      exec ${pkgs.util-linux}/bin/setsid uwsm-app -- xdg-terminal-exec --app-id=org.hakase.popup."''${tui_name}" -e "''${tui_name}"
    }

    focus() {
      exec ${pkgs.hyprland}/bin/hyprctl dispatch focuswindow "class:org.hakase.popup.''${tui_name}"
    }

    replace() {
      ${pkgs.hyprland}/bin/hyprctl dispatch closewindow "class:''${active_popup}"
      sleep 1
      launch
    }

    run() {
      local status
      status=$(popup_checker)

      if [[ "$status" == "no_popup" ]]; then
        launch
      elif [[ "$status" == "same" ]]; then
        focus
      elif [[ "$status" == "different" ]]; then
        replace
      fi
    }

    run
  '';
in {
  wayland.windowManager.hyprland.settings = {
    exec-once = [
      "uwsm app -- waybar"
    ];
  };
  imports = [
    ./style.nix
  ];

  home.packages = [
    hakase-popup-launch
    pkgs.jq
    pkgs.socat
    pkgs.util-linux
    pkgs.bluetui
    pkgs.impala
    pkgs.wiremix
    pkgs.pamixer
  ];

  programs.waybar = {
    enable = true;
    settings = {
      mainBar = {
        reload_style_on_change = true;
        layer = "top";
        position = "top";
        spacing = 0;
        height = 26;

        modules-left = ["custom/omarchy" "hyprland/workspaces" "hyprland/workspaces#special"];
        modules-center = ["clock" "custom/update" "custom/screenrecording-indicator"];
        modules-right = [
          "group/tray-expander"
          "bluetooth"
          "network"
          "pulseaudio"
          "cpu"
          "battery"
        ];

        "hyprland/workspaces" = {
          on-click = "activate";
          show-special = false;
          all-outputs = true;
          format = "{icon}";
          format-icons = {
            "default" = "";
            "1" = "1";
            "2" = "2";
            "3" = "3";
            "4" = "4";
            "5" = "5";
            "6" = "6";
            "7" = "7";
            "8" = "8";
            "9" = "9";
            "10" = "0";
            "active" = "󱓻";
          };
          persistent-workspaces = {
            "1" = [];
            "2" = [];
            "3" = [];
            "4" = [];
            "5" = [];
          };
        };

        "hyprland/workspaces#special" = {
          on-click = "activate";
          show-special = true;
          special-visible-only = true;
          all-outputs = true;
          ignore-workspaces = ["1" "2" "3" "4" "5" "6" "7" "8" "9" "10"];
          format = "{icon}";
          format-icons = {
            "special:youtube" = "󰗃";
            "special:music" = "󰎆";
            "special:steam" = "󰓓";
          };
        };

        "custom/omarchy" = {
          format = "";
          on-click = "vicinae toggle"; # TODO: add fallback
          on-click-right = "xdg-terminal-exec";
          tooltip-format = "Omarchy Menu\n\nSuper + Alt + Space";
        };

        "cpu" = {
          interval = 5;
          format = "󰍛";
          on-click = "hakase-focus-wrapper btop";
          on-click-right = "xdg-terminal-exec";
        };

        "clock" = {
          format = "{:%H:%M %a %b %d}";
          format-alt = "{:L%d %B W%V %Y}";
          tooltip = false;
          on-click-right = "omarchy-launch-floating-terminal-with-presentation omarchy-tz-select";
        };

        "network" = {
          format-icons = ["󰤯" "󰤟" "󰤢" "󰤥" "󰤨"];
          format = "{icon}";
          format-wifi = "{icon}";
          format-ethernet = "󰀂";
          format-disconnected = "󰤮";
          tooltip-format-wifi = "{essid} ({frequency} GHz)\n⇣{bandwidthDownBytes}  ⇡{bandwidthUpBytes}";
          tooltip-format-ethernet = "⇣{bandwidthDownBytes}  ⇡{bandwidthUpBytes}";
          tooltip-format-disconnected = "Disconnected";
          interval = 3;
          spacing = 1;
          on-click = "hakase-popup-launch impala";
        };

        "battery" = {
          format = "{capacity}% {icon}";
          format-discharging = "{icon}";
          format-charging = "{icon}";
          format-plugged = "";
          format-icons = {
            charging = ["󰢜" "󰂆" "󰂇" "󰂈" "󰢝" "󰂉" "󰢞" "󰂊" "󰂋" "󰂅"];
            default = ["󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹"];
          };
          format-full = "󰂅";
          tooltip-format-discharging = "{power:>1.0f}W↓ {capacity}%";
          tooltip-format-charging = "{power:>1.0f}W↑ {capacity}%";
          interval = 5;
          on-click = "omarchy-menu power";
          states = {
            warning = 20;
            critical = 10;
          };
        };

        "bluetooth" = {
          format = "";
          format-disabled = "󰂲";
          format-connected = "󰂱";
          format-no-controller = "";
          tooltip-format = "Devices connected: {num_connections}";
          on-click = "hakase-popup-launch bluetui";
        };

        "pulseaudio" = {
          format = "{icon}";
          on-click = "hakase-popup-launch wiremix ";
          on-click-right = "pamixer -t";
          tooltip-format = "Playing at {volume}%";
          scroll-step = 5;
          format-muted = "";
          format-icons = {
            default = ["" "" ""];
          };
        };

        "group/tray-expander" = {
          orientation = "inherit";
          drawer = {
            transition-duration = 600;
            children-class = "tray-group-item";
          };
          modules = ["custom/expand-icon" "tray"];
        };

        "custom/expand-icon" = {
          format = "";
          tooltip = false;
        };

        "custom/screenrecording-indicator" = {
          on-click = "omarchy-cmd-screenrecord";
          exec = "$OMARCHY_PATH/default/waybar/indicators/screen-recording.sh";
          signal = 8;
          return-type = "json";
        };

        "tray" = {
          icon-size = 12;
          spacing = 17;
        };
      };
    };
  };
}
