# !!!!!! Shamelessly stolen from Niksingh710's config!
# https://github.com/niksingh710/gdots/blob/d97c544a9d73a34d12770ba20b6b80d14e964aa1/.config/waybar/bars/top.jsonc
{ niri-gaps ? 8
, pkgs ? import <nixpkgs> {}
, lib ? pkgs.lib
}:

let
  cputemp = pkgs.writeShellApplication {
    name = "cputemp";
    runtimeInputs = with pkgs; [
      lm_sensors
    ];
    text = builtins.readFile ./cputemp;
  };
  cycle-notifications = pkgs.writeShellApplication {
    name = "cycle-notifications";
    runtimeInputs = with pkgs; [
      swaynotificationcenter
    ];
    text = ''
      ${pkgs.babashka}/bin/bb -cp ${./.} -m cycle-notifications -- "$@"
    '';
  };
in {
  layer = "top";
  # Left margin will be provided by Niri's gaps.
  margin =
    let i = builtins.toString niri-gaps;
    in "${i} ${i} ${i} 0";

  modules-left = [
    # "custom/updates"
    "niri/workspaces"
    "group/info"
  ];
  modules-right = [
    "custom/recorder"
    "privacy"
    "group/brightness"
    "group/sound"
    "group/together"
    # "group/cnoti"
    "tray"
    # "group/power"
  ];

  backlight = {
    device = "intel_backlight";
    format = "{icon}";
    format-icons =
      [ "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ];
    on-scroll-down = "brightnessctl s 5%-";
    on-scroll-up = "brightnessctl s +5%";
    smooth-scrolling-threshold = 1;
    tooltip = true;
    tooltip-format = "Brightness: {percent}% ";
  };
  "backlight/slider" = {
    device = "intel_backlight";
    max = 100;
    min = 5;
    orientation = "vertical";
  };
  battery = {
    format = "{icon}";
    format-charging = "<b>{icon} </b>";
    format-full = "<span color='#82A55F'><b>{icon}</b></span>";
    format-icons = [ "󰁻" "󰁼" "󰁾" "󰂀" "󰂂" "󰁹" ];
    rotate = 270;
    states = {
      critical = 15;
      good = 95;
      warning = 30;
    };
    tooltip-format = "{timeTo} {capacity} % | {power} W";
  };
  bluetooth = {
    format-connected = "<b></b>";
    format-disabled = "";
    format-off = "󰂲";
    format-on = "";
    on-click = "rofi-bluetooth -config ~/.config/rofi/menu.d/network.rasi -i";
    tooltip-format = ''
      {controller_alias}	{controller_address}

      {num_connections} connected'';
    tooltip-format-connected = ''
      {controller_alias}	{controller_address}

      {num_connections} connected

      {device_enumerate}'';
    tooltip-format-enumerate-connected = "{device_alias}	{device_address}";
    tooltip-format-enumerate-connected-battery =
      "{device_alias}	{device_address}	{device_battery_percentage}%";
  };
  "bluetooth#status" = {
    format-connected = "<b>{num_connections}</b>";
    format-connected-battery =
      "<small><b>{device_battery_percentage}%</b></small>";
    format-disabled = "";
    format-off = "";
    format-on = "";
    on-click = "rofi-bluetooth -config ~/.config/rofi/menu.d/network.rasi -i";
    tooltip-format = ''
      {controller_alias}	{controller_address}

      {num_connections} connected'';
    tooltip-format-connected = ''
      {controller_alias}	{controller_address}

      {num_connections} connected

      {device_enumerate}'';
    tooltip-format-enumerate-connected = "{device_alias}	{device_address}";
    tooltip-format-enumerate-connected-battery =
      "{device_alias}	{device_address}	{device_battery_percentage}%";
  };
  clock = {
    calendar = {
      format = { today = "<span color='#a6e3a1'><b><u>{}</u></b></span>"; };
      mode = "month";
      mode-mon-col = 3;
      on-click-right = "mode";
      on-scroll = 1;
      weeks-pos = "right";
    };
    format = ''
      {:%H
      %M}'';
    tooltip-format = "<tt><small>{calendar}</small></tt>";
  };
  cpu = {
    format = "<b>{usage}󱉸</b>";
    on-click = "foot btop";
  };
  "custom/colorpicker" = {
    exec = "colorpicker -j";
    format = "{}";
    interval = "once";
    on-click = "sleep 1 && colorpicker";
    return-type = "json";
    signal = 1;
  };
  "custom/cpu-icon" = {
    format = "󰻠";
    tooltip = false;
  };
  "custom/cputemp" = {
    exec = "${cputemp}/bin/cputemp";
    format = "{}";
    interval = 10;
    return-type = "json";
  };
  "custom/dmark" = {
    format = "";
    tooltip = false;
  };
  "custom/github" = {
    exec = "$HOME/.config/waybar/bin/github.sh";
    format = "{}";
    interval = 3600;
    on-click =
      "xdg-open https://github.com/notifications;pkill -RTMIN+9 waybar";
    return-type = "json";
    signal = 9;
  };
  "custom/hotspot" = {
    exec = "~/.config/waybar/bin/hotspot";
    format = "{} ";
    interval = 5;
    on-click = "hash wihotspot && wihotspot";
    return-type = "json";
  };
  "custom/hyprkill" = {
    exec = ''
      echo '
      Kill clients using hyrpctl kill'
    '';
    format = "{}";
    interval = "once";
    on-click = "sleep 1 && hyprctl kill";
  };
  "custom/hyprshade" = {
    exec = "toggle-hyprshade status";
    format = "{}";
    on-click = "toggle-hyprshade";
    return-type = "json";
    signal = 11;
    tooltip = true;
  };
  "custom/mark" = {
    format = "";
    tooltip = false;
  };
  "custom/notifications" = {
    exec = "${cycle-notifications}/bin/cycle-notifications";
    format = "<b>{}</b> ";
    interval = "once";
    on-click = "swaync-client -t";
    on-click-right = "~/scratch/waybar/cycle-notifications toggle";
    return-type = "json";
    signal = 2;
  };
  "custom/recorder" = {
    exec = "echo ''";
    exec-if = "pgrep 'wl-screenrec'";
    format = "{}";
    interval = "once";
    on-click = "recorder";
    signal = 4;
    tooltip = "false";
  };
  "custom/updates" = {
    exec = "~/.config/waybar/bin/updatecheck";
    exec-if = "exit 0";
    format = "{}";
    interval = 10800;
    return-type = "json";
    signal = 8;
  };
  "custom/vpn" = {
    exec = "~/.config/waybar/bin/vpn";
    format = "{} ";
    interval = 5;
    return-type = "json";
  };
  "custom/weather" = {
    exec = ''
      wttrbar --custom-indicator '{ICON}
        <b>{temp_C}</b>' --location noida
    '';
    format = "{}";
    interval = 3600;
    return-type = "json";
    tooltip = true;
  };
  disk = {
    format = ''
      <b> 󰋊 
      {percentage_used}󱉸</b>
    '';
    interval = 600;
    path = "/";
  };
  "group/audio" = {
    drawer = {
      transition-duration = 500;
      transition-left-to-right = false;
    };
    modules = [ "pulseaudio" "pulseaudio#mic" "pulseaudio/slider" ];
    orientation = "inherit";
  };
  "group/bluetooth" = {
    drawer = {
      transition-duration = 500;
      transition-left-to-right = true;
    };
    modules = [ "bluetooth" "bluetooth#status" ];
    orientation = "inherit";
  };
  "group/brightness" = {
    drawer = {
      transition-duration = 500;
      transition-left-to-right = false;
    };
    modules = [
      "backlight"
      "backlight/slider"
    ];
    orientation = "inherit";
  };
  "group/cnoti" = {
    modules = [ "custom/github" ];
    orientation = "inherit";
  };
  "group/connection" = {
    modules =
      [ "custom/vpn" "custom/hotspot" "group/network" "group/bluetooth" ];
    orientation = "inherit";
  };
  "group/gcpu" = {
    modules = [ "custom/cpu-icon" "custom/cputemp" "cpu" ];
    orientation = "inherit";
  };
  "group/info" = {
    drawer = {
      transition-duration = 500;
      transition-left-to-right = false;
    };
    modules = [ "custom/dmark" "group/gcpu" "memory" "disk" ];
    orientation = "inherit";
  };
  "group/network" = {
    drawer = {
      transition-duration = 500;
      transition-left-to-right = true;
    };
    modules = [ "network" "network#speed" ];
    orientation = "inherit";
  };
  "group/power" = {
    drawer = {
      transition-duration = 500;
      transition-left-to-right = false;
    };
    modules = [ "battery" "power-profiles-daemon" ];
    orientation = "inherit";
  };
  "group/sound" = {
    modules = [ "group/audio" "custom/notifications" ];
    orientation = "inherit";
  };
  "group/together" = {
    modules = [
      "group/utils"
      "clock"
    ];
    orientation = "inherit";
  };
  "group/utils" = {
    drawer = {
      transition-duration = 500;
      transition-left-to-right = true;
    };
    modules = [
      "custom/mark"
      # "custom/weather"
      # "custom/colorpicker"
      # "custom/hyprshade"
      "idle_inhibitor"
      "custom/hyprkill"
    ];
    orientation = "inherit";
  };
  "hyprland/submap" = {
    format = "<b>󰇘</b>";
    max-length = 8;
    tooltip = true;
  };
  "hyprland/workspaces" = {
    all-outputs = true;
    format = "{icon}";
    format-icons = {
      "1" = "१";
      "10" = "०";
      "2" = "२";
      "3" = "३";
      "4" = "४";
      "5" = "५";
      "6" = "६";
      "7" = "७";
      "8" = "८";
      "9" = "९";
    };
    on-click = "activate";
  };
  idle_inhibitor = {
    format = "{icon}";
    format-icons = {
      activated = "󱎴";
      deactivated = "󰍹";
    };
    tooltip-format-activated = "Idle Inhibitor is active";
    tooltip-format-deactivated = "Idle Inhibitor is not active";
  };
  memory = {
    format = ''
      <b>  
      {:2}󱉸</b>'';
  };
  network = {
    format = "{icon}";
    format-disconnected = "󰖪";
    format-ethernet = "󰈀";
    format-icons = {
      disconnected = [ "󰖪" ];
      ethernet = [ "󰈀" ];
      wifi = [ "󰤨" ];
    };
    format-linked = "󰈁";
    format-wifi = "󰤨";
    on-click =
      "pgrep -x rofi &>/dev/null && notify-send rofi || networkmanager_dmenu";
    tooltip = false;
  };
  "network#speed" = {
    format = " {bandwidthDownBits} ";
    interval = 5;
    on-click =
      "pgrep -x rofi &>/dev/null && notify-send rofi || networkmanager_dmenu";
    rotate = 90;
    tooltip = true;
    tooltip-format = "{ipaddr}";
    tooltip-format-disconnected = "Not Connected to any type of Network";
    tooltip-format-ethernet = ''
      {ifname} 󰈀 
      {ipaddr} | {frequency} MHz{icon} '';
    tooltip-format-wifi = ''
      {essid} ({signalStrength}%)   
      {ipaddr} | {frequency} MHz{icon} '';
  };
  "niri/workspaces" = {
    all-outputs = true;
    format = "{icon}";
    format-icons = {
      "1" = "१";
      "10" = "०";
      "2" = "२";
      "3" = "३";
      "4" = "४";
      "5" = "५";
      "6" = "६";
      "7" = "७";
      "8" = "८";
      "9" = "९";
    };
    on-click = "activate";
  };
  position = "right";
  power-profiles-daemon = {
    format = "{icon}";
    format-icons = {
      balanced = "<span><small> </small></span>";
      default = "";
      performance = "<span color='#B37F34'><small></small></span>";
      power-saver = "<span color='#a6e3a1'><small></small></span>";
    };
    tooltip = true;
    tooltip-format = ''
      Power profile: {profile}
      Driver: {driver}'';
  };
  privacy = {
    icon-size = 14;
    icon-spacing = 4;
    modules = [{
      tooltip = true;
      tooltip-icon-size = 24;
      type = "screenshare";
    }];
    orientation = "vertical";
    transition-duration = 250;
  };
  pulseaudio = {
    format = "{icon}";
    format-bluetooth = "{icon}";
    format-icons = {
      car = " ";
      default = [ "󰕿" "󰖀" "󰕾" ];
      handsfree = "󰋌";
      headphones = "󰋌";
      headset = "󰋌";
      phone = "";
      portable = "";
    };
    format-muted = "󰖁";
    on-click = "${pkgs.pavucontrol}/bin/pavucontrol";
    on-click-right = "volume mute";
    on-scroll-down = "pactl set-sink-volume @DEFAULT_SINK@ -5%";
    on-scroll-up = "pactl set-sink-volume @DEFAULT_SINK@ +5%";
    smooth-scrolling-threshold = 1;
    tooltip-format = "{volume}% {icon} | {desc}";
  };
  "pulseaudio#mic" = {
    format = "{format_source}";
    format-source = "";
    format-source-muted = "";
    on-click = "pactl set-source-mute 0 toggle";
    on-scroll-down = "pactl set-source-volume 0 -1%";
    on-scroll-up = "pactl set-source-volume 0 +1%";
    tooltip-format = "{volume}% {format_source} ";
  };
  "pulseaudio/slider" = {
    max = 140;
    min = 0;
    orientation = "vertical";
  };
  reload_style_on_change = true;
  tray = {
    icon-size = 18;
    spacing = 10;
  };
}
