# TODO: Hyprland theme
# TODO: Enforce enabling hyprland module in osConfig, if applicable
# TODO: Common desktop module
# TODO: Better organization
{
  config,
  lib,
  pkgs,
  theme,
  inputs,
  osConfig,
  ...
}: let
  cfg = config.niko.desktop.hyprland;
  screenshotScript = pkgs.writeScript "screenshot" ''
    #!/bin/sh
    grim -g "$(slurp)" - | \
      wl-copy -t image/png
  '';
in {
  options.niko.desktop.hyprland = {
    enable = lib.mkEnableOption "Hyprland window manager";
    extraConfig = with lib;
      mkOption {
        type = types.lines;
        default = "";
        description = "Extra Hyprland config";
      };
  };

  config = lib.mkIf cfg.enable {
    # https://wiki.hyprland.org/FAQ/#how-do-i-change-me-mouse-cursor
    home.packages = with pkgs; [wofi wl-clipboard grim slurp chromium swaylock catppuccin-cursors.mochaDark];

    wayland.windowManager.hyprland = {
      enable = true;
      xwayland = {
        enable = true;
        hidpi = true;
      };
      extraConfig = ''
        $mod = SUPER

        input {
          kb_layout = pl
          follow_mouse = 1
          accel_profile = flat
          kb_options = ${osConfig.services.xserver.xkbOptions}
        }

        general {
          gaps_in = 5
          gaps_out = 5
          border_size = 2
        }

        decoration {
          rounding = 8
        }

        bind = $mod, Return, exec, alacritty

        bind = $mod SHIFT, E, exec, pkill Hyprland

        bind = $mod, Q, killactive,
        bind = $mod, F, fullscreen,
        bind = $mod SHIFT, Space, togglefloating,

        bind = $mod, left, movefocus, l
        bind = $mod, right, movefocus, r
        bind = $mod, up, movefocus, u
        bind = $mod, down, movefocus, d

        bind = $mod, H, movefocus, l
        bind = $mod, L, movefocus, r
        bind = $mod, K, movefocus, u
        bind = $mod, J, movefocus, d

        bind = $mod, D, exec, wofi -S drun

        bind = $mod SHIFT, S, exec, ${screenshotScript}

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

        bind = $mod SHIFT, 1, movetoworkspace, 1
        bind = $mod SHIFT, 2, movetoworkspace, 2
        bind = $mod SHIFT, 3, movetoworkspace, 3
        bind = $mod SHIFT, 4, movetoworkspace, 4
        bind = $mod SHIFT, 5, movetoworkspace, 5
        bind = $mod SHIFT, 6, movetoworkspace, 6
        bind = $mod SHIFT, 7, movetoworkspace, 7
        bind = $mod SHIFT, 8, movetoworkspace, 8
        bind = $mod SHIFT, 9, movetoworkspace, 9
        bind = $mod SHIFT, 0, movetoworkspace, 10

        bindm = $mod, mouse:272, movewindow
        bindm = $mod, mouse:273, resizewindow
        ${cfg.extraConfig}
      '';
    };

    home.file."start-hypr".source = pkgs.writeScript "start-hypr" ''
      #!/bin/sh
      cd ~
      exec Hyprland
    '';
  };
}