{ pkgs, lib, macos-hyprcursor-src, ... }:
let
  scripts = ./scripts;
  shell = "quickshell";

  # Cursor Theme
  macos-hyprcursor = pkgs.stdenv.mkDerivation {
    name = "macos-hyprcursor";
    src = macos-hyprcursor-src;
    installPhase = ''
      mkdir -p $out/share/icons
      cp -R themes/SVG/"macOS (SVG)" $out/share/icons/macos
    '';
  };
in {

  home.packages = [ macos-hyprcursor ];

  wayland.windowManager.hyprland = {
    enable = true;
    systemd.enable = true;
    xwayland.enable = true;

    plugins = [ pkgs.hyprlandPlugins.hy3 ];

    settings = {
      "$mod" = "SUPER";
      "$terminal" = "alacritty";

      env = [
        "HYPRCURSOR_THEME,macos"
        "HYPRCURSOR_SIZE,24"
      ];

      general = {
        layout = "hy3";

        border_size = 3;
        gaps_in = 8;
        gaps_out = 8;

        resize_on_border = true;

        "col.inactive_border" = "rgb(474f6f)";
        "col.active_border" = "rgb(b4f9f8)";
      };

      windowrulev2 = [
        "bordercolor rgb(f7768e), fullscreen:1"
        "noanim, initialClass:ueberzug(.*)"
      ];

      cursor = {
        inactive_timeout = 5;
        hide_on_key_press = true;
      };

      plugin.hy3 = {
        tab_first_window = false;

        tabs = {
          height = 6;
          render_text = false;
          border_width = 0;

          "col.active" = "rgb(b4f9f8)";
          "col.inactive" = "rgb(474f6f)";
          "col.urgent" = "rgb(f7768e)";
        };
      };

      decoration = {
        rounding = 10;
        shadow.color = "rgba(16161ea0)";
      };

      group.groupbar = {
        font_family = "JetBrainsMono Nerd Font";
        font_size = 14;
      };

      layerrule = [
        "noanim, ^(notifications)$"
      ];

      animations = {
        bezier = [ "overshot, 0.13, 0.99, 0.29, 1.06" ];
        animation = [
          "windows, 1, 2, default, popin 70%"
          "windowsMove, 1, 4, overshot, slide"
          "border, 1, 8, default,"
          "fade, 1, 8, default,"
          "workspaces, 1, 5, overshot, slidevert"
        ];
      };

      misc = {
        disable_hyprland_logo = true;
        disable_autoreload = true;
        new_window_takes_over_fullscreen = 2;
        focus_on_activate = true;
      };

      # Bindings

      bind = [
        "$mod, Return, exec, $terminal"
        "$mod, D, global, ${shell}:launcher"
        "$mod Shift, D, global, ${shell}:dashboard"
        "$mod, M, exec, hyprlock"

        # Exit
        "$mod Shift, E, global, ${shell}:session"

        # Emacs Everywhere
        # "$mod, Q, exec, $HOME/.config/emacs/bin/doom +everywhere"

        # Windows
        "$mod, Left, hy3:movefocus, l"
        "$mod, Right, hy3:movefocus, r"
        "$mod, Up, hy3:movefocus, u"
        "$mod, Down, hy3:movefocus, d"
        "$mod, H, hy3:movefocus, l"
        "$mod, L, hy3:movefocus, r"
        "$mod, K, hy3:movefocus, u"
        "$mod, J, hy3:movefocus, d"

        "$mod Shift, Left, hy3:movewindow, l"
        "$mod Shift, Right, hy3:movewindow, r"
        "$mod Shift, Up, hy3:movewindow, u"
        "$mod Shift, Down, hy3:movewindow, d"
        "$mod Shift, H, hy3:movewindow, l"
        "$mod Shift, L, hy3:movewindow, r"
        "$mod Shift, K, hy3:movewindow, u"
        "$mod Shift, J, hy3:movewindow, d"

        "$mod, F, fullscreen, 1"
        "$mod Shift, F, fullscreen, 0"
        "$mod Shift, Q, hy3:killactive,"

        "$mod, V, hy3:makegroup, v, ephemeral"
        "$mod, B, hy3:makegroup, h, ephemeral"
        "$mod, T, hy3:makegroup, tab"
        "$mod Shift, T, hy3:changegroup, toggletab"
        "$mod, E, hy3:changegroup, opposite"
        "$mod, A, hy3:changefocus, raise"

        # Screenshot
        "$mod, P, exec, ${scripts}/screenshot-save"

        # Workspace manipulation
        "$mod, N, global, ${shell}:newworkspace"
        "$mod Shift, N, global, ${shell}:movetonewworkspace"

        # Game Mode
        "$mod, F1, exec, ${scripts}/gamemode"
      ] ++
      builtins.concatMap (n:
        let
          wsp = builtins.toString n;
          key = if n == 10 then "0" else wsp;
        in [
          "$mod, ${key}, global, ${shell}:workspace${wsp}"
          "$mod Shift, ${key}, global, ${shell}:movetoworkspace${wsp}"
        ]) (lib.range 1 10);

      bindn = [
        ", mouse:272, hy3:focustab, mouse"
        ", escape, global, ${shell}:escape"
      ];

      bindl = [
        # XF86 key bindings
        ", XF86AudioMute, global, ${shell}:mute"
        ", XF86AudioMicMute, global, ${shell}:muteMic"
        ", XF86AudioPlay, global, ${shell}:mediaToggle"
        ", XF86AudioNext, global, ${shell}:mediaNext"
        ", XF86AudioPrev, global, ${shell}:mediaPrev"
        ", XF86AudioStop, global, ${shell}:mediaStop"
        ", XF86AudioRaiseVolume, global, ${shell}:volumeUp"
        ", XF86AudioLowerVolume, global, ${shell}:volumeDown"
        ", XF86MonBrightnessUp, global, ${shell}:brightnessUp"
        ", XF86MonBrightnessDown, global, ${shell}:brightnessDown"

        # Screenshot
        ", Print, exec, ${scripts}/screenshot"
        "Shift, Print, exec, ${scripts}/screenshot-slurp"
      ];
    };
  };
}
