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

with lib;

let
  cfg = config.settings.desktop-environments.niri;
in {
  options = {
    settings.desktop-environments.niri.enable = lib.mkOption {
      type = lib.types.bool;
      description = ''
        Enable niri window manager configuration
      '';
      default = false;
    };
  };

  imports = [
    inputs.niri.homeModules.niri
    ./waybar.nix
  ];

  config = mkIf cfg.enable {
    home.packages = with pkgs; [
      fuzzel
    ];

    programs.alacritty = {
      enable = true;
      settings = {
        window.decorations = "None";
      };
    };

    programs.niri.enable = true;
    programs.niri.package = pkgs.unstable.niri;

    programs.niri.settings.outputs = {
      "HDMI-A-1".position = {
        x = 1920;
        y = 0;
      };
      "DP-1".position = {
        x = 0;
        y = 0;
      };
    };

    programs.niri.settings.layout = {
      gaps = 8;
      focus-ring = {
        width = 2;
      };
      preset-column-widths = [
        { proportion = 1. / 3.; }   
        { proportion = 1. / 2.; }   
        { proportion = 2. / 3.; }   
      ];
    };

    programs.niri.settings.workspaces = {
      "gen" = { open-on-output = "DP-1"; };
      "web" = { open-on-output = "HDMI-A-1"; };
      "com" = { open-on-output = "HDMI-A-1"; };
      "dev" = { open-on-output = "DP-1"; };
      "mus" = { open-on-output = "HDMI-A-1"; };
    };

    programs.niri.settings.binds = with config.lib.niri.actions; {
      "Mod+Shift+Slash".action = show-hotkey-overlay;

      # Spawn applications
      "Mod+Space".action = spawn "fuzzel"; 
      "Mod+T".action = spawn "alacritty";
      "Mod+B".action = spawn "firefox";

      # Volume controls
      "XF86AudioRaiseVolume".action = spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.1+";
      "XF86AudioLowerVolume".action = spawn "wpctl" "set-volume" "@DEFAULT_AUDIO_SINK@" "0.1-";
      "XF86AudioMute".action = spawn "wpctl" "set-mute" "@DEFAULT_AUDIO_SINK" "toggle";

      # Media controls
      "XF86AudioPlay".action = spawn "playerctl" "play-pause";
      "XF86AudioNext".action = spawn "playerctl" "next";
      "XF86AudioPrev".action = spawn "playerctl" "previous";

      "Mod+O".action = toggle-overview;
      "Mod+Q".action = close-window;
      "Mod+C".action = center-column;
      "Mod+R".action = switch-preset-column-width;
      "Mod+Escape".action = quit;

      # Sizing
      "Mod+Minus".action = set-column-width "-10%";
      "Mod+Equal".action = set-column-width "+10%";

      # Full screen
      "Mod+F".action = maximize-column;
      "Mod+Shift+F".action = fullscreen-window;
      "Mod+Ctrl+F".action = expand-column-to-available-width;

      # Toggle floating
      "Mod+V".action = toggle-window-floating;
      "Mod+Shift+V".action = switch-focus-between-floating-and-tiling;

      # Print screen
      "Print".action = screenshot { show-pointer=false; };

      # Window focus
      "Mod+H".action = focus-column-left;
      "Mod+J".action = focus-window-down;
      "Mod+K".action = focus-window-up;
      "Mod+L".action = focus-column-right;

      # Window moving
      "Mod+Ctrl+H".action = move-column-left;
      "Mod+Ctrl+J".action = move-window-down;
      "Mod+Ctrl+K".action = move-window-up;
      "Mod+Ctrl+L".action = move-column-right;

      # Focus to different monitor
      "Mod+Shift+H".action = focus-monitor-left;
      "Mod+Shift+J".action = focus-monitor-down;
      "Mod+Shift+K".action = focus-monitor-up;
      "Mod+Shift+L".action = focus-monitor-right;

      # Move to different monitor
      "Mod+Ctrl+Shift+H".action = move-column-to-monitor-left;
      "Mod+Ctrl+Shift+J".action = move-column-to-monitor-down;
      "Mod+Ctrl+Shift+K".action = move-column-to-monitor-up;
      "Mod+Ctrl+Shift+L".action = move-column-to-monitor-right;

      # Switch workspaces
      "Mod+Page_Down".action = focus-workspace-down;
      "Mod+Page_Up".action = focus-workspace-up;
      "Mod+U".action = focus-workspace-down;
      "Mod+I".action = focus-workspace-up;

      # Move workspaces
      "Mod+Ctrl+Page_Down".action = move-column-to-workspace-down;
      "Mod+Ctrl+Page_Up".action = move-column-to-workspace-up;
      "Mod+Ctrl+U".action = move-column-to-workspace-down;
      "Mod+Ctrl+I".action = move-column-to-workspace-up;

      # Workspace scrolling
      "Mod+WheelScrollDown" = {
        action = focus-workspace-down;
        cooldown-ms = 150;
      };
      "Mod+WheelScrollUp" = {
        action = focus-workspace-up;
        cooldown-ms = 150;
      };
      "Mod+Shift+WheelScrollDown" = {
        action = focus-column-left;
        cooldown-ms = 150;
      };
      "Mod+Shift+WheelScrollUp" = {
        action = focus-column-right;
        cooldown-ms = 150;
      };
    };
  };
}

