#
# Bar
#

{ config, lib, user, pkgs, ...}:

{
  environment.systemPackages = with pkgs; [
    waybar
  ];

  home-manager.users.${user} = {                           # Home-manager waybar config
    programs.waybar = {
      enable = true;
      #systemd = {
      #  enable = true;
      #  target = "sway-session.target";                     # Needed for waybar to start automatically
      #};

      style = ''
        * {
            border: none;
            border-radius: 0;
            font-family: FiraCode Nerd Font Mono;
            font-size: 11px;
            min-height: 0;
        }
        
        window#waybar {
            background: rgba(43, 48, 59, 0.3);
            border-bottom: 3px solid transparent;
            color: white;
        }
        
        #workspaces button {
            padding: 0 5px;
            margin: 0 3px;
            color: white;
            border-bottom: 3px solid transparent;
        }
        
        #workspaces button.focused {
            background: rgba(100, 114, 125, 0.2);
            border-bottom: 2px solid gray;
        }
        
        #mode {
            background: transparent;
            border-bottom: 2px solid gray;
        }
        #clock, #battery, #cpu, #memory, #network, #pulseaudio, #temperature, #backlight, #idle_inhibitor, #custom-spotify {
            color: white;
            padding: 0 6px;
            margin: 0 5px;
        }
        
        #custom-vkeyboard, #custom-appkill {
            padding: 0px 50px;
            margin: 0px 0px;
            font-size: 20px;
        }
        
        #clock {
            background-color: transparent;
            color: white;
        }
        
        #battery {
            color: white;
        }
        
        #battery.charging {
            color: white;
        }
        
        @keyframes blink {
            to {
                background-color: rgba(1, 1, 1, 0);
            }
        }
        #battery.warning:not(.charging) {
            background: #f53c3c;
            color: white;
            animation-name: blink;
            animation-duration: 0.5s;
            animation-timing-function: linear;
            animation-iteration-count: infinite;
            animation-direction: alternate;
        }
        
        #cpu {
            background: transparent;
            border-bottom: 2px solid yellow;
            color: white;
        }
        
        #memory {
            background: transparent;
            border-bottom: 2px solid green;
            color: white;
        }
        
        #network {
            background: transparent;
            color: white
        }
        
        #network.disconnected {
            background: transparent;
            color: crimson;
        }
        
        #pulseaudio {
            background: transparent;
            color: white;
        }

        #pulseaudio.muted {
            border-bottom: 2px solid red;
        }
        
        #tray {
            background-color: transparent;
        }
        
        #custom-sl.good {
            border-bottom: 3px solid green;
        }
        
        #custom-sl.other {
            border-bottom: 3px solid #F4AF39;
        }
        
        #custom-appkill {
            color: red;
            background-color: black;
        }
      '';
      settings = [{
        layer = "bottom";
        position = "top";
        height = 22;
        tray = { spacing = 10; };
        modules-center = [ "clock" "custom/appkill" ];
        modules-left = [ "sway/mode" "sway/workspaces" "sway/window" ];
        #modules-left = [ "wlr/workspaces" ];
        modules-right = [ "idle_inhibitor" "pulseaudio" "network" "cpu" "memory" "backlight" "temperature" "battery" "tray" ];
        #modules-right = [ "cpu" "memory" "pulseaudio" "clock" "tray" ];

        "sway/workspaces" = {
          format = "<span font='14'>{icon}</span>";
          format-icons = {
            "1"="";
            "2"="";
            "3"="";
            "4"="";
            "5"="";
          };
          all-outputs = false;
          persistent_workspaces = {
             "1" = [];
             "2" = [];
             "3" = [];
             "4" = [];
             "5" = [];
          };
        };
        "wlr/workspaces" = {
          format = "{icon}";
          format-icons = {
            "1"="";
            "2"="";
            "3"="";
            "4"="";
            "5"="";
          };
          all-outputs = true;
          active-only = false;
          on-click = "activate";
        };
        clock = {
          format = "{:%b %d %H:%M}";
          tooltip-format = "<big>{:%Y %B}</big>\n<tt><small>{calendar}</small></tt>";
          format-alt = "{:%A, %B %d, %Y}";
        };
        idle_inhibitor = {
            format = "{icon}";
            format-icons = {
                activated = "";
                deactivated = "";
            };
        };
        cpu = {
          format = "{usage}% ";
          tooltip = false;
          interval = 2;
        };
        disk = {
          format = "{percentage_used}% ";
          path = "/";
          interval = 30;
        };
        memory = {
          format = "{}% ";
          interval = 5;
        };
        temperature = {
            states = {
                "good" = 50;
                "warning" = 70;
                "critical" = 80;
            };
            "thermal-zone" = 2;
            "hwmon-path" = "/sys/class/hwmon/hwmon3/temp1_input";
            "format" = "{temperatureC}°C {icon}";
            "format-icons" = ["❄" "❄" "" "" ""];
        };
        backlight = {
            "format" = "{percent}% {icon}";
            "format-icons" = ["" ""];
            "on-scroll-up" = "light -A +5%";
            "on-scroll-down" = "light -U -5%";
        };
        battery = {
          interval = 60;
          states = {
            good = 95;
            warning = 20;
            critical = 5;
          };
          format = "{capacity}% {icon}";
          format-charging = "{capacity}% ";
          format-plugged = "{capacity}% ";
          format-alt = "{time} {icon}";
          format-icons = ["" "" "" "" ""];
        };
        network = {
          format-wifi = " {ipaddr}";
          format-ethernet = "{ifname}: {ipaddr}/{cidr} ";
          format-linked = "{ifname} (No IP) 睊";
          format-disconnected = "Not connected ⚠";
          format-alt = "{ifname}: {ipaddr}/{cidr}";
          tooltip-format = "{essid} {signalStrength}%";
          on-click-right = "${pkgs.alacritty}/bin/alacritty -e nmtui";
        };
        pulseaudio = {
          scroll-step = 1;
          format = "{volume}% {icon} {format_source}";
          format-bluetooth = "{volume}% {icon} {format_source}";
          format-bluetooth-muted = "{volume}% {icon} {format_source}";
          format-muted = " {format_source}";
          format-source = "{volume}% ";
          #format-source = "<span font='14'></span>";
          format-source-muted = "";
          format-icons = {
            default = [ "" "" "" ];
            headphones = "";
            handsfree = "";
            headset = "";
          };
          tooltip-format = "{desc}, {volume}%";
          on-click-right = "${pkgs.pamixer}/bin/pamixer --default-source -t";
          on-click = "${pkgs.pavucontrol}/bin/pavucontrol";
        };
        "custom/appkill" = {
            format = " ";
            icon-size = 20;
            #on-click = "${pkgs.hyprland}/bin/hyprctl dispatch killactive .";
            on-click = "${pkgs.sway}/bin/swaymsg kill";
            tooltip = false;
        };
      }];
    };
  };
}