{
  config,
  lib,
  pkgs,
  ...
}:
with lib;
let
  cfg = config.mjallen.desktop.hyprland;
  drawer = "nwg-drawer -fm nautilus -term kitty -mb 10 -mt 10 -ml 10 -mr 10 -pbuseicontheme -i ${cfg.iconThemeName}";
in
{
  imports = [
    ./options.nix
    ./packages.nix
    ./theme.nix
    ./variables.nix
  ];

  config = mkIf cfg.enable {

    services = {
      hyprpolkitagent.enable = true;
    };

    programs = {
      vscode.profiles.default.userSettings."window"."titleBarStyle" = "custom";
    };

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

      plugins = with pkgs.hyprlandPlugins; [
        hyprgrass
      ];

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

        # Mouse
        # mouse_[up|down] - scroll wheel
        # middle_mouse - 274
        # thumb_up - 276
        # thumb_down - 275

        # l -> locked, will also work when an input inhibitor (e.g. a lockscreen) is active.
        # r -> release, will trigger on release of a key.
        # e -> repeat, will repeat when held.
        # n -> non-consuming, key/mouse events will be passed to the active window in addition to triggering the dispatcher.
        # m -> mouse, see below.
        # t -> transparent, cannot be shadowed by other binds.
        # i -> ignore mods, will ignore modifiers.
        # s -> separate, will arbitrarily combine keys between each mod/key, see [Keysym combos](#keysym-combos) above.
        # d -> has description, will allow you to write a description for your bind.
        # p -> bypasses the app's requests to inhibit keybinds.

        # https://wiki.hyprland.org/Configuring/Binds/
        # https://wiki.hyprland.org/Configuring/Binds/#mouse-buttons

        bind = [
          "$mod, Return, exec, ${cfg.defaultApps.terminal.pname}"
          "$mod, SPACE, exec, wofi --show drun"
          ", xf86Search, exec, wofi --show drun"
          "$mod, Q, killactive, "
          "$mod, M, exec, wlogout --protocol layer-shell"
          "$mod, E, exec, ${cfg.defaultApps.fileExplorer.pname}"
          "$mod, V, togglefloating, "
          "$mod, D, exec, ${drawer}"
          "$mod, P, pseudo, " # dwindle
          "$mod, S, togglesplit, " # dwindle
          "$mod SHIFT, Q, exec, hyprlock"
          "$mod SHIFT, 4, exec, hyprshot -m region --clipboard-only"
          "$mod, F, fullscreen, 1"
          "$mod SHIFT, F, fullscreen, 0"
          "$mod SHIFT, E, exec, smile"

          "$mod, mouse:276, movecurrentworkspacetomonitor, ${cfg.display1.input}"
          "$mod, mouse:275, movecurrentworkspacetomonitor, ${cfg.display2.input}"

          # alt-tab between workspaces on active monitor
          "$mod, Tab, workspace, m+1"
          "$mod SHIFT, Tab, workspace, m-1"

          "$mod, h, movefocus, l"
          "$mod, l, movefocus, r"
          "$mod, k, movefocus, u"
          "$mod, j, movefocus, d"

          "$mod, 1, workspace, 1"
          "$mod, 2, workspace, 2"
          "$mod, 3, workspace, 3"
          "$mod, 4, workspace, 4"
          "$mod, 5, workspace, 5"
          "$mod, 6, workspace, 6"
          "$mod, 7, workspace, 7"
          "$mod, 8, workspace, 8"
          "$mod, 9, workspace, 9"
          "$mod, 0, workspace, 10"

          "$mod ALT, 1, movetoworkspace, 1"
          "$mod ALT, 2, movetoworkspace, 2"
          "$mod ALT, 3, movetoworkspace, 3"
          "$mod ALT, 4, movetoworkspace, 4"
          "$mod ALT, 5, movetoworkspace, 5"
          "$mod ALT, 6, movetoworkspace, 6"
          "$mod ALT, 7, movetoworkspace, 7"
          "$mod ALT, 8, movetoworkspace, 8"
          "$mod ALT, 9, movetoworkspace, 9"
          "$mod ALT, 0, movetoworkspace, discord"

          "$mod CTRL, l, resizeactive, 10 0"
          "$mod CTRL, h, resizeactive, -10 0"
          "$mod CTRL, k, resizeactive, 0 -10"
          "$mod CTRL, j, resizeactive, 0 10"

          "$mod SHIFT, l, movewindow, r"
          "$mod SHIFT, h, movewindow, l"
          "$mod SHIFT, k, movewindow, u"
          "$mod SHIFT, j, movewindow, d"

          "$mod, b, exec, ${cfg.defaultApps.browser.pname}"
        ];

        bindm = [
          # Move/resize windows with mod + LMB/RMB and dragging
          "$mod, mouse:272, movewindow"
          "$mod, mouse:273, resizewindow"
          # middle mouse will grab a window, mod + middle mouse will close it
          "$mod SHIFT, mouse:274, movewindow"
        ];

        bindel = [
          ", XF86AudioRaiseVolume, exec, wpctl set-volume -l 1.5 @DEFAULT_AUDIO_SINK@ 5%+"
          ", XF86AudioLowerVolume, exec, wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"
        ];

        bindl = [
          ", XF86AudioMute, exec, wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"
          ", XF86AudioPlay, exec, playerctl play-pause"
          ", XF86AudioPrev, exec, playerctl previous"
          ", XF86AudioNext, exec, playerctl next"
          ", XF86AudioMicMute, exec, wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle"

          ", XF86MonBrightnessUp, exec, brightnessctl set +5%"
          ", XF86MonBrightnessDown, exec, brightnessctl set 5%-"

          "$mod, XF86MonBrightnessUp, exec, brightnessctl -d kbd_backlight set +10%"
          "$mod, XF86MonBrightnessDown, exec, brightnessctl -d kbd_backlight set 10%-"
        ];

        monitor = cfg.monitor;
        monitorv2 = cfg.monitorv2 or { };

        render = {
          cm_fs_passthrough = 1;
        };

        misc = {
          vrr = 1;
        };

        general = {
          gaps_in = 5;
          gaps_out = 10;
          border_size = 1;
          "col.active_border" = "rgb(8aadf4) rgb(24273A) rgb(24273A) rgb(8aadf4) 45deg";
          "col.inactive_border" = "rgb(24273A) rgb(24273A) rgb(24273A) rgb(24273A) 45deg";
          layout = "dwindle";
          allow_tearing = true;
        };

        decoration = {
          rounding = 10;
          blur = {
            enabled = true;
            size = 2;
            passes = 2;
            new_optimizations = true;
            xray = false;
          };
          # drop_shadow = "yes";
          # shadow_range = 4;
          # shadow_render_power = "3";
          # "col.shadow" = "rgba(1a1a1aee)";
        };

        animations = {
          enabled = "yes";
          bezier = [
            "overshot, 0.05, 0.9, 0.1, 1.05"
            "smoothOut, 0.36, 0, 0.66, -0.56"
            "smoothIn, 0.25, 1, 0.5, 1"
          ];
          animation = [
            "windows, 1, 5, overshot, slide"
            "windowsOut, 1, 4, smoothOut, slide"
            "windowsMove, 1, 4, default"
            "border, 1, 10, default"
            "fade, 1, 10, smoothIn"
            "fadeDim, 1, 10, smoothIn"
            "workspaces, 1, 6, default"
          ];
        };

        dwindle = {
          pseudotile = "yes";
          preserve_split = "yes";
        };

        misc = {
          force_default_wallpaper = 0;
        };

        workspace = cfg.workspace;

        windowrule = [
          "float, title:(file_progress)"
          "float, title:(.*[Cc]onfirm.*)"
          "float, title:(.*[Dd]ialog.*)"
          "float, title:(.*[Dd]ownload.*)"
          "float, title:(.*[Nn]otification.*)"
          "float, title:(.*[Ee]rror.*)"
          "float, title:(.*[Ss]plash.*)"
          "float, title:(.*[Cc]onfirmreset.*)"
          "float, title:(.*[Ss]ign [Ii]n - .*)"
          "float, title:(.*[Oo]pen [Ff]ile.*)"
          "float, title:(.*branchdialog.*)"
          "float, class:(.*pavucontrol.*)"
          "move onscreen cursor 0% 0%, class:(.*pavucontrol.*)"
          "float, class:(.*[Oo]verskride.*)"
          "float, class:(.*FileRoller.*)"
          "float, class:(.*wlogout.*)"
          "idleinhibit stayfocused, title:(.*mpv.*)"

          "float, class:(.*nm-connection-editor.*)"
          "move onscreen cursor 0% 0%, class:(.*nm-connection-editor.*)"

          "float, title:(Media viewer)"
          "float, class:(it.mijorus.smile),title:(Smile)"
          "float, class:(.blueman-manager-wrapped)$,title:(Bluetooth Devices)"
          # Picture in picture windows
          "float, title:(.*Picture-in-Picture.*)"
          "pin, title::(.*Picture-in-Picture.*)"

          # discord/vesktop
          "workspace: name:discord, class:(.*vesktop)"
          "float, class:(.*vesktop),title:(.*Discord Popout.*)"
          "pin, class:(.*vesktop),title:(.*Discord Popout.*)"

          # Music
          "workspace: name:discord, class:(Apple Music.*)"

          # Steam
          "float, class:(.*[Ss]team), title:(.*[Ss]team.*)$"
          "workspace name:steam silent, class:(.*[Ss]team), title:(.*[Ss]team.*)$"
          "tile, class:(.*[Ss]team), title:(.*[Ss]team.*)$"
          "float, class:(.*steam),title:(.*Friends List.*)"

          # Code
          "pin, class:(.*codium.*),title:(Save As)"
          "float, class:(.*codium.*),title:(Save As)"
          "float, class:(xdg-desktop-portal-gtk),title:(Open Workspace from File)"

          # Game Tearing??? https://wiki.hyprland.org/Configuring/Tearing/
          "immediate, class:(.*gamescope)"

          # vmware
          # this tag will set the below options to the vdi window
          # this will have it auto open as a 2160x7680 window
          # and makes multi-monitor work
          "tag +horizonrdp, class:(.*[Hh]orizon-client),title:(USPS Next VDI)"

          "noanim, tag:horizonrdp"
          "noblur, tag:horizonrdp"
          "norounding, tag:horizonrdp"
          "noshadow, tag:horizonrdp"
          "immediate, tag:horizonrdp"
          "allowsinput, tag:horizonrdp"
          "noborder, tag:horizonrdp"
          "nodim, tag:horizonrdp"
          "nomaxsize, tag:horizonrdp"
          "renderunfocused, tag:horizonrdp"
          "idleinhibit, tag:horizonrdp"
          "float, tag:horizonrdp"
          # "size 2160 7680, tag:horizonrdp"
          # "move onscreen 0 0, tag:horizonrdp"
          # float the vmware window cause its annoying to use in fullscreen
          "float, class:(.*[Hh]orizon-client),title:([Oo]mnissa [Hh]orizon [Cc]lient)"

          "tag +waydroid, class:([Ww]aydroid.*)"
          "float, tag:waydroid"
          "pin, tag:waydroid"
        ]
        ++ cfg.windowRule;

        plugin = {
          touch_gestures = {
            # The default sensitivity is probably too low on tablet screens,
            # I recommend turning it up to 4.0
            sensitivity = "4.0";

            # must be >= 3
            workspace_swipe_fingers = "3";

            # switching workspaces by swiping from an edge, this is separate from workspace_swipe_fingers
            # and can be used at the same time
            # possible values: l, r, u, or d
            # to disable it set it to anything else
            workspace_swipe_edge = "d";

            # in milliseconds
            long_press_delay = "400";

            # resize windows by long-pressing on window borders and gaps.
            # If general:resize_on_border is enabled, general:extend_border_grab_area is used for floating
            # windows
            resize_on_border_long_press = true;

            # in pixels, the distance from the edge that is considered an edge
            edge_margin = "10";

            # emulates touchpad swipes when swiping in a direction that does not trigger workspace swipe.
            # ONLY triggers when finger count is equal to workspace_swipe_fingers
            #
            # might be removed in the future in favor of event hooks
            emulate_touchpad_swipe = false;

            experimental = {
              # send proper cancel events to windows instead of hacky touch_up events,
              # NOT recommended as it crashed a few times, once it's stabilized I'll make it the default
              send_cancel = "0";
            };

            hyprgrass-bind = [
              # swipe left from right edge
              ", edge:r:l, workspace, +1"

              # swipe up from bottom edge
              ", edge:d:u, exec, ${cfg.defaultApps.browser.pname}"

              # swipe down from left edge
              ", edge:l:d, exec, pactl set-sink-volume @DEFAULT_SINK@ -4%"

              # swipe down with 4 fingers
              ", swipe:4:d, killactive"

              # swipe diagonally left and down with 3 fingers
              # l (or r) must come before d and u
              ", swipe:3:ld, exec, foot"

              # tap with 3 fingers
              ", tap:3, exec, foot"

              # longpress can trigger mouse binds:
              ", longpress:2, movewindow"
              ", longpress:3, resizewindow"
            ];
          };
        };

        gestures = {
          workspace_swipe = true;
          workspace_swipe_cancel_ratio = "0.15";
        };

        input = {
          kb_layout = "us";

          kb_variant = "";
          kb_model = "";
          kb_options = "";
          kb_rules = "";

          numlock_by_default = true;

          follow_mouse = 1;

          touchpad = {
            clickfinger_behavior = 1;
            natural_scroll = "yes";
          };

          sensitivity = 0; # -1.0 - 1.0, 0 means no modification.
        };

        experimental = {
          xx_color_management_v4 = true;
        };

        debug = {
          full_cm_proto = true;
          disable_logs = true;
          disable_scale_checks = true;
        };
      };

      extraConfig = ''
        exec-once = dbus-update-activation-environment --systemd --all
        exec-once = systemctl --user import-environment WAYLAND_DISPLAY XDG_CURRENT_DESKTOP
        exec-once = /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1
        exec-once = xhost +SI:localuser:root
        exec-once = nwg-look -a
        exec-once = nwg-dock-hyprland -d
      ''
      + cfg.extraConfig or '''';
    };
  };
}
