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

let
  cfg = config.samfelag.modules.desktop.wm.hyprland;
  mod = "Mod4";
in {

  options.samfelag.modules.desktop.wm.hyprland = with lib; {
    enable = mkEnableOption "hyprland window manager";
    mod = my.mkOpt' types.str "Mod4" "Main modifier key for hyprland";
    kb_layout = my.mkOpt' types.str "es" "Keyboard layout(s) for hyprland";
    extraKeybindings = my.mkOpt (types.listOf types.str) [ ];
  };

  config = lib.mkIf cfg.enable {

    # - Configuration -----------------------------

    # Hyprland config
    programs.hyprland = {
      enable = true;
      xwayland.enable = true;
    };

    hm.wayland.windowManager.hyprland = {
      enable = true;
      package = pkgs.hyprland;

      xwayland.enable = true;
      systemd.enable = true;

      plugins = [
        pkgs.hyprlandPlugins.hy3
      ];

      settings = {

        general = {
          layout = "hy3";
        };

        env = [
          "HYPRCURSOR_THEME,rose-pine-hyprcursor"
          "HYPRCURSOR_SIZE,24"
        ];

        input = {
          kb_layout = cfg.kb_layout;
        };

        monitor = ",highres,auto,1";

        exec-once = [
        ];

        decoration = {
          rounding = 10;
        };

        gestures = {
          # See https://wiki.hyprland.org/Configuring/Variables/ for more
          workspace_swipe = true;
        };

        bind = [

          "${cfg.mod}, Q, killactive,"

          "${cfg.mod}, F, fullscreen, 1"
          "${cfg.mod} SHIFT, F, fullscreen, 0"

          "${cfg.mod} SHIFT, SPACE, togglefloating"

          # Move focus
          "${cfg.mod}, left, hy3:movefocus, l"
          "${cfg.mod}, right, hy3:movefocus, r"
          "${cfg.mod}, up, hy3:movefocus, u"
          "${cfg.mod}, down, hy3:movefocus, d"

          "${cfg.mod}, h, hy3:movefocus, l"
          "${cfg.mod}, l, hy3:movefocus, r"
          "${cfg.mod}, k, hy3:movefocus, u"
          "${cfg.mod}, j, hy3:movefocus, d"

          # Move focus
          "${cfg.mod} SHIFT, left, hy3:movewindow, l"
          "${cfg.mod} SHIFT, right, hy3:movewindow, r"
          "${cfg.mod} SHIFT, up, hy3:movewindow, u"
          "${cfg.mod} SHIFT, down, hy3:movewindow, d"

          "${cfg.mod} SHIFT, h, hy3:movewindow, l"
          "${cfg.mod} SHIFT, l, hy3:movewindow, r"
          "${cfg.mod} SHIFT, k, hy3:movewindow, u"
          "${cfg.mod} SHIFT, j, hy3:movewindow, d"

          # Switch workspace
          "${cfg.mod}, 1, workspace, 1"
          "${cfg.mod}, 2, workspace, 2"
          "${cfg.mod}, 3, workspace, 3"
          "${cfg.mod}, 4, workspace, 4"
          "${cfg.mod}, 5, workspace, 5"
          "${cfg.mod}, 0, workspace, 6"

          # Move to workspace
          "${cfg.mod} SHIFT, 1, movetoworkspace, 1"
          "${cfg.mod} SHIFT, 2, movetoworkspace, 2"
          "${cfg.mod} SHIFT, 3, movetoworkspace, 3"
          "${cfg.mod} SHIFT, 4, movetoworkspace, 4"
          "${cfg.mod} SHIFT, 5, movetoworkspace, 5"
          "${cfg.mod} SHIFT, 0, movetoworkspace, 6"

          # - Keyboard ---------------------------
          "${cfg.mod}, Escape, exec, hyprctl switchxkblayout current next"

        ] ++ cfg.extraKeybindings;

        bindm = [
          "${cfg.mod}, mouse:272, hy3:movewindow"
          "${cfg.mod} SHIFT, mouse:272, resizewindow 1"
        ];
      };
    };
  };
}
