{ config, lib, ... }:
with lib;
let
  cfg = config.mjallen.programs.waybar;
  nord = import (lib.snowfall.fs.get-file "modules/home/desktop/theme/nord.nix");

  defaultOpacity = "opacity: 0.85;";
  defaultBorderRadius = "border-radius: 1rem;";
  defaultCenterOptions = ''
    padding-top: 0.2rem;
    padding-bottom: 0.2rem;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    margin: 3px 0;
  '';
  borderRight = ''
    padding-top: 0.2rem;
    padding-bottom: 0.2rem;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    margin: 3px 0;
    border-radius: 0rem 1rem 1rem 0rem;
    margin-right: 0.5rem;
  '';
  borderLeft = ''
    padding-top: 0.2rem;
    padding-bottom: 0.2rem;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    margin: 3px 0;
    border-radius: 1rem 0rem 0rem 1rem;
    margin-left: 0.5rem;
  '';
in
{
  imports = [
    ./options.nix
    ./scripts/hass.nix
    ./scripts/weather.nix
  ];

  config = mkIf cfg.enable {
    # https://github.com/Alexays/Waybar/wiki/Module:-Hyprland
    # https://www.nerdfonts.com/cheat-sheet

    programs.waybar = {
      enable = true;
      systemd.enable = true;
      settings = {
        mainBar = {
          layer = cfg.layer;
          position = "top";
          mod = "dock";
          exclusive = true;
          passthrough = false;
          gtk-layer-shell = true;
          height = 0;

          # Module Layout
          modules-left = [ "hyprland/workspaces" ];

          modules-center = [ "hyprland/window" ];

          modules-right = [
            "tray"
            "custom/left-end"
            "temperature"
            "temperature#gpu"
            "keyboard-state#capslock"
            "keyboard-state#numlock"
            "wireplumber#sink"
            "bluetooth"
            "network"
            "idle_inhibitor"
            "custom/right-end"
            "custom/left-end"
            "clock"
            "battery"
            "custom/weather"
            "custom/right-end"
          ];

          # modules-right = [
          #   "tray"
          #   "custom/left-end" ] ++
          #   cfg.modules-right ++
          #   [ "custom/right-end" ];

          # Module Definitions
          # Left
          "hyprland/workspaces" = {
            disable-scroll = true;
            all-outputs = true;
            on-click = "activate";
            persistent_workspaces = {
              "*" = 1;
            };
          };

          # Center
          "hyprland/window" = {
            separate-outputs = true;
            format = { };
          };

          # Left
          tray = {
            icon-size = 16;
            spacing = 10;
          };

          temperature = {
            hwmon-path = "/sys/class/hwmon/hwmon4/temp1_input";
            critical-threshold = 110;
            format-critical = "{temperatureC}°C ";
            format = "{temperatureC}°C {icon}";
            format-icons = [
              "" # fa-temperature-empty
              "" # fa-temperature-quarter
              "" # fa-temperature-half
              "" # fa-temperature-three-quarters
              "" # fa-temperature-full
            ];
            tooltip-format = "CPU: {temperatureC}°C";
          };

          "temperature#gpu" = {
            hwmon-path = "/sys/class/hwmon/hwmon0/temp1_input";
            critical-threshold = 110;
            format-critical = "{temperatureC}°C ";
            format = "{temperatureC}°C {icon}";
            format-icons = [
              "" # fa-temperature-empty
              "" # fa-temperature-quarter
              "" # fa-temperature-half
              "" # fa-temperature-three-quarters
              "" # fa-temperature-full
            ];
            on-click = "lact";
            tooltip-format = "GPU: {temperatureC}°C";
          };

          "keyboard-state#capslock" = {
            capslock = true;
            format = "{icon}";
            tooltip-format = "Caps Lock {state}";
            format-icons = {
              locked = "󰬶";
              unlocked = "󰬵";
            };
          };

          "keyboard-state#numlock" = {
            numlock = true;
            format = "{icon}";
            tooltip-format = "Num Lock {state}";
            format-icons = {
              locked = "󰎠";
              unlocked = "󱧓";
            };
          };

          "wireplumber#sink" = {
            format = "{icon} {volume}%";
            tooltip = false;
            format-muted = "{icon} Muted";
            on-click = "wpctl set-mute @DEFAULT_SINK@ toggle";
            on-click-right = "pavucontrol -t 1";
            on-scroll-up = "wpctl set-volume @DEFAULT_SINK@ 5%+";
            on-scroll-down = "wpctl set-volume @DEFAULT_SINK@ 5%-";
            scroll-step = 5;
            format-icons = {
              headphone = "󰋋";
              headphone-muted = "󰟎";
              hands-free = "󰋋";
              headset = "󰋋";
              phone = "";
              portable = "󰋋";
              car = "";
              default = [
                ""
                ""
                ""
                ""
              ];
            };
          };

          "wireplumber#source" = {
            node-type = "Audio/Source";
            format = "";
            format-muted = "";
            tooltip = false;
            # tooltip-format = "{source_desc} | {source_volume}%";
            on-click = "wpctl set-mute @DEFAULT_SOURCE@ toggle";
            on-click-right = "pavucontrol -t 2";
            on-scroll-up = "wpctl set-volume @DEFAULT_SOURCE@ 5%+";
            on-scroll-down = "wpctl set-volume @DEFAULT_SOURCE@ 5%-";
            scroll-step = 5;
          };

          "bluetooth" = {
            on-click = "overskride";
            tooltip = true;
            format = "{icon}";
            tooltip-format = "{status}";
            tooltip-format-disabled = "{status}";
            tooltip-format-off = "{status}";
            tooltip-format-on = "{status}";
            tooltip-format-connected = "{status}";
            tooltip-format-enumerate-connected = { };
            format-icons = {
              disabled = "󰂲";
              off = "󰂲";
              on = "󰂯";
              connected = "󰂱";
            };
          };

          network = {
            interface = cfg.networkInterface;
            on-click = "nm-connection-editor";
            format = "{icon}";
            tooltip-format = "{ifname} via {gwaddr} 󰊗";
            tooltip-format-wifi = ''
              {essid} ({signalStrength}%) {icon}
               {bandwidthDownBits}  {bandwidthUpBits}
            '';
            tooltip-format-ethernet = "{ifname} ";
            tooltip-format-disconnected = "Disconnected";
            max-length = 50;
            format-icons = {
              wifi = [
                "󰤯"
                "󰤟"
                "󰤢"
                "󰤥"
                "󰤨"
              ];
              ethernet = "󰈀";
              linked = "󰤫";
              disconnected = "󰤫";
            };
          };

          idle_inhibitor = {
            format = "{icon}";
            format-icons = {
              activated = "󰐂";
              deactivated = "󱩜";
            };
          };

          clock = {
            format = "{:%I:%M %p}";
            tooltip-format = "<tt><small>{calendar}</small></tt>";
            calendar = {
              mode = "month";
              format = {
                months = "<span color='${nord.frost.nord9}'><b>{}</b></span>";
                days = "<span color='${nord.frost.nord10}'><b>{}</b></span>";
                weekdays = "<span color='${nord.frost.nord8}'><b>{}</b></span>";
                today = "<span color='${nord.aurora.nord14}'><b><u>{}</u></b></span>";
              };
            };
          };

          battery = {
            # bat = "macsmc-battery";
            interval = 60;
            # statuses: 'Charging', 'Discharging'
            states = {
              warning = 30;
              critical = 15;
            };
            format = "{capacity}% {icon}";
            format-icons = {
              default = [
                "󰂃" # critical
                "󰁺" # 10%
                "󰁻" # 20%
                "󰁼" # 30%
                "󰁽" # 40%
                "󰁾" # 50%
                "󰁿" # 60%
                "󰂀" # 70%
                "󰂁" # 80%
                "󰂂" # 90%
                "󰁹" # 100%
              ];
              charging = [
                "󰢟" # critical
                "󰢜" # 10%
                "󰂆" # 20%
                "󰂇" # 30%
                "󰂈" # 40%
                "󰢝" # 50%
                "󰂉" # 60%
                "󰢞" # 70%
                "󰂊" # 80%
                "󰂋" # 90%
                "󰂅" # 100%
              ];
            };
            max-length = 25;
          };

          "custom/weather" = {
            tooltip = true;
            format = { };
            interval = 30;
            exec = "waybar-weather";
            return-type = "json";
            markup = "pango";
          };

          "custom/left-end" = {
            format = "&nbsp";
            tooltip = false;
          };

          "custom/right-end" = {
            format = "&nbsp";
            tooltip = false;
          };
        }
        // cfg.extraModules;
      };

      # * { font-size: 13px; }
      # window.eDP-1 * { font-size: 10px; }

      style = ''
        .blink_me {
          animation: blinker 1s linear infinite;
        }

        @keyframes blinker {
          50% {
            color: ${nord.aurora.nord11};
          }
        }

        * {
          font-family:
            Jetbrains Mono Nerd Font,
            monospace;
          font-size: 14px;
          min-height: 0;
        }

        #waybar {
          background: transparent;
          color: ${nord.snowStorm.nord6};
          margin: 5px 5px;
        }

        #workspaces {
          background-color: ${nord.polarNight.nord0};
          ${defaultBorderRadius}
          ${defaultOpacity}
          ${defaultCenterOptions}
          margin-left: 0.6rem;
        }

        #workspaces button {
          color: ${nord.frost.nord10};
          ${defaultBorderRadius}
          padding: 0.4rem;
        }

        #workspaces button.active {
          color: ${nord.frost.nord8};
          ${defaultBorderRadius}
        }

        #workspaces button:hover {
          color: ${nord.frost.nord7};
          ${defaultBorderRadius}
        }

        #workspaces button.focused {
          color: ${nord.snowStorm.nord6};
          background: ${nord.aurora.nord13};
          ${defaultBorderRadius}
        }

        #workspaces button.urgent {
          color: ${nord.polarNight.nord0};
          background: ${nord.snowStorm.nord6};
          ${defaultBorderRadius}
        }

        #tooltip {
          background: ${nord.polarNight.nord0};
          border-color: ${nord.polarNight.nord0};
          ${defaultBorderRadius}
          border-width: 1rem;
          border-style: solid;
        }

        #window {
          color: ${nord.aurora.nord15};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultBorderRadius}
          ${defaultCenterOptions}
          margin-left: 4rem;
          margin-right: ${toString cfg.windowOffset}rem;
        }

        /* make window module transparent when no windows present */
        #window.empty {
          background-color: transparent;
        }

        #custom-weather { 
          color: ${nord.frost.nord10};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        #battery {
          color: ${nord.aurora.nord15};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
          min-width: 3rem;
        }

        #clock {
          color: ${nord.frost.nord9};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        /* ------------- */

        #idle_inhibitor {
          color: ${nord.frost.nord10};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
          padding-right: 1rem;
        }

        #idle_inhibitor:hover {
          background: ${nord.polarNight.nord3};
        }

        #network {
          color: ${nord.aurora.nord15};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
          padding-right: 15px;
        }

        #network:hover {
          background: ${nord.polarNight.nord3};
        }

        #bluetooth {
          color: ${nord.frost.nord9};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        #bluetooth:hover {
          background: ${nord.polarNight.nord3};
        }

        #wireplumber.source {
          color: ${nord.frost.nord8};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        #wireplumber.source.muted {
          animation-name: blinker;
          animation-duration: 2s;
          animation-timing-function: linear;
          animation-iteration-count: infinite;
          padding-right: 1rem;
        }

        #wireplumber.source:hover {
          background: ${nord.polarNight.nord3};
        }

        #wireplumber.sink {
          color: ${nord.frost.nord7};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        #wireplumber.sink.muted {
          animation-name: blinker;
          animation-duration: 5s;
          animation-timing-function: linear;
          animation-iteration-count: infinite;
        }

        #wireplumber.sink:hover {
          background: ${nord.polarNight.nord3};
        }

        #keyboard-state.numlock {
          color: ${nord.frost.nord8};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        #keyboard-state.capslock {
          color: ${nord.frost.nord9};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        #temperature.gpu {
          color: ${nord.frost.nord10};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0;
        }

        #temperature.gpu:hover {
          background: ${nord.polarNight.nord3};
        }

        #temperature {
          color: ${nord.frost.nord9};
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          border-radius: 0
        }

        /* ------------- */

        #tray {
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${defaultCenterOptions}
          ${defaultBorderRadius}
          margin-right: 0.6rem;
        }

        /* ------------- */

        #custom-left-end {
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${borderLeft}
        }

        #custom-right-end {
          background-color: ${nord.polarNight.nord0};
          ${defaultOpacity}
          ${borderRight}
        }
      ''
      + cfg.extraModulesStyle or '''';
    };
  };
}
