{ pkgs, hyprlandSettings, ... }:
let
  settings = import ../../settings.nix { inherit pkgs; };

  defaultOpacity = "opacity: 0.85;";
  defaultBorderRadius = "border-radius: 1rem;";
  defaultCenterOptions = ''
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    margin: 3px 0;
  '';
  borderRight = ''
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
    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.5rem;
    padding-bottom: 0.5rem;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    margin: 3px 0;
    border-radius: 1rem 0rem 0rem 1rem;
    margin-left: 0.5rem;
  '';
in
{
  imports = [
    ./scripts/hass.nix
    ./scripts/weather.nix
  ];
  # https://github.com/Alexays/Waybar/wiki/Module:-Hyprland
  # https://www.nerdfonts.com/cheat-sheet

  programs.waybar = {
    enable = true;
    systemd.enable = true;
    settings = {
      mainBar = {
        layer = "top";
        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/lights"
        #   "temperature"
        #   "temperature#gpu"
        #   "keyboard-state#capslock"
        #   "keyboard-state#numlock"
        #   "wireplumber#sink"
        #   "wireplumber#source"
        #   "bluetooth"
        #   "idle_inhibitor"
        #   "clock"
        #   "custom/weather"
        # ];

        modules-right = hyprlandSettings.waybar.modules-right;

        # 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;
        };

        "custom/lights" = {
          tooltip = false;
          exec = "waybar-hass --get_light light.living_room_lights";
          interval = "once";
          format = "{text}";#"󱉓";
          on-click = "waybar-hass --toggle_light light.living_room_lights";
          return-type = "json";
        };

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

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

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

        "keyboard-state#numlock" = {
          numlock = true;
          icon-size = 60;
          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 = "{format-off}";
          format-disabled = "󰂲";
          format-off = "󰂲";
          format-on = "󰂯";
          format-connected = "󰂱";
          tooltip-format = "{status}";
          tooltip-format-disabled = "{status}";
          tooltip-format-off = "{status}";
          tooltip-format-on = "{status}";
          tooltip-format-connected = "{status}";
          tooltip-format-enumerate-connected = { };
        };
        
        network = {
          interface = "wlan0";
          format = "{ifname}";
          format-wifi = ""; # "{essid} ({signalStrength}%) ";
          format-ethernet = "󰊗"; # "{ipaddr}/{cidr} 󰊗";
          format-disconnected = "" ; # An empty format will hide the module.
          tooltip-format = "{ifname} via {gwaddr} 󰊗";
          tooltip-format-wifi = "{essid} ({signalStrength}%) ";
          tooltip-format-ethernet = "{ifname} ";
          tooltip-format-disconnected = "Disconnected";
          max-length = 50;
        };

        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='${settings.nord.frost.nord9}'><b>{}</b></span>";
              days =       "<span color='${settings.nord.frost.nord10}'><b>{}</b></span>";
              weekdays =   "<span color='${settings.nord.frost.nord8}'><b>{}</b></span>";
              today =      "<span color='${settings.nord.aurora.nord14}'><b><u>{}</u></b></span>";
            };
          };
        };

        battery = {
          # bat = "macsmc-ac";
          interval = 60;
          states = {
              warning = 30;
              critical = 15;
          };
          format = "{capacity}% {icon}";
          format-icons = ["" "" "" "" ""];
          max-length = 25;
        };

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

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

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

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

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

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

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

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

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

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

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

      #custom-weather { 
        color: ${settings.nord.frost.nord10};
        background-color: ${settings.nord.polarNight.nord0};
        ${defaultOpacity}
        ${borderRight}
      }

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

      #clock {
        color: ${settings.nord.frost.nord9};
        background-color: ${settings.nord.polarNight.nord0};
        ${defaultOpacity}
        ${borderLeft}
      }

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

      #idle_inhibitor {
        color: ${settings.nord.frost.nord10};
        background-color: ${settings.nord.polarNight.nord0};
        ${defaultOpacity}
        ${borderRight}
      }

      #network {
        color: ${settings.nord.aurora.nord15};
        background-color: ${settings.nord.polarNight.nord0};
        ${defaultOpacity}
        ${borderRight}
        padding-right: 15px;
      }

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

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

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

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

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

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

      #temperature {
        color: ${settings.nord.frost.nord9};
        background-color: ${settings.nord.polarNight.nord0};
        ${defaultOpacity}
        ${borderLeft}
      }

      #custom-lights {
        color: ${settings.nord.frost.nord8};
        background-color: ${settings.nord.polarNight.nord0};
        ${defaultOpacity}
        ${borderLeft}
      }

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

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