{ lib, config, pkgs, ... }:
let
  openOnCurrentScreen = pkgs.writeShellScriptBin "eww-open-on-current-screen" ''
    window="$1"
    shift

    screen="$(hyprctl monitors -j | ${lib.getExe pkgs.jq} -r '.[] | select(.focused == true) | .name' | head -n1)"

    if [ -n "$screen" ]; then
      exec ${lib.getExe pkgs.eww} open "$window" --screen "$screen" "$@"
    fi

    exec ${lib.getExe pkgs.eww} open "$window" "$@"
  '';
in {

  config = lib.mkIf (config.usercfg.wm == "Wayland") {
    home.packages = with pkgs; [ eww jq jaq custom.amdgpu_top openOnCurrentScreen ];

    xdg.configFile."eww" = {
      source = lib.cleanSourceWith {
        filter = name: _type:
          let baseName = baseNameOf (toString name);
          in !(lib.hasSuffix ".nix" baseName) && (baseName != "_colors.scss");
        src = lib.cleanSource ./bar/.;
      };

      recursive = true;
    };

    # colors file
    xdg.configFile."eww/css/_colors.scss".text = ''
      $base00: #${config.colorScheme.palette.base00};
      $base01: #${config.colorScheme.palette.base01};
      $base02: #${config.colorScheme.palette.base02};
      $base03: #${config.colorScheme.palette.base03};
      $base04: #${config.colorScheme.palette.base04};
      $base05: #${config.colorScheme.palette.base05};
      $base06: #${config.colorScheme.palette.base06};
      $base07: #${config.colorScheme.palette.base07};
      $base08: #${config.colorScheme.palette.base08};
      $base09: #${config.colorScheme.palette.base09};
      $base0A: #${config.colorScheme.palette.base0A};
      $base0B: #${config.colorScheme.palette.base0B};
      $base0C: #${config.colorScheme.palette.base0C};
      $base0D: #${config.colorScheme.palette.base0D};
      $base0E: #${config.colorScheme.palette.base0E};
      $base0F: #${config.colorScheme.palette.base0F};


      $fg: $base07;
      $bg0: $base00;
      $bg1: $base01;

      $border-color: $base03;
      $border-color-focus: $base04;
      $border-radius: ${config.colorScheme.palette.border-radius}px;
      $border-width: ${config.colorScheme.palette.border-width}px;

      $gaps-screen: ${config.colorScheme.palette.gaps-screen}px;
      $gaps-window: ${config.colorScheme.palette.gaps-window}px;
    '';
  };
}
